Linux Commands

Linux provides a very large number of commands and utilities. Some of the more important, popular, and useful commands are listed by category below with a brief description, common options, and examples.

File and Directory Commands

This list includes basic commands to: change directory; display directory listing; create, copy, move, remove, and rename files and directories.

Command Description
cd Change current directory. With no argument, changes to user's home directory.
  • Examples:
  • cd Documents/todo   change current directory to Documents/todo directory
  • cd ..   change current directory to parent directory
ls Display directory contents or information about file(s). Options include:
  • -l   long-format listing (includes permissions, owner, size, modification time, etc.)
  • -a   display all files and directories, including hidden (-A displays all but . and ..)
  • -h   human-readable file sizes with long-format listings (Kilobytes, Megabytes, etc.)
  • -R   recursive, -S sort by file size, -t sort by time/date modified
  • Examples:
  • ls -l memo.txt   display long-format information about memo.txt
  • ls -lhS Documents/todo   display long-format information on contents of todo directory, sorted by file size specified in human-readable format
touch Create new empty file if it doesn't exist, update access and modification time if it does.
Example:   touch notes.txt
cp Copy files and directories. Many options, including:
  • -i   interactive; prompts before overwriting destination file
  • -u   update only; won't overwrite newer destination file
  • -p   preserves attributes: ownership, permissions, timestamps
  • -a   archive: preserves attributes, recursive (copies subdirectories)
  • Examples:
  • cp file.txt file.txt.bk  
  • cp memo1.txt memo2.txt memos   copy into memos directory (must exist)
mv Move or rename files and directories.
  • Examples:
  • mv notes.txt todo.txt   rename notes.txt to todo.txt
  • mv todo.txt memo1.txt misc   move two files into misc directory.
  • mv -i todo.txt Documents/April   move todo.txt into Documents/April directory, with prompt before overwrite. Update option (-u) also supported.
mkdir Create one or more directories.
  • Examples:
  • mkdir todo   create todo directory
  • mkdir -p dir/subdir   create parent directory too with -p option
rmdir Remove empty directories.
  • Examples:
  • rmdir todo   error if not an empty directory
  • rmdir -p dir/subdir   remove heirarchy of empty directories using -p option
rm Remove files (and directories with -r option).
  • Examples:
  • rm memo.txt   removes memo.txt
  • rm -i memo.txt   asks first with interactive option (-i). Enter y or Y to confirm
  • rm -r dir   recursive option (-r) will remove non-empty directories

Commands to View and Manipulate Files

Command Description
cat Display or concatenate files.
  • Examples:
  • cat memos.txt   display memos.txt
  • cat memos*.txt > allmemos.txt   concatenate matching files into allmemos.txt[1]
less Display long text files and other output one screen at a time. Includes lots of options and commands. Press h when in use to see commands summarized.
  • Example:
  • sudo less /var/log/apache2/access.log   page through log file
head Display beginning of file(s); default is first 10 lines.
  • Examples:
  • head notes.txt   display first 10 lines of notes.txt
  • head -6 memo.txt notes.txt   display first six lines of each file with header
tail Display last lines of file(s), 10 by default.
  • Examples:
  • tail -n 20 todo.txt   display of last 20 lines of file
  • sudo tail -f /var/log/apache2/access.log   display last 10 lines of log file and follow it, displaying lines as file continues to grow.
sort Display sorted contents of file(s). Options include: numeric sort, reverse sort, sort on field(s), field separator, and more. See info sort for details and examples.
cut Display selected characters or fields from lines of input. Options include:
  • -c   list characters to be included by column number
  • -f   list fields to be included
  • -d   field delimeter (default is tab)
  • Example:
  • ls -l | tr -s ' ' | cut -d' ' -f5,9   display file size and file name from directory listing.[2]
uniq Display unique lines from input file, or report duplicate lines.
diff Displays differences between two files.
  • Example:
  • diff -u memo.txt memo-rev.txt   Display differences using unified format.
tr Translate, squeeze and/or delete characters. Options include:
  • -d   delete specified character(s)
  • -s   replace multiple instances of specified character with single character
  • Examples:
  • echo A Cat Box | tr 'ABC' 'abc'   displays a cat box
  • tr -d '\r' < dos-file.txt > unix-file.txt   removes carriage returns (\r) [3]
  • ls -l | tr -s ' ' | cut -d' ' -f5,9   removes extra space characters
wc Display number of lines, words, and bytes in file(s).
  • Examples:
  • wc -l todo.txt   display number of lines in file
  • wc -w draft1.txt draft2.txt   display number of words in each file, plus total
grep Search file(s) for lines that match a pattern (string or regular expression). Options for extended and perl-compatible regular expressions, plus case insensitive, whole-word, recursive searches, and much more.
  • Example:
  • who | grep jdoe   search list of logged in users for jdoe

Commands to Get Information on Files or Utilities

Command Description
type Display type of command (path to utility, shell builtin, shell keyword, alias).
  • Examples:
  • type cp   cp is /bin/cp
  • type cd   cd is a shell builtin
which Display path to command(s). Searches $PATH and displays first match.
  • Example:
  • which python   /usr/bin/python
whereis Search standard directories for location of command(s), related manual and source files.
  • Example:
  • whereis python3   python3: /usr/bin/python3.5 ... /usr/share/man/man1/python3.1.gz
locate Search database(s) and display filenames matching pattern.
  • Example:
  • locate openssh-server   /etc/network/if-up.d/openssh-server...
file Display file type information
  • Examples:
  • file /bin/cp   /bin/cp: ELF 64-bit LSB executable, x86-64, version 1 ...
  • file Bourne-Again shell script, ASCII text executable
  • file notes.txt   notes.txt: ASCII text
find Find files in specified directories based on specified criteria. Search criteria include:
  • -type   file type (e.g., f for regular file or d for directory)
  • -name   file name matching pattern
  • -mtime   modification time
  • Example:
  • find ~/bin -type f -mtime -2   List files in user's bin directory modified within last 2 days

Commands to Get System Information

Command Description
free Display information on memory usage: used, free, swap, buffer, cache, etc.
  • Example:
  • free -mt   includes total column, amounts in megabytes
df Display available disk space on mounted file systems or those passed as argument(s).
  • Example:
  • df -h /dev/sda2   shows disk usage on sda2 in human-readable units
du Display disk usage information for current or specified directory/subdirectories.
  • Example:
  • du -ah Documents/misc   disk space used by all files in Documents/misc
top Dynamic display of system information and process status. Many options and interactive commands (e.g., M sorts on memory use).
uptime Display time, how long system has been running, number of users, system load average over 1, 5, and 15 minutes.

Miscellaneous Commands

This list includes a grab bag of other commands that are useful in writing shell scripts.

Command Description
date Display time and date with format options. User with root privileges can set system clock.
  • Example:
  • date '+Date: %B %d, %Y'   Result: Date: March 14, 2018
echo Display output. Options: -e support escape sequences, -n suppress newline
  • Example:
  • echo -e "name \t address \t phone"  
printf Display formatted string. Bash version of function popularized in the C language. One noteable difference: in Bash, arguments are separated by spaces, not commas.
  • Example: printf "%.2f\n" .2   Result: 0.20
read Prompt user for input and store response in variable

Sources of Information on Linux Commands

The man and info commands and the --help option all provide information on commands. The format of the output for each is different; the information provided may be similar, or it may be very different. The following shows how to access each of these resources for the ls and sed commands:

ls --help
man ls
info ls

sed --help
man sed
info sed

The difference between man sed and info sed is particularly noteworthy with info sed providing a comprehensive tutorial with examples.

The information provided with info ls is part of info coreutils which is the documentation for the GNU core utilities for text and file manipulation. This is very helpful documentation organizing information on the utilities by category.

Both man and info provide documentation for a large number of programs. Invoke either command followed by two presses of the tab key to see the complete list.

The Bash help Command

The Bash help command provides information on the builtin commands, control stuctures, and keywords. The following examples invoke help to find information on the let command and the while loop:

help let

help while

Back to top

  1. The example uses an ambiguous file reference and redirection. ^
  2. The example is from the excellent book Linux Commands, Editors, and Shell Programming by Mark Sobell. ^
  3. This example is borrowed from Classic Shell Scripting, which is a bit dated but still an excellent resource. ^