Linux Command Line Tips

Various Notes on Using and Managing a Linux System from the Command Line


  • A line that start with a "$" denotes a command line entry.
  • "#" and what follows are comments. A '#" can appear later on a line and everything that follows on the line becomes a comment.
  • Most examples will work equally well on an Ubuntu desktop or a non-minimally Linux system built with Yocto.
  • Entries below are roughly in alphabetical order
  • Most of the commands have many options. Refer to the man pages for more information.

Annoying Bell

Turn off the bell you hear while working on the command line

edit /etc/inputrc:

# do not bell on tab-completion
set bell-style none


Quickly build a searchable cscope database / index of your project:

$ find -L . -name '*.[chS]' > cscope.files
$ cscope -b -q -k
$ cscope -d


date uses the following format: [MMDDhhmm[[CC]YY][.ss]]

$ # assume it's noon on March 13 2016:
$ date 031312002016
Sun Mar 13 12:00:00 EDT 2016

$ # check how timezone is set:
$ ls -l /etc/localtime 
lrwxrwxrwx 1 root root 27 Jan  4 19:17 /etc/localtime -> /usr/share/zoneinfo/EST5EDT

$ # change it to central:
$ rm /etc/localtime
$ ln -s /usr/share/zoneinfo/CST6CDT /etc/localtime
$ date 
Sun Mar 13 11:02:10 CDT 2016

Additional notes on date and timezone:

  • Use ntp daemon to automatically update date and time
  • The TZ setting can also be modified in /etc/profile

Basic Disk Operations

Tasks for creating a new drive partition mounting, testing, and fixing it (need to be root).

$ # format partitions on an unmounted drive:
$fdisk /dev/sda

$ # create a filesystem on the third SDA partition:
$ mke2fs /dev/sda3

$ # mount the new partition at /mnt/drive:
$ mkdir -p /mnt/drive
$ mount /dev/sda3 /mnt/drive

$ # test and fix disk errors on the partition (unmount first)
$ e2fsck /dev/sda3 

$ # or to automatically fix errors and be verbose:
$ e2fsck -vy /dev/sda3


Control environment variables from the command line:

$ export var1="value"	# note there are no spaces

$ echo $var1		# return the value of "var1"

$ echo "I like $var1"
I like value

$ unset var1		# remove the environemnt variable

$ echo $var1

$ set | grep var1


Non-trivial examples using a very powerful but non-intuitive tool:

$ # find *.py files and copy to a temp directory
$ find . -name '*.py' | xargs -I{} cp {} ~/temp

$ # delete *.js files that aren't minified: 
$ find . -path '*.js' ! -path '*.min.js' | xargs rm

$ # find my js files but exclude those in node_modules and build
find . -path '*.js' ! -path "./node_modules/*" ! -path "./build/*"

$ # Find source files in a C project and include symbolic links
$ find -L  . -name '*.[chS]'


Some useful grep options for searching through files:

$ # search recursively for "elf" as a word and case insensitive
$ grep -iwR elf 

$ # do the same thing but exclude binary files 
$ grep -iwIR elf 

$ # find "elf" when it's at the end of a line, note use of regex here
$ grep -iwR 'elf$'


Note: syslog can usually be found at /var/log

$ # write to the kernel log:
$ echo Hello World > /dev/kmsg

$ tail -n 1 /var/log/syslog
Mar 13 13:19:47 kernel: [71698.997888] Hello World

$ # write to syslog and stderr with specific tag
$ logger -s -t 'tag' 'hello world again'
<13>Mar 14 01:49:11 tag: hello world again

$ tail -n 1 /var/log/syslog 
Mar 14 01:49:11 ls1021atwr tag: hello world again

man pages

Use the man pages effectively:

$ # search for socket man pages
$ man -f socket		
socket (7)           - Linux socket interface
socket (2)           - create an endpoint for communication
Socket (3perl)       - networking constants and support functions

$ # list socket(7)
$ man -S 7 socket

$ # search for man pages with socket in the short description:
$ man -k socket
Socket (3perl)       - networking constants and support functions
accept (2)           - accept a connection on a socket
accept4 (2)          - accept a connection on a socket

$ # walk through each man page for "socket":
$ man -a socket

Processor Affinity

Run a command on a particular CPU core

$ # Use a bit mask to specify the CPU 
$ # example: cat the cpu stats using CPU 1 (the second core) 
$ taskset 2 cat /proc/cpuinfo	

User and Group ID

This example is run from a freshly installed embedded system as root.

$ # return our groups:
$ groups 

$ # get our real and effective user & group IDs
$ id
uid=0(root) gid=0(root) groups=0(root)

Help us improve this article by adding your comment or question:

email addresses are neither displayed nor shared