📜 ⬆️ ⬇️

20 working methods on the Linux command line, which will save a lot of time

The topic of useful methods of working in the terminal Linux is inexhaustible. It would seem that everything is arranged very simply: the invitation of the shell, and the commands entered from the keyboard. However, in this simplicity lies the abyss of unobvious, but useful features. That is why we regularly publish materials on the specifics of working in the Linux command line. In particular, today it will be a translation of an article whose author is keen on saving time through increasing labor productivity.



If you are interested in working on the Linux command line, here are some of our materials on this topic:
')

Linux command-line techniques and time savings


Have you ever seen a colleague use some simple Linux commands to solve problems that take several lines of yours? It must have happened when this happened, all you could do was say to yourself: “Well, nothing for yourself! I did not know that this can be done so simply. "

In this article, I will show you some professional Linux command line techniques that will save you a lot of time and in some cases save you from a bad mood. Not only will your friends or colleagues say, “Well, wow!”, Looking at you. These techniques also help you increase your personal productivity, since you will need to enter fewer commands and even make fewer mouse clicks.

This is not to say that these Linux tips are for beginners only. It is possible that even experienced users will find here something useful, something they did not know despite the fact that they have used Linux for many years. Linux is learned from experience. Here will fit and their own experiences, and the experience of other people.

Before we begin, I would like to note that some of the methods presented here depend on the configuration of the command shell.

0. Command completion using the Tab key.


I'll start with something quite obvious, but very important: command completion with the Tab key.

When you start typing something in the Linux terminal, you can press the Tab key, after which a hint will be displayed containing a set of options to continue input, starting with the characters you just entered.

For example, my_best_file_1.txt you are going to copy a file called my_best_file_1.txt , you can simply type cp m and press Tab to see the possible options for continuing the command.


Using the Tab key to auto-complete input

The Tab key can also be used to autocomplete commands.

1. Go to the last working directory


Imagine that you worked in a directory that is not so easy to get to, and then moved to another directory located in a completely different place. Then you realized that you need to return to the previous directory. In this case, just use the following command:

 cd - 

It will allow you to be in the last working directory. Now in such situations you will no longer need to enter long paths, or use copy and paste.


Simple directory switching

2. Return to home directory


This technique is also very obvious. You can use the following command to return to your home directory from anywhere:

 cd ~ 

However, you can return to your home directory even faster - with the cd .

In most modern Linux distributions, the shell is preconfigured to precisely this perception of the cd . It will save you a couple of keystrokes on the keyboard.


The fastest way to go to your home directory

3. Displaying the contents of the directory


You may be wondering what the subtlety of using the command is to display the contents of a directory. Everyone knows that the ls -l for this purpose.

But that's the point. Almost everyone uses the ls -l to list the contents of directories, while the same can be done with the following command:

 ll 

The efficiency of this command depends on the Linux distribution and on the shell configuration, but it is very likely that you will be able to use it in most distributions.


Using the ll command instead of the ls -l command

4. Call several commands in one line


Imagine that you need to execute several commands in sequence. Probably, you enter one command, then wait, when it is completed, then - enter the next one?

In this situation, a command separator will be useful ; (semicolon). With this approach, you can enter several commands in one line. At the same time, unlike the usual input of commands, to execute the next command, it is not necessary to wait for the completion of the previous one.

 command_1; command_2; command_3 

5. Execution of several commands in one line and condition for successful completion of the previous command


We have just looked at how to call several commands in one line. It saves time. But what if you need, say, when calling two commands, so that the next command is executed only if the previous one completes without errors?

Imagine you want to build code, and then, if the build was successful, call make ?

In this situation, you can use the && separator. This delimiter ensures that the next command will be executed only if the previous one runs successfully.

 command_1 && command_2 

Here is a good example of using && :

 sudo apt update && sudo apt upgrade 

6. Simple search and use of previously entered commands.


Imagine that you, a couple of minutes or a couple of hours ago, entered a long command, and you need this command again. Moreover, the problem is that you can’t remember exactly this command.

In this situation, you will save the reverse search. This technique allows you to search in the history of teams by keyword. Here it is enough to use the key combination Ctrl + R to start a reverse search and enter something related to the command. The system will view the command history and show the commands that match the entered query.

 Ctrl + R search_term 

By default, only one result will be shown. In order to see more results matching the query, you will need to use the Ctrl + R key combination again and again. To exit the reverse search mode, press Ctrl + C


Reverse search in command history

Please note that in some Bash shells, you can use the Page Up and Page Down keys to search through search results.

7. Unlocking the terminal after accidentally pressing Ctrl + S


Perhaps you are used to using the Ctrl + S key combination to save files. But if you press these keys in a Linux terminal, you will block it.

If earlier you had to close and restart the terminal in order to correct the situation - now you can breathe easy, you will not have to do this anymore. In order to bring the terminal to working condition, simply use the key combination Ctrl + Q

8. Move to the beginning or end of the line.


Imagine that you enter a long command and somewhere in the middle you realize that you have to change something at the beginning of it. You probably use the arrow keys to move first to the beginning of the line, and then return to the end.

Of course, in this situation, you can use the Home and End keys, but, alternatively, you can go to the beginning of the line using the Ctrl + A key combination, and to the end using the Ctrl + E — key combination.


Move to the beginning or end of the line

This way seems to me more convenient than using the Home and End keys, especially when I work on a laptop.

9. Reading log files in real time


When you need to analyze the log files in which, during the analysis, some working application writes data, you can use the tail command with the -f option.

 tail -f path_to_Log 

In addition, you can use the grep in its usual form to display only those lines that interest you:

 tail -f path_to_log | grep search_term 

Also here you can use the option F This will cause the tail utility to continue to work even if the log file being viewed is deleted. With this approach, if this file is created again, tail will continue to output data from it to the screen.

10. Reading compressed log files without unpacking them.


Server logs are usually compressed with gzip to save disk space. This can lead to some inconvenience when analyzing logs by developers or system administrators. You may need to use the scp command to copy the file to your local directory, and then extract the file from the archive in order to view it, since you may not have permission to write to where the file of interest is stored.

Here we are assisted by a group of commands whose name begins with the letter “z”. These commands are an alternative to the usual commands used to work with log files. Among them are less , cat , grep , and others.

So, using the zless , zcat , zgrep and similar zgrep , you will not have to explicitly unpack compressed files. Details on these commands can be found here .

By the way, when I told a colleague about these “secret” teams, I got a cup of coffee.

11. Using the less command to read files


If you need to view a file, especially a large one, you can try the cat , but it’s much better to look for something else. The fact is that cat will display the entire file, which is not so convenient.

To view files, you can use editors like Vi or Vim, working in the terminal, but if you just need to read the file, the less command will be very useful.

 less path_to_file 

During a session with less you can search for the desired text fragments by keywords, navigate through the pages, display data with line numbers, and so on.

12. Reuse the last element from the previous command with! $


In many situations, it is very useful to use the argument of the previous command.

Suppose you need to create a directory, and then go into it. Here you can use the !$ Option by entering the transition command after the directory creation command.


Use! $ To use the last command argument.

Even better, the same thing is done with the Alt +. key combination Alt +. (point). The point can be used several times to look through the options of the last command.

13. Using the previous command in the current command with !!


With the help !! You can call all the previous command. This technique turns out to be especially useful when you need to execute a command and it turns out that you need superuser privileges to execute it. For example, the figure below shows the situation in which the sudo !! allows you to save a lot of time.


Take advantage !!! to substitute the last command as an argument

14. Using aliases to correct input errors


You may already be familiar with the alias command. It can be used to correct errors in the commands entered.

For example, it may happen that you often enter gerp instead of grep . If you can’t manage a bad habit, write an alias in your bashrc file as follows:

 alias gerp=grep 

Now you don’t have to re-type this command if you enter its name incorrectly.

15. Copying and pasting in a Linux terminal


This advice is somewhat ambiguous, since it depends on the Linux distribution and on the terminal.

But usually the copy and paste commands can be called as follows:


16. Shutting down a team or process


Perhaps, what I want to advise here is quite obvious, however, I will tell you about it anyway. If the command is executed in the foreground and you want to exit it, you can press Ctrl + C to stop it.

17. Using the yes command to automate interactive commands or scripts


If you have to work with some commands or scripts that involve user interaction, and you know that you will enter Y in response to each program question, you can automate this process with the yes command. This is done like this:

 yes | command_or_script 

18. Cleaning the contents of a file without deleting the file itself.


If you want to clear the contents of a text file without deleting the file itself, you can use the following command:

  > filename 

19. Clarification of the presence in the directory of files containing certain text


The Linux command line supports many ways to search for information. However, if you just need to find out if there are files in a directory that contain certain text, you can use this command:

 grep -Pri Search_Term path_to_directory 

However, I advise anyone who needs a search in Linux to figure out how to use the find .

20. Call for help for any team.


I want to finish this material with one very obvious, but, nevertheless, a very important "trick", which consists in invoking help for a command or command line tool.

Almost all command line tools contain reference information revealing the features of their use. Usually reading the help helps to understand the basics of the team. You can call help like this:

 command_tool --help 

Results: share useful tips!


I deliberately did not include teams like fuck in this material, since they are not one of those standard tools that can be found literally everywhere. What I have said here can be useful when working with almost all Linux distributions and command line shells without the need to install new programs.

Dear readers! You may already be familiar with some of the methods for using Linux commands listed here, or maybe you know them all. In any case, we invite you to share your favorite tricks in the comments.

Source: https://habr.com/ru/post/339820/


All Articles