The most useful Linux command line tricks
- Transfer
Everyone who uses the Linux command line has come across lists of helpful hints. Everyone knows that everyday affairs can be done more efficiently, but this knowledge alone, not backed up by practice, is of no use to anyone.
What do the typical workdays of a system administrator who sits on Linux look like? If we ignore everything except the commands typed on the keyboard, it turns out that the commandsthese are constantly repeated. Everything goes to the level of automatism. And, even if there is something to improve, the habit resists the new. As a result, it takes a lot of time to do things the way they’re more familiar, rather than the quicker, and, after a short period of getting used to it, more convenient. Keeping this in mind, consciously introducing new useful little things into your own practice means professionally growing and developing, which means saving time, which can be spent on a lot of things.
Here is a short list of useful Linux command line tricks. You may already be familiar with some of them, but have already forgotten them. And something may well turn out to be a pleasant find even for connoisseurs. I would like to hope that some of them will be useful to you and will turn from a “list” into live teams that you will use every day.
Sometimes, outputting team results looks like a hash of messy lines. You can find what you need in such data, but working with them is inconvenient. For example - something like this can be obtained in response to a command
Results of the mount command, arranged in the form of a table
By default, the command forms a table view, focusing on spaces between words. But what if other characters, like colons, are used as delimiters? For example, in the conclusion
You can also arrange such data - just specify the separator character with the parameter
Formatted output / etc / passwd
If you google on this topic, you will find that many people ask the question of how to repeat the command until it is successfully completed. For example, this can be useful when pinging a server until it responds, when checking file uploads, or when polling a certain URL until it is available.
The cycle will help to solve a similar problem
The command will be repeated until its successful completion.
In this example, the design
Most likely, this is one of the most useful Linux command line tricks.
Everything is simple here:
Sorted Process List
Sorting processes by using the CPU is done like this:
Sorted process list
To display information about the architecture, use the command
The command is quite suitable for viewing log files
Working with multitail
You can install this utility if you can’t find it in your own team
To return to the previous directory, simply type
Using a utility
Perhaps you yourself can find suitable scripts for using this command.
When you launch any program in the background and close the console, the program will also exit. But what if it is necessary for the program to work even after closing the shell?
In order to achieve this, you can use the team
Perhaps this command is one of those that is most often forgotten, opening several terminal windows only for the sake of the commands executed in them.
Nohup command
In the example shown in the figure above, a file
File nohup.out A
useful thing, agree?
Suppose you want to automate a process that requires the user to constantly respond
Perhaps instead you decide to automate the negative responses. Here the following construction will help:
YES response automation
You can create files of a given size using the command
The above command will create a 10 MB file filled with zeros.
Creating a file of a given size
Sometimes you may forget to type
Running the last command with root privileges
In order to write to the file everything that was displayed in the terminal window, you can use the command
After exiting the session, the protocol will be written to a file
Here is a command that allows you to replace spaces with tabs:
In fact, it is universal and can work with any characters.
And here is an example of the above command for replacing lowercase letters in a text file with uppercase:
Uppercase lowercase letters in a file
Automatically generating a list of arguments for commands: xargs
The utility
Or, maybe you have a file with a list of URLs, and you want to load resources at these addresses, or somehow process them:
It should be borne in mind that the output of the first command is passed as an argument at the end of the command
Xargs team
Linux Command Line Utilities - The topic is incredibly extensive. Therefore, any list like ours can be replenished for a very, very long time. For example, much of the unexpected is hidden in the
And what interesting tricks do you use on the command line?
What do the typical workdays of a system administrator who sits on Linux look like? If we ignore everything except the commands typed on the keyboard, it turns out that the commandsthese are constantly repeated. Everything goes to the level of automatism. And, even if there is something to improve, the habit resists the new. As a result, it takes a lot of time to do things the way they’re more familiar, rather than the quicker, and, after a short period of getting used to it, more convenient. Keeping this in mind, consciously introducing new useful little things into your own practice means professionally growing and developing, which means saving time, which can be spent on a lot of things.
Here is a short list of useful Linux command line tricks. You may already be familiar with some of them, but have already forgotten them. And something may well turn out to be a pleasant find even for connoisseurs. I would like to hope that some of them will be useful to you and will turn from a “list” into live teams that you will use every day.
The output of the results of the commands in a table
Sometimes, outputting team results looks like a hash of messy lines. You can find what you need in such data, but working with them is inconvenient. For example - something like this can be obtained in response to a command
mount
. It would be nice to display the same in the form of a table. And this is not only possible, but also very simple:mount | column –t
Results of the mount command, arranged in the form of a table
By default, the command forms a table view, focusing on spaces between words. But what if other characters, like colons, are used as delimiters? For example, in the conclusion
cat /etc/passwd
? You can also arrange such data - just specify the separator character with the parameter
–s
. For example, below is the command for the ":" symbol.cat /etc/passwd | column -t –s:
Formatted output / etc / passwd
Repeated repetition of a command until its successful completion
If you google on this topic, you will find that many people ask the question of how to repeat the command until it is successfully completed. For example, this can be useful when pinging a server until it responds, when checking file uploads, or when polling a certain URL until it is available.
The cycle will help to solve a similar problem
while true
. It looks like this: The command will be repeated until its successful completion.
In this example, the design
>/dev/null 2>&1
redirects the program output to /dev/null
, including Standard Error, and Standard Out. Most likely, this is one of the most useful Linux command line tricks.
Sort processes by memory usage
Everything is simple here:
ps aux | sort -nk 4
Sorted Process List
Sorting processes by using CPU resources
Sorting processes by using the CPU is done like this:
ps aux | sort -nk 3
Sorted process list
To display information about the architecture, use the command
getconf LONG_BIT
.View multiple log files at once
The command is quite suitable for viewing log files
tail
, but sometimes it may be necessary to work with several similar files at the same time. To solve this problem, a console tool is suitable multitail
that supports text selection, filtering, and many other useful features. Working with multitail
You can install this utility if you can’t find it in your own team
apt-get install multitail
.Return to previous directory
To return to the previous directory, simply type
cd –.
Interval monitoring
Using a utility
watch
(for example watch df –h
) will help organize monitoring of the output of any command. For example, you can observe the amount of free space, and how it changes. Perhaps you yourself can find suitable scripts for using this command.
Continuing the program after the session
When you launch any program in the background and close the console, the program will also exit. But what if it is necessary for the program to work even after closing the shell?
In order to achieve this, you can use the team
nohup
, whose name stands for "no hang-up". It looks like this:nohup wget site.com/file.zip
Perhaps this command is one of those that is most often forgotten, opening several terminal windows only for the sake of the commands executed in them.
Nohup command
In the example shown in the figure above, a file
nohup.out
containing the output of the command will be created in the current directory : File nohup.out A
useful thing, agree?
Automatic answer yes or no
Suppose you want to automate a process that requires the user to constantly respond
yes
. You can do this using the command yes
:yes | apt-get update
Perhaps instead you decide to automate the negative responses. Here the following construction will help:
yes no | command
YES response automation
Creating a file of a given size
You can create files of a given size using the command
dd
:dd if=/dev/zero of=out.txt bs=1M count=10
The above command will create a 10 MB file filled with zeros.
Creating a file of a given size
Running the last command with root privileges
Sometimes you may forget to type
sudo
in before a command that needs root privileges. There is no need to re-enter everything - just use this command:sudo !!
Running the last command with root privileges
Creating a terminal session protocol
In order to write to the file everything that was displayed in the terminal window, you can use the command
script
. After exiting the session, the protocol will be written to a file
typescript
.Replacing spaces with tabs
Here is a command that allows you to replace spaces with tabs:
cat geeks.txt | tr ':[space]:' '\t' > out.txt
In fact, it is universal and can work with any characters.
Uppercase lowercase letters
And here is an example of the above command for replacing lowercase letters in a text file with uppercase:
cat myfile | tr a-z A-Z > output.txt
Uppercase lowercase letters in a file
Automatically generating a list of arguments for commands: xargs
The utility
xargs
is perhaps worthy of the title of one of the most useful features of the Linux command line. It can be used to pass the output of a command as an argument to another. For example, here's how to search for .png files and compress them, or do something else with them:find. -name *.png -type f -print | xargs tar -cvzf images.tar.gz
Or, maybe you have a file with a list of URLs, and you want to load resources at these addresses, or somehow process them:
cat urls.txt | xargs wget
It should be borne in mind that the output of the first command is passed as an argument at the end of the command
xargs
. If, when constructing the second command, it is necessary to explicitly indicate the place where the output of the first should go, just use a pair of curly brackets, {}
and a parameter –i
to replace the argument in the right place:ls /etc/*.conf | xargs -i cp {} /home/likegeeks/Desktop/out
Xargs team
Summary
Linux Command Line Utilities - The topic is incredibly extensive. Therefore, any list like ours can be replenished for a very, very long time. For example, much of the unexpected is hidden in the
awk
and commands sed
. Perhaps the main thing is that such lists go into business. And what interesting tricks do you use on the command line?