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 the working directory to the parent 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 will make 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 will be permanently deleted – there is no wastebasket.

rm -i [file]

Prompt before removing a file.

rm *

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

rm -r [directory]

Remove [directory] and anything contained in it. Please tripple-check that the command – 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 will only be 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 actually 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 will be 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. 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 permission 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://gitlab.com/beepmode/robotctl/raw/master/robots_blacklist/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://gitlab.com/beepmode/robotctl/raw/master/robots_blacklist/robots.txt

Compressing and archives

gzip [file]

Compress [file]. The file will be replaced and the zip file will have the same name with a .gz extension.

gzip -k [file]

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

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 -czcf [name.tar.gz] –exclude=’./[exclude_directory]’ [directory]

Archive and compress [directory] and give it the name [name.tar.gz] but exclude the directory [exclude_directory] from 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 can be useful if you included a password in a command (such as mysql command).