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:
Print the name of the current directory (the “working directory”).
List all files in the current directory, apart from hidden files (“dot files”).
List all files, including hidden files.
Long listing (includes information about files and directories).
Long listing sorted list by date (newest file first).
Long listing sorted by size (largest file first).
Long listing sorted by size and in reverse order (smallest file first).
Long listing with files sizes in human-readable format (i.e. “1K” instead of “1024”).
Change the working directory to the home directory (
Change the working directory to the directory
Change to the parent directory.
Change the working directory to the the last working directory.
Create the file [file]. If the file already exists the timestamp will be updated.
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.
[destination]. For instance,
cp wp-config.php wp-config.php.bak makes a backup of the wp-config.php file.
Preserve file attributes such as the ownership and timestamps. Without this option attributes are overwritten.
Prompt before overwriting an existing file. By default
cp overwrites any existing destination file.
[file]. The file is permanently deleted – there is no wastebasket.
Prompt before removing a file.
Remove all files in the current directory. This does not remove directories.
[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.
[directory] and anything contained in it, and never prompt before removing a file.
[directory]. The directory is only removed if it is empty.
Create (make) the directory
Make the directory and any parent directories (if they don’t exist yet).
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.
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.
Print the first ten lines of
Print the first three lines of
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.
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.
[group] owner of
[file]. Note that the user and group are separated by a colon (
Recursively change the
[group] ownership of
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
[url]. For instance, you can download a robots.txt file to block naughty bots:
[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
[file]. The file is replaced, and the compressed file is named
[file] but keep the file that is compressed.
Print information about
[file.gz], such a the compression ratio.
[file.gz]. You can also use
Archive and zip
[directory] and give it the name
-v flag is optional; it prints verbose output to the screen, including all the files that are added to the archive.
[name.tar.gz] archive in the current working directory.
[name.tar.gz] archive to
Display the manual for
Display the executable for
Display the shell history (that is, commands that have been run). Each entry shows a number followed by the command that was executed.
Run command number
Run the last command in the history. If you ran a command that required sudo privileges you can repeat the command by using
Delete entry number
[n]. This is useful if you for instance included a password in a command (such as mysql command).