Beginner's guide to the Linux terminal. Everything you need to know in under 8 minutes of your time
Linux is an open-source operating system that powers many servers, supercomputers, and devices. Learning Linux can boost your career as a software developer, system administrator, cybersecurity professional, or IT support specialist. In this blog post, I will introduce one of the most useful skills you need to master in Linux: the command line.
What's the terminal?
The command line, also known as the terminal or the shell, is a text-based interface that allows you to interact with the operating system and run various commands. You can use the command line to perform tasks such as navigating the file system, managing files and directories, installing and updating software, monitoring system performance, troubleshooting problems, and automating tasks.
How do I use it?
To access the command line, you need to open a terminal emulator program, such as GNOME Terminal, Konsole, or xterm. You can find one in your applications menu. Once you have a terminal window open, you can type commands and press Enter to execute them.
You will see a prompt that indicates your username, hostname, and current directory. For example:
Basic navigation
One of the most common commands is ls
, which stands for list. This command shows you the files and folders in your current directory, which is the location where you are working. For example, if you type ls
in your home directory, you will see something like this:
Documents Downloads Music Pictures Videos
You can also use ls
with some options to change the output. For example, ls -l
shows more details about the files and folders, such as their size, permissions, owner, and modification date. ls -a
shows hidden files and folders, which start with a dot. ls -h
shows the sizes in human-readable format, such as KB or MB.
Another common command is cd
, which stands for change directory. This command allows you to move to a different directory. For example, if you want to go to your Documents folder, you can type cd Documents
and press Enter. You can also use cd ..
to go back to the previous directory, or cd ~
to go to your home directory.
You can also use pwd
, which stands for print working directory, to see the full path of your current directory. For example:
user@host:~$ pwd
/home/user/Documents
File management
To create a new file or folder, you can use the touch
and mkdir
commands, respectively. For example, if you want to create a new file called test.txt in your current directory, you can type touch test.txt
. If you want to create a new folder called test in your current directory, you can type mkdir test
.
To delete a file or folder, you can use the rm
and rmdir
commands, respectively. For example, if you want to delete the test.txt file that you created earlier, you can type rm test.txt
. If you want to delete the test folder that you created earlier, you can type rmdir test
. Be careful with these commands, as they do not ask for confirmation and they do not move the files or folders to the trash bin.
To copy or move a file or folder, you can use the cp
and mv
commands, respectively. For example, if you want to copy the test.txt file from your current directory to your Downloads folder, you can type cp test.txt ~/Downloads
. If you want to move it instead of copying it, you can type mv test.txt ~/Downloads
.
To rename a file or folder, you can also use the mv
command. For example, if you want to rename the test.txt file to new.txt, you can type mv test.txt new.txt
.
To edit a text file, you can use a text editor like nano or vim. For example, if you want to edit the test.txt file with nano, you can type nano test.txt
. This will open the file in a simple text editor that allows you to make changes and save them. To exit nano, press Ctrl+X and follow the instructions.
Basic commands
Including the aforementioned, some of the most common and useful commands you should know are:
pwd
: print the current working directorycd
: change the current working directorycp
: copy files and directoriesmv
: move or rename files and directoriesrm
: remove files and directoriesmkdir
: create directoriestouch
: create or update filescat
: display or concatenate filesecho
: display a message or a variablegrep
: search for a pattern in a file or inputfind
: search for files or directories that match certain criteriaps
: display information about processestop
: display dynamic information about system performancekill
: terminate a process by its process IDping
: test the connectivity to a host by sending packets and measuring the response timessh
: connect to a remote host using the Secure Shell protocolscp
: copy files securely between hosts using the Secure Shell protocolcurl
: transfer data from or to a server using various protocolstar
: create or extract compressed archive filesman
: display the manual page for a command or a topic
That's not all! There's a lot of commands. Check out a neat little overview that you can skim through right here!
Using the terminal to its fullest
Tab completion
You can use the Tab key to autocomplete commands, filenames, and directories. This can save you a lot of typing and avoid typos. You can also use the double tab to see all the possible options that match your input. For example, if you type ls /e
and press tab twice, you will see all the directories that start with /e
.
These are some of the basic commands and tips that will help you get started with the command line in Linux. There are many more commands and options that you can explore and learn by using the man
command, which shows the manual page for any command. For example, if you want to learn more about the ls command, you can type man ls
.
Reverse search
You can use the Ctrl+R key combination to search for a previous command that you have typed in the terminal. This can be useful if you want to repeat a long or complex command without typing it again.
You can type a part of the command and press Ctrl+R repeatedly to cycle through the matching commands in your history. To execute the command, press Enter. To edit the command, press Esc.
History
You can use the history
command to see a list of all the commands that you have typed in the terminal. You can also use the up and down arrow keys to navigate through the history.
You can use the !n
syntax to execute the command with the number n
in the history. For example, !10
will execute the 10th command in the history. You can also use the !!
syntax to execute the last command in the history.
Pipes and redirection
You can use the pipe symbol (|
) to pass the output of one command as the input of another command. This can allow you to chain multiple commands together and perform complex operations. For example, ls -l | grep txt
will list only the files that have the txt
extension.
You can also use the redirection symbols (>
and <
) to redirect the output or input of a command to a file. For example, echo "Hello World" > hello.txt
will create a file named hello.txt
and write Hello World
to it.
I've mentioned grep in this section. This tool is insanely useful for filtering all sorts of things. Read about it below!
The manual
You can learn more about each command by typing man
followed by the command name. For example, man ls
will show you the manual page for the ls
command, which explains its syntax, options, and examples.
If you're ever curious or don't know how to use a certain command, the manual page is a great resource for it - albeit I find a quick google search is usually faster at providing answers.
Conclusion
The command line is a powerful and versatile tool that can help you accomplish many tasks in Linux. By learning the basic commands and how to combine them, you can improve your productivity and efficiency. You can also explore more advanced features, such as pipes, redirection, variables, scripts, and aliases, to further enhance your command line skills.
I hope this blog post has given you an overview of the command line and why it is a useful skill to know in Linux. If you want to learn more, you can check out some of the online courses and tutorials available