If you run a Linux distribution, and you are only using the GUI, you are missing out. The Linux terminal is an extremely powerful tool that goes well beyond the GUI. Writing commands might seem scary for a beginner, but you will soon get the hang of it. In this guide, you will find the most important Linux commands, to use the terminal like a pro.
Unblock any international website, browse anonymously, and download movies and Mp3 with complete safety with CyberGhost, just for $2.75 per month:
We have prepared this guide on Linux Mint 18.1 and Ubuntu 16.04 LTS.
The basic Linux commands, however, are universal, and they should work on any past and future distribution. They will even work with non-Linux operating systems based on Unix, such as FreeBSD, or the macOS / OS X terminal.
Table of Contents
- Important information about the Linux terminal
- Basic Linux commands for the terminal
- Are you looking for other Linux commands?
Important information about the Linux terminal
There is not a single Linux distribution, as far as we know, that doesn't have a terminal of some kind. On the contrary, there are a few distributions that don't have a GUI by default, and everything is done on the command line.
To open a terminal quickly from the GUI, the shortcut Ctrl+Alt+T will work on most distributions and desktop environments.
This is the basic anatomy of most Linux commands:
[sudo] command [optional switch] [file or directory path]
Using sudo will run any command with administrative rights. Most Linux commands that have to deal with system files and installation/uninstallation of programs demand sudo.
The linux commands are case-sensitive
It is important to remember that everything written in the terminal is case-sensitive. When the command is "sudo", neither "Sudo", "SUDO", nor "sUdO" will work.
Most Linux commands are lowercase, but there are capitalized switches, such as "chown -R".
The file and directory names are also case-sensitive. "File1" and "file1" are different files, even if they are in the same directory.
Beware of spaces
Spacing is equally important. "chown-R" will only return an error. If we want to create/access/delete a file or directory that has a space in the filename, we can either put the whole filename inside quotation marks...
...or "escape" the space using the backslash "\".
If we did neither, the particular mkdir command, which creates directories, would create two directories, "Folder" and "Name". Other Linux commands would just fail.
Finding previous Linux commands
Pressing the Up keyboard key will cycle through the last Linux commands we successfully used, in order. No failed commands will show here.
We can also use the history command to see all the Linux commands we have ever used on the terminal.
The invisible password
When we are asked for our password, e.g. after we used "sudo", as we type the password nothing will show on screen, no stars or dots or anything. We just type the password and press Enter.
If you want to change the default behavior and have stars appear while typing your password, check out our guide:
Use Tab to autocomplete
The Tab button on the keyboard is a huge time saver on Linux commands, as it will automatically fill in the names of files and directories.
If we want to delete a file named "whydidIgivethisfilesuchalongname", we just need to type "rm w" and pressing Tab will automatically complete the rest of the filename.
If there are more than files that begin with the same letters, e.g. "whydidIgivethisfilesuchalongname" and "whydidIeatsomuch", pressing Tab on "rm w" will autocomplete the common "whydidI".
We then need to type an additional character - "g" or "e", in the example - and press again Tab for the autocompletion to resume.
Copying and pasting Linux commands
To copy or paste on the terminal, Ctrl+C and Ctrl+V won't work.
Instead, we must use Ctrl+Shift+C and Ctrl+Shift+V. Or we can right click and use the commands from the context menu.
While using Linux commands, the characters "?" and "*" are wildcards.
"?" will replace any single character. So, if we have two files names test1file and test2file, we can delete them both with "rm test?file". But this won't delete test12file.
"*" will replace any string of characters. "rm test*file" will delete test1file, test12file, testBLABLABLAfile. It will also delete any other filename that begins with "test-" and ends in "-file", including testfile.
It is obvious that we must be extremely careful when combining deletion commands with wildcards. "rm *" will delete every file in the current directory, and it won't use Trash.
Extra information about Linux commands
We can learn more about any of the Linux commands with [command] --help and man [command].
[command] --help will show the usage of the command, and the available options and switches.
man [command] will show the command's manual, which is an extended version of the --help output.
However, not all Linux commands have a manual or a --help option.
Basic Linux commands for the terminal
In this list, we haven't included every possible command, just the Linux commands that would be more useful to a Linux beginner. In the future, we will create a separate guide with advanced Linux commands.
These Linux commands will help us navigate to particular directories and search for files.
- ls - list all the contents of the current working directory
Make sure you read ls --help because there are many options on how to show the directory contents.
- cd [directory] - change directory
- pwd - print the full path of the current working directory. It stands for "Print Working Directory"
- find [file or directory] - look for a particular file or directory inside the current working directory
We can use it with wildcards, such as find *.png
- locate [file or directory] - look for a specific file or directory everywhere on the filesystem, and return all paths that contain it.
We can use it with wildcards, such as locate *.png
Directory access shortcuts
When we want to navigate to a particular directory or access a specific file, it's handy to keep in mind the following shortcuts.
- "~" represents our personal /home directory, e.g. cd ~ and cd ~/Documents
- ".." represents the parent directory, i.e. the directory that contains the one we currently are. If we are at /home/test/public and type cd .. it will take us to /home/test/
- We can also use the full path to change to a particular directory, e.g. cd /home/test/public/. The Tab button can help us every step of the way to autocomplete the directories.
What to do with commands that return too many results
If we run "ls" on a directory with 1,000 files, or we use "locate *.png" on a disk with lots of png pictures, we will get too many results.
In this case, we can use a pipe with the vertical bar "|" symbol (accessible with "Shift + \" ) and more or less.
With locate *.png | more we will get the results page by page, and we can reveal the next pages by pressing space. We quit by pressing "q".
With locate *.png | less we will still get the first page of results, but navigate up and down with the arrow keys. Again, we quit with "q".
File and Directory Handling
This is how to create, delete, and search for files and directories.
- mkdir [directory name] - create a new directory
We can include the full path to the directory, to create it anywhere on the disk, e.g. mkdir ~/public/dir1
If we create a directory with sudo mkdir, we won't be able to create files or directories inside without administrative rights.
- touch [file name] - create an empty file
It is especially useful with the "echo" command, which you will find in the next section of the guide.
If the file already exists, touch won't erase its contents, it will just change the last access time to the current time.
- cp [file1] [file2] - copy file1 and create a new file named file2. The equivalent of Copy -> Paste.
To copy directories and their contents, we need to use cp -r [dir1] [dir2].
We can include the full paths for both files or directories to copy and paste from different parts of the disk, e.g. cp ~/file1 ~/public/file2.
- mv [/dir1/file1] [/dir2/file1] - move file1 from dir1 to dir2. The equivalent of Cut -> Paste.
mv will work the same for directories, without needing an extra switch.
The mv command is also the only way to rename a file/directory using Linux commands. We just type mv file1 file2 to rename file1 to file2.
- rm [file] - delete a file
To delete a directory and all its contents, we need to use rm -r [dir].
The rm command will immediately remove any files/directories, without using Trash. So, be careful, especially if you are using wildcards.
The sudo rm will remove system files and folders.
- ln -s [file] [link] - create a symbolic link for a file or folder
A symbolic link is a more powerful version of a shortcut we create on Windows. There are some differences that we will cover in a different guide.
- zip [archive.zip] [file] - creates a zipped folder that contains a compressed file.
Using wildcards, we can compress everything in a directory with zip archive.zip *.
- unzip [archive.zip] - unzips a compressed folder.
By default, unzip will extract all the files in the working directory.
- tar -cf [archive.tar] [file] - creates a compressed folder that contains a compressed file, using a different algorithm than zip.
Using wildcards, we can compress everything in a directory with tar -cf archive.tar *.
- tar -xf [archive.tar] - decompresses a .tar archive.
By default tar -xf will extract all the files in the working directory. With tar -zxf [archive.tar.gz] we can extract tar.gz archives
- gzip [file] - replaces the file with a .gz archive containing the file.
Both zip and tar retain the original file but gzip will only leave the .gz archive, and remove the original file.
- gzip -d [archive.gz] - decompresses a .gz archive.
By default gzip -d will extract all the files in the working directory.
- unrar e [archive.rar] - decompresses a .rar archive.
Most distributions don't have rar preinstalled to create a .rar archive, and can only install it as a trial version, which needs a paid registration. We will find unrar preinstalled on almost all distributions, however, to decompress .rar archives.
Creating and deleting files and directories is fine. But what if we want to access the file content at the Linux Terminal?
- echo "text" > [file.txt] - creates a file that includes whatever "text" we wrote with the command
Echo just prints the "text" on the screen. Using the redirection character ">" the result of echo is redirected to the file.txt
Caution! If the file.txt already exists, the echo "text" > file.txt will overwrite it, deleting all of its previous content.
- echo "text" >> [file.txt] - appends the "text" at the end of the file.txt content
This is a safer way of running echo. If the file.txt file exists, echo "text" >> file.txt will just add the "text" at the bottom of the file's content, without overwriting the previous content.
The > file.txt and >> file.txt parts of the command can be used with all Linux commands that print a result onscreen, such as ls, locate, etc. This will redirect or append the result to a file.
- cat [text file] - display all the contents of a text file at the terminal.
For large files, we can use cat [text file] | more and cat [text file] | less, the same way we pipe the output of ls or locate.
We can also use head [text file] to only show the first ten lines, and tail [text file] to display the last ten lines.
- grep "pattern" [file] - search for a particular text pattern inside a text document. If the pattern is found, the command prints the line containing the pattern on the terminal.
We can also search for a pattern in all the files in a directory, by using grep -r "pattern" [dir].
- nano [text file] - nano is a relatively user-friendly terminal text editor. We can also run nano with no parameters, to create a new text file.
Just remember that the commands on the bottom all work with Ctrl, e.g. we press Ctrl+X to exit, Ctrl+G to get help, etc.
- sort [text file] - will print the lines of text files sorted.
- wc [text file] - will do a word count for a text file, printing the numbers of newlines, words, and bytes, in this order.
Are you looking for other Linux commands?
If you want to know more about one of the Linux commands we mentioned or are curious about another Linux command, leave us a comment.
Do you want to support PCsteps, so we can post high quality articles throughout the week?
If you prefer your purchases from China, we are affiliated with the largest international e-shops: