Main menu

Linux shell commands

Submitted by johng on 4 April, 2011 - 17:19

The linux shell command line interface is a powerful tool for performing system administration tasks. In comparison a graphical user interface (GUI) is easier to use but has limitations: (i) confined to perform specific tasks, (ii) required to navigate through menus and (iii) not easily configurable to perform batch jobs.

Some of the advantages in using the command line interface are:

  • It is often faster to type a command on the keyboard than having to navigate menus in a GUI
  • The command line interface usually requires less resources than a GUI
  • Commands can be used in scripting to produce highly customisable and powerful tools to perform specific tasks

Many books and online resources are available on shell commands. As a starting point a self-contained manual page is available at the console by using the man command:

man name_of_command

For example the following command will provide details about the du command

$ man du

DU(1)                            User Commands                           DU(1)

NAME
       du - estimate file space usage
 
SYNOPSIS
       du [OPTION]... [FILE]...
       du [OPTION]... --files0-from=F
 
DESCRIPTION
       Summarize disk usage of each FILE, recursively for directories.
 
       Mandatory  arguments  to  long  options are mandatory for short options
       too.
 
       -a, --all
              write counts for all files, not just directories
 
       --apparent-size
              print apparent sizes,  rather  than  disk  usage;  although  the
              apparent  size is usually smaller, it may be larger due to holes
              in (`sparse') files, internal  fragmentation,  indirect  blocks,
              and the like
 
       -B, --block-size=SIZE
              use SIZE-byte blocks

etc...

 

Command Line Reference

A summary of commonly used (bash shell) commands is tabulated below. Specific examples are given in the sections that follow.

Basic Commands (Navigation and Disk Space)
cd Change directory
cp Copy files or directories
df Report file system disk space usage
du Estimate the amount of disk space used by file(s)/directories
ls List directory contents
pwd Display the name of the current directory
rm Delete files or directories
Files and Permissions
cat Concatenate files (print contents to standard output)
chmod Set file permissions
chown Set file ownership and group
diff Compare the contents of two files and report the difference
file Determine the file type
head Display the first part of a file
less View a file (with backward screen movement)
more View a file (a screen at a time)
sort Sort lines in a text file
stat Display file details and attributes
tail Display the last part of a file
Searching & String Manipulation
awk Pattern scanning and processing
find Search for files in a directory structure
grep Search for patterns in a file
sed Stream editor for performing text transformations
locate Find files by name
System Administration & Diagnostics
dmesg Print the contents of the kernel ring buffer (useful for troubleshooting hardware issues)
fdisk Disk partition table tool
finger Display user information
lsof List open files
lspci List PCI devices
ps Reporting tool of current processes
kill Send a signal to a running process (a terminate signal by default)
shutdown Shutdown the system
top Display linux tasks
uname Print system information (e.g. hostname, kernel version)
mount Mount a filesystem
w Display currently logged in users
Archiving & Backups
rsync File copy & synchronisation tool. Often used for backing up files to remote server
tar Archiving utility
Networking & Remote Access
hostname Display the system hostname
ifconfig Configure a network interface
nslookup Query a host
ping Test the connectivity to a host
ssh Secure shell access - Remote login program

 


Examples

Basic Commands - Directories & Files

ls List the contents of the current directory
ls -lat List of the contents of the current directory. Include hidden files, long listing format and sort by modification time
ls -R List the contents of the current directory. Include all subdirectories in the long listing format
pwd Display the name of the current directory
cd .. Move up one directory level (from the current working directory)
cd /home/joe_blogs Change the current working directory to /home/joe_blogs
cp imagetest.jpg imagetest2.jpg Copy the file imagetest.jpg to imagetest2.jpg
cp -rp directory1 directory2 Copy the directory1 (and all contents within) to directory2, preserving file permissions
mv movie1.iso movie2.iso Rename the file movie1.iso to movie2.iso
rm module.h Delete the file module.h
rm -r sqlite3 Delete the directory sqlite3. The recurse option (-r) will delete the directory and all files contained within
mkdir Images Create a directory called Images (in the current working directory)
rmdir fontconfig Remove the directory fontconfig. This command will only work if the directory is empty

Basic Commands - Disk Space Monitoring

df Show free space on all mounted file systems
df -h Show free space on all mounted file systems in human readable format (e.g. MB, GB, ...)
df -h /home Show the amount of free space on the file system /home in human readable format
du Report the disk usage of all files in the current directory (recurse into sub directories) in kB
du -sh Report the total disk usage of all files in the current directory (recurse into sub directories) in kB
du * | sort -nr | head -10 Create a report of disk usage of all files in the current directory. Show the top 10 files (sorted by file size)

Files & Permissions

cat graphics.h Print the contents of the file graphics.h to the screen
more /etc/profile Display the contents of the file /etc/profile one screen full at a time
less textfile.dat Display the contents of the file /etc/profile. Similar to more with the added feature that backward movement is allowed
head license.dat Display the first part of the file license.dat
tail /etc/passwd Display the last part of the file /etc/passwd. The default is to display the last 10 lines if no options are given
tail -f /var/log/messages Display the last part of the file /var/log/messages with the follow option. The output will be updated as data is appended to the file
chmod g+rw archive.md5 Set the group permission of the file archive.md5 to read and write
chmod 644 cerfiticate.pdf Set the group permission of the file archive.md5 to read and write
chown -R john backups Set the ownership of the directory backups to john. The recursive option (-R) has been selected to operate on all files and sub-directories inside the directory called backups
file patch.py Report the file type (for the file patch.py). Some possible results include (i) ASCII text files, (ii) binaries and (iii) script executables
stat grid.bmp Display the status of a file, i.e. whether it is a file or directory, permissions, modification time etc ...
sort -nr filename Sort the contents of the filename using a numerical sort in reverse order
sort -k2 filename Sort the contents of the filename by the 2nd field (the default field delimeter is a space)
diff file1 file2 Report the differences between file1 and file2. Lines from file1 (not in file2) are displayed with < (at the beginning of each line). Lines in file2 (not in file1) are displayed with >
diff -y file1 file2 Report the differences between file1 and file2. Display the output in 2 columns side by side

Searching - Files

find . -type f -name '*.pdf' Search for files ending in .pdf in the current directory (and recurse into sub-directories)
find ~ -type f -name '*.doc' | wc -l Count the number of files ending in .doc in the home directory (~). Note how the output of the find command is piped into the wc command
find /var/log -type f -mtime +365 Search for files in /var/log that were modified over a year ago
locate *.jpg Files all files ending in jpg (*.jpg) on the system. locate relies on a system database (updatedb) to be update daily by cron.

Searching - Text file and string manipulation

grep "joe" filename.txt Search for the string "joe" in the file filename.txt
grep -A 3 fred /etc/passwd Search for "fred" in the file /etc/passwd. If there is a match also display the next 3 lines
grep -i error report.log Search for the string "error" in the file report.log. The -i option indicates that the match is case-insensitive, e.g. ERROR will be reported as a match
grep -v "apples" list.dat Display lines in the file list.dat which don't match the pattern "apples". The -v option inverts the match
grep -v '^$' file.dat Display the contents of the file file.dat with the exception of blank lines. The special characters ^ and $ refer to the beginning and the end of a line respectively
awk '{print $1}' file.dat Print the 1st column of the contents of the file file.dat
ls -l | awk '{print $NF}' Print the last field on the output of the command ls -l
awk -F":" '{print $3}' /etc/passwd Print a list of all the user IDs on the current system, i.e. for each line in /etc/passwd print out the 3rd field. The field delimeter is the colon
sed 's/John/Bill/g' address.dat Change all instances of the string "John" to "Bill" in the file address.dat. The command option "s" denotes substitution and the "g" denotes global replacement, i.e. make changes to the whole file
sed '/^$/d' x.dat Remove all blank lines using the file x.dat as input data. ^ and $ are special characters denoting the beginning and end of a line respectively

Archiving & Backups

tar -cvf bin.tar bin Create the tar archive file bin.tar of all files in the bin directory: Options: -c = create, -v = verbose output and -f specifies the tar file
tar -tf bin.tar List the contents of the tar file bin.tar. Options: -t = list the contents, -f specifies the filename
tar -xvf bin.tar Extract the contents of bin.tar into to the current directory. Options: -x = extract, -v = verbose output and -f specifies the tar file
rsync -av /directory /dest/path Copy files from /directory to /dest/path (archive mode and verbose output)
rsync -avx /var /some/directory Copy files from /var to /some/directory (archive mode, verbose output and don't cross file system boundaries)
rsync -av --delete /home/joe user@host:/backup Copy files using ssh from /home/joe (archive mode & verbose output) to the directory /backup on the remote server with hostname host. Use user to authenticate and delete files at the destination that don't exist on the source machine (i.e. /home/joe)

System Administration & Diagnostics

top Display linux tasks
ps -aef Display all running processes in a full format listing
uptime Report how long the system has been up and running (in days)
uname -a Print system information such as hostname, kernel version, platform and processor type
dmesg Print the kernel ring buffer. Useful for diagnosing hardware problems
cat /etc/issue Report the operating system
passwd Change the user password
id  Print the user ID of the current user
id user Print the user (and group) ID of user
lsof List all open files
lsof /data List all open files in the filesystem /data
lsof -p 12345 List all open files 
lsof -u user123 List all files that are currently open by the user user123
kill 12345 Send the terminal signal to process with ID 12345. This will attempt to kill the process (with signal id 15)
kill -9 12345 Send the kill signal to the process with ID 12345. This is more aggressive than the previous example
exit exit the current shell
shutdown -r now Reboot the system immediately
finger username Display information about the user username
su Switch user (become super user)
su username Switch to user username in the current login session
su - username Switch to user username in the current login session. Setup the user environment as if the user username logged in directly
sudo command Execute the command as another user. In this case there is no argument provided for the username (i.e. - username). The command will be executed as the root user
mount /home mount the filesystem labelled /home (in /etc/fstab)
mount -t iso9660 /dev/sr0 /mnt/cd mount a CD (with device name /dev/sr0) to the mount point /mnt/cd
w Show a list of users currently logged into the system

Networking & Remote Access

hostname Display the hostname (of the current session)
ifconfig -a Display details of all network interfaces (even if they are down)
ifconfig eth1 192.168.2.1 Assign 192.168.2.1 as the IP address for the network interface eth0
ifconfig eth0 up Enable the network interface eth0
nslookup www.example.com Query the name server www.example.com
nslookup 192.168.2.10 Query the server with IP address 192.168.2.10
ping 74.125.237.50 ping the machine with IP address 74.125.237.50
ping domainname.com ping the mahine with DNS name domainname.com
ssh -l joe machinename Connect using a secure shell login program to the host called machinename as the user joe
ssh -X -i key2 fred@machine Connect using a secure shell login program to the host called machine with X-forwarding enabled as the user fred and using the private key key2

References and Related links

A basic linux command guide http://www.my-guides.net/en/content/view/29/26/
A more comprehensive guide http://www.pixelbeat.org/cmdline.html
Unix (Linux) tutorial for beginners http://www.ee.surrey.ac.uk/Teaching/Unix/

Further Reading

  • Linux Command Line and Shell Scripting Bible (Richard Blum)
  • Learning the bash Shell (Cameron Newman)
  • Unix and Linux System Administration Handbook (Evi Nemeth, Garth Snyder, Trent R. Hein and Ben Whaley)