Some Linux commands
One of the steepest Unix/Linux learning curves is the sheer number of built-in commands, all of which have many options -- most of which you'll never use. Plus there are a number of advanced commands that are extremely powerful but also extremely complex.
To help address this, this page introduces a number of built-in Linux utilities along with some of their common options, by category.
Command line file arguments can be replaced by standard input
Most built-in Linux commands that obtain data from a file can also accept the data piped in on their standard input.
And here's a Linux commands cheat sheet you may find useful:
Basic commands
Displaying file contents
cat outputs all the contents of its input (one or more files and/or standard input) or the specified file
cat -n prefixes each line of output with its line number
CAUTION – only use on small files!
zcat <file.gz> like cat, but understands the gzip (.gz) format, and decompresses the data before writing it to standard output
CAUTION – only use on small files! ..or pipe the output to more
Another CAUTION – does not understand .zip or .bz2 compression formats
more and less pagers
both display their (possibly very long) input one Terminal "page" at a time
in more:
use spacebar to advance a page
use q or Ctrl-c to exit more
in less:
q – quit
left, right arrows - move back or forward one character
up, down arrows - move up or down one line
Ctrl-f or spacebar – page forward
Ctrl-b – page backward
/<pattern> – search for <pattern> in forward direction
n – next match
N – previous match
?<pattern> – search for <pattern> in backward direction
n – previous match going back
N – next match going forward
use less -N to display line Numbers
use less -I to use case Insensitive pattern searches
less can be used directly on .gz format files
head and tail
show you the first or last 10 lines (by default) of their input
head -n 20 or just head -20 shows the first 20 lines
tail -n 2 or just tail -2 shows the last 2 lines
tail -n +100 or just tail +100 shows all lines starting at line 100
tail -n +100 | head -20 shows 20 lines starting at line 100
tail -f shows the last lines of a file, then follows the output as more lines are written (Ctrl-c to quit)
gzip <file> – compresses <file> contents creating a <file.gz> compressed file
and removes the original <file>
gunzip <file.gz> – un-compresses <file.gz> contents creating a <file> file
and removes the <file.gz> file
gunzip -c <file.gz> | more (or less)
like zcat, un-compresses lines of <file.gz> and outputs them to standard output
<file.gz> is not altered on disk
always pipe the output to a pager!
File system navigation
ls - list the contents of the specified directory
-l says produce a long listing (including file permissions, sizes, owner and group)
-a says show all files, even normally-hidden dot files whose names start with a period ( . )
-h says to show file sizes in human readable form (e.g. 12M instead of 12201749)
-t says to sort files on last modification time
-r says to reverse the current sort order
-d says to show directory listing information only, instead of directory contents
usually combined with -l, e.g.: ls -ld <dirname>
cd <whereto> - change the current working directory to <whereto>. Some special <wheretos>:
.. (period period) means "up one level"
~ (tilde) means "my home directory"
- (dash) means "the last directory I was in"
. (period) means "the current directory"
find <in_directory> [ operators ] -name <expression> [ tests ]
looks for files matching <expression> in <in_directory> and its sub-directories
<expression> can be a double-quoted string including pathname wildcards (e.g. "[a-g]*.txt")
there are tons of operators and tests:
-type f (file) and -type d (directory) are useful tests
-maxdepth NN is a useful operator to limit the depth of recursion
-iname ignores case when matching
file <file> tells you what kind of file <file> is
df shows you the top level directory structure of the system you're working on, along with how much disk space is available
-h says to show sizes in human readable form (e.g. 12G instead of 12318201749)
pwd - display the present working directory
-P says to display the full absolute path
tree <directory> - shows the file system hierarchy of the specified directory
tree is not available on all Linux systems
Create, rename, link to, delete files
touch <file> – create an empty file, or update the modification timestamp on an existing file
mkdir -p <dirname> – create directory <dirname>.
-p says to also create any needed sub-directories
mv <file1> <file2> – renames <file1> to <file2>
mv <file1> ... <fileN> <to_dir>/ – moves files <file1> ... <fileN> into directory <to_dir>
mv -t <dir> <file1> ... <fileN> – same as above, but specifies the target directory as an option
via the -t <to_dir> option
ln -s <path> creates a symbolic (-s) link (symlink) to <path> in the current directory
a symbolic link can be manipulated as if it is the linked-to file or directory
and can be deleted without affecting the linked-to file/directory
the default link file name corresponds to the last name component in <path>
always specify the -s option to create a symbolic link
without the -s option a difficult-to-manage "hard link" is created
always change into (cd) the directory where you want the link before executing ln -s
ln -sf -t <target_dir> <file1> <file2> ... <fileN>
creates symbolic links to <file1> <file2> ... <fileN> in target directory <target_dir>
rm <file> deletes a file. This is permanent - not a "trash can" deletion.
rm -rf <dirname> deletes an entire directory – be careful!
Copying files and directories
cp <source> [<source>...] <destination> copies the file(s) <source> [<source>...] to the directory or file <destination>
using . (period) as the destination means "here, with the same name"
-p option says to preserve file modification timestamps
cp -r <dirname>/ <destination>/ will recursively copy the directory <dirname>/ and all its contents to the directory <destination>/.
cp -t <dirname>/ <file> [<file>...] copies one or more specified files to the target directory.
scp <user>@<host>:<remote_source_path> <local_destination_path>
Works just like cp but copies <remote_source_path> from the remote host machine to the <local_destination_path>
-p (preserve file times) and -r (recursive) options work the same as cp
scp <local_source_path>... <user>@<host>:<remote_destination_path> is similar, but copies one or more <local_source_path> to the <remote_destination_path> on the remote host machine.
A nice scp syntax resource is located here.
wget <url> fetches a file from a valid URL (e.g. http, https, ftp).
default output file is named as the last component of the URL
-O <file> specifies the name for the local file
rsync -arvW <source_directory>/ <target_directory>/
rsync -ptlrvP <source_directory>/ <target_directory>/Recursively copies <source_directory> contents to <target_directory>, but only if <source_directory> files are newer or don't yet exist in <target_directory>
Remote path syntax (<user>@<host>:<absolute_or_home-relative_path>) can be used for either source or target, but not both.
Always include a trailing slash ( / ) after the source and target directory names!
-a means "archive" mode (equivalent to -ptl and some other options)
-r means recursively copy sub-directories (this is now the default behavior)
-v means verbose
-W means Whole file only
Normally the rsync algorithm compares the contents of files that need to be copied and only transfers the different portions. This option disables file content comparisons, which are not appropriate for large and/or binary files.
-p means preserve file permissions
-t means preserve file times
-l means copy symbolic links as links (this is the default behavior)
vs -L which means dereference the link and copy the file it refers to
-P means show transfer Progress (useful when large files are being transferred)
see https://manpages.ubuntu.com/manpages/trusty/man1/rsync.1.html
Miscellaneous commands
echo <text> prints the specified text on standard output
evaluation of metacharacters (special characters) inside the text may be performed first
-e says to enable interpretation of backslash escapes such as \t (tab) and \n newline
-n says not to output the trailing newline
wc -l reports the number of lines in its input
wc -c reports the number of characters in its input
wc -w reports the number of words in its input
history lists your command history since login to the terminal
redirect to a file to save a history of the commands executed in a shell session
pipe to grep to search for a particular command
re-execute a previous command via !<NN> where <NN> is the history line number
env lists all the environment variables currently defined in your login session
seq N produce N numbers, 1-N, each on a separate line
xargs transfers data on its standard input to the command line of the specified command
e.g. ls ~/*.txt | xargs echo
which <pgm> searches all $PATH directories to find <pgm> and reports its full pathname
will report all the places it looked if <pgm> was not found
type <pgm> is more general and works for functions and aliases
du <file_or_directory..file_or_directory>
shows the disk usage (size) of the specified files/directories
-h says report the size in human-readable form (e.g. 12M instead of 12201749)
-s says summarize the directory size for directories
-c says print a grand total when multiple items are specified
groups - lists the Unix groups you belong to
Advanced commands
cut, sort, uniq
cut command lets you isolate ranges of data from its input lines