Last updated: 28 April 2021

This article provides a brief overview of common Linux commands. If you are new to the Linux command line, there are lots of free resources for learning more about the topic. We quite like Linux Notes for Professionals, which is compiled from Stack Overflow documentation. However, a quick online search will return plenty of other guides and books.

We also got knowledgebase articles about more advanced command line utilities:

pwd

Print the name of the current directory (the “working directory”).

ls

List all files in the current directory, apart from hidden files (“dot files”).

ls -a

List all files, including hidden files.

ls -l

Long listing (includes information about files and directories).

ls -lt

Long listing sorted list by date (newest file first).

ls -lS

Long listing sorted by size (largest file first).

ls -lSr

Long listing sorted by size and in reverse order (smallest file first).

ls -lh

Long listing with files sizes in human-readable format (i.e. “1K” instead of “1024”).

cd

Change the working directory to the home directory ($HOME).

cd [dir]

Change the working directory to the directory [dir].

cd ..

Change to the parent directory.

cd –

Change the working directory to the the last working directory.

Working with files

touch [file]

Create the file [file]. If the file already exists the timestamp will be updated.

file [file]

Determine the file type. File extensions have no meaning in Linux. For instance, a zip archive doesn’t need to have a .zip extension. Similarly, a PHP script can have a .png extension (which is fairly common on compromised websites). The file utility confirms the actual file type.

cp [source] [destination]

Copy [source] to [destination]. For instance, cp wp-config.php wp-config.php.bak makes a backup of the wp-config.php file.

cp -a [source] [destination]

Preserve file attributes such as the ownership and timestamps. Without this option attributes are overwritten.

cp -i [source] [destination]

Prompt before overwriting an existing file. By default cp overwrites any existing destination file.

rm [file]

Remove [file]. The file is permanently deleted – there is no wastebasket.

rm -i [file]

Prompt before removing a file.

rm *

Remove all files in the current directory. This does not remove directories.

rm -r [directory]

Remove [directory] and anything contained in it. Please tripple-check that the command you entered is correct – you won’t be the first person to accidentally delete important data.

rm -rf [directory]

Remove [directory] and anything contained in it, and never prompt before removing a file.

rmdir [directory]

Remove [directory]. The directory is only removed if it is empty.

mkdir [directory]

Create (make) the directory [directory].

mkdir -p [directory]/[directory]

Make the directory and any parent directories (if they don’t exist yet).

cat [file]

Print the contents of [file]. Although the cat utility is designed to concatenate files it is more commonly used to print the contents of one or more files.

less [file]

Print the contents of [file] using a pager. The pager is used to display the contents of the file one page at the time, which is useful if the file is very large.

You can use your arrow keys and the Page Up and Page Down keys to navigate up and down (Home and End also work), and you can search the file using a forward stroke (/) followed by the search string (i.e. /search_string). Search results are highlighted and you can use the n key to move the cursor to the next match.

head [file]

Print the first ten lines of [file].

head -3 [file]

Print the first three lines of [file].

tail [file]

Print the last ten lines of [file]. As with head, you can change the number of lines to be displayed. For instance, tail -5 prints the last five lines.

tail -f [file]

Print the last ten lines of [file] and follow any changes made to the file. Any lines that are added to the file are printed to the screen. This is useful to monitor log files.

chown [user]:[group] [file]

Change the [user] and [group] owner of [file]. Note that the user and group are separated by a colon (:).

chown -R [user]:[group] [directory]

Recursively change the [user] and [group] ownership of [directory].

chmod [permissions] [file]

Change the permissions of [file]. The permissions are for the user, group and others, and the possible permissions are read (4), write (2), execute (1) or none(0). The numbers are the octal representation of these permissions. If you want a user to be able to read a setting.php file and deny all other permissions you can therefore use:

chmod 400 settings.php

Downloading files

wget [url]

Download [url]. For instance, you can download a robots.txt file to block naughty bots:

wget https://bitbucket.org/pajac2/robotctl/raw/master/block_list/robots.txt

wget -O [output-name] [url]

Download [url] and save it as [output name]. The -O option overwrites any existing file, which is useful if you want to, say, grab the latest robots.txt file:

wget -O robots.txt https://bitbucket.org/pajac2/robotctl/raw/master/block_list/robots.txt

Compressing and archives

gzip [file]

Compress [file]. The file is replaced, and the compressed file is named [file].gz.

gzip -k [file]

Compress [file] but keep the file that is compressed.

gzip -l [file.gz]

Print information about [file.gz], such a the compression ratio.

gzip -d [file.gz]

Decompress [file.gz]. You can also use gunzip [file.gz].

tar -czvf [name.tar.gz] [directory]

Archive and zip [directory] and give it the name [name.tar.gz]. The -v flag is optional; it prints verbose output to the screen, including all the files that are added to the archive.

tar -xvf [name.tar.gz]

Extract the [name.tar.gz] archive in the current working directory.

tar -xvf [name.tar.gz] -C [target_directory]

Extract the [name.tar.gz] archive to [target_directory].

Manuals and information about utilities

man [utility]

Display the manual for [utility].

type [utility]

Display the executable for [utility].

Shell history

history

Display the shell history (that is, commands that have been run). Each entry shows a number followed by the command that was executed.

![n]

Run command number [n].

!!

Run the last command in the history. If you ran a command that required sudo privileges you can repeat the command by using sudo !!.

history -d [n]

Delete entry number [n]. This is useful if you for instance included a password in a command (such as mysql command).