The command line interface (CLI) is an essential tool for any Linux user. Whether you are a seasoned developer or a Linux newbie, mastering the command line can help you work faster and more efficiently.
In this article, we will discuss ten common and useful tips when using the Linux command line, complete with examples to help you get started.
Autocompletion:
Tab completion is a feature that allows you to quickly complete filenames or commands by pressing the Tab key. For example, if you want to navigate to the Documents directory, you can simply type cd Doc and then press Tab.
The command line will automatically complete the directory name for you.
Skipping directly to the end or beginning of the line:
After typing a long command, if you would like the cursor to go at the beginning, use Ctrl-A. This will immediately skip the cursor to the beginning of the line. Similarly, use Ctrl-E to skip the cursor to the end of the line. This can be a timesaver when trying to edit typed commands before executing them.
Recall previous commands:
The history command allows you to view a list of the previous commands you have entered. This can be useful if you need to repeat a command or if you want to search for a command you previously entered.
For example, if you want to run the last command you entered, you can simply type !! and press Enter. Here is an in-depth guide to using this command.
Use the pipe operator
The pipe operator (|) allows you to pass the output of one command as the input to another command.
For example, if you want to search for a specific string in a file, you can use the grep command along with the pipe operator.
$ cat file.txt | grep "search string"
Redirection of output:
In Linux, the output of a command can be redirected to a file, a program or another command using the redirection operators.
The main operators for redirection are:
> : redirect output to a file and overwrite its contents if it already exists.
>> : redirect output to a file and append to its contents if it already exists.
Here are some examples of how to use redirection in Linux:
Redirect output to a file using >
ls -l > file.txt
This command will list the files and directories in the current directory and redirect the output to a file named file.txt.
Append or overwrite output to a file using >> or >
$ echo “Hello World” >> file.txt
$ echo “Hello World” > file.txt
Note: It is important to know that >> will append to the existing contents of the file while > will overwrite all the previous content.
Find files quickly:
The find command allows you to search for files and directories based on various criteria. For example, if you want to find all files with a .txt extension in the current directory and its subdirectories, you can use the following command:
$ find -name *.txt
Wildcards for patterns:
Wildcards allow you to specify patterns that match multiple filenames or directories. For example, if you want to list all files in the current directory with a .doc extension, you can use the following command:
$ ls *.doc
You can also use wildcards to remove all files with a specific extension. So, to delete all files that have a .doc extension, the command will be:
$ rm *.doc
The head command
The head command allows you to view the first few lines of a file. This can be useful if you want to monitor log files or other files that are constantly being updated. For example, if you want to view the first 10 lines of a file (which is the default view), you can use the following command:
$ head -n 10 file.txt
To view only the first line, use the -n parameter with it:
$ head -n 1 file.txt
The tail command
This is the inverse of the head command. The tail command allows you to view the last few lines of a file, with the last 10 lines being the default count. This can be useful if you want to monitor log files or other files that are constantly being updated.
For example, if you want to view the last 10 lines of a file, you can use the following command:
$ tail -n 10 file.txt
Just like the head command, you can use the -n parameter to change the number of lines to output.
Both these commands: head and tail are very useful when examining logs or files with a lot of content that keep updating.
Download files directly from the command-line:
The curl command allows you to transfer data to or from a server using various protocols, including HTTP, FTP, and SMTP. There is also the wget command that you can use for this.
For example, if you want to download a file from a web server, you can use the following command:
$ curl -O https://example.com/file.txt
Getting help from the command-line:
Finally, there are the man pages for these commands. When unfamiliar with a command, use the man command to know more.
man commandname
So, these are some of the common and useful tricks that can make working with the Linux command-line a lot easier.
Also, do check out this handy Linux commands cheat sheet for reference.