Anyone using the Linux command line met with lists of helpful tips. Everyone knows that everyday affairs can be performed more efficiently, but only this knowledge alone, not supported by practice, is of no use to anyone.
What do the typical work days of a system administrator sitting on Linux look like? If to abstract from everything, except the commands typed on the keyboard, it will appear that these
commands constantly repeat. Everything goes to the level of automatism. And, if even in the work there is something to improve, the habit resists the new. As a result, a lot of time is spent on doing things as usual, and not as quickly, and, after a short period of habituation, it is more convenient. To keep this in mind, to consciously introduce new useful trifles into one’s own practice means to grow and develop professionally, which means saving time, which can be a lot of things to spend.
Before you - a small list of useful techniques for working with the Linux command line. You may already be familiar with some of them, but have managed to forget them. And something may well 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 commands that you will use every day.
The output of the results of the teams in a table
Sometimes the output of command output looks like a jumble of messy lines. You can find what you need in such data, but it is inconvenient to work with them. For example - something similar can be obtained in response to the
mount
command. It would be nice to display the same thing in the form of a table. And this is not only possible, but also very simple:
')
mount | column –t
The results of the command mount, decorated in a tableBy default, the command generates a table view, focusing on the spaces between words. And what if other characters are used as delimiters, like colons? For example - in the output
cat /etc/passwd
?
You can also sort such data - it is enough to specify the separator character with the
–s
parameter. For example, below is the command for the ":" symbol.
cat /etc/passwd | column -t –s:
Formatted / etc / passwd outputRepeating a command multiple times before successful completion.
If you google on this topic, you will find that many are asking how to repeat a command until it is successfully completed. For example, this may be useful when pinging a server until it responds, when checking file uploads, or when polling a certain URL until it is available.
The
while true
cycle will help solve this problem. It looks like this:
The command will be repeated until successful completion.In this example, the
>/dev/null 2>&1
redirects the output of the program to
/dev/null
, including both Standard Error and Standard Out.
Most likely, this is one of the most useful techniques for working with the Linux command line.
Sorting processes by memory used
Everything is simple:
ps aux | sort -nk 4
Sorted process listSorting processes by using CPU resources
Sorting processes using CPU is done like this:
ps aux | sort -nk 3
Sorted process listTo get information about the architecture, use the
getconf LONG_BIT
.
Simultaneous viewing of several log files
The
tail
command is quite suitable for viewing log files, but sometimes you may need to work simultaneously with several similar files. To solve this problem, the
multitail
console tool is
multitail
, which supports text selection, filtering and many other useful features.
Work with multitailInstall this utility, if you do not find it, you can use the command
apt-get install multitail
.
Return to the previous directory
To return to the previous directory, just type
cd –.
Monitoring at regular intervals
Using the
watch
utility (for example,
watch df –h
) will help organize the monitoring of the output of any command. For example, you can monitor the amount of free space, and how it changes.
Perhaps you yourself will be able to find suitable scripts for using this command.
The continuation of the program after the session
When you run any program in the background and close the console, the program will also exit. What if it is necessary for the program to work even after the shell is closed?
In order to achieve this, you can use the command
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 windows of the terminal just for the sake of the commands executed in them.
Nohup commandIn the example shown in the figure above, the file
nohup.out
will be created in the current directory, containing the output of the command:
Nohup.out fileUseful thing, agree?
Auto answer yes or no
Suppose you want to automate a process that requires the user to constantly answer
yes
. This can be done using the
yes
command:
yes | apt-get update
You may instead decide to automate negative responses. This design will help:
yes no | command
Automate Answer YESCreating a file of a given size
You can create files of a given size using the
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 sizeExecution of the last command with root privileges
Sometimes you can forget to enter
sudo
before a command that needs root privileges. There is no need to re-enter everything - just use this command:
sudo !!
Execution of the last command with root privilegesCreating a terminal session protocol
In order to write to the file everything that was displayed in the terminal window, you can use the
script
command.
After exiting the session, the protocol will be recorded in the
typescript
file.
Replacing spaces with tabs
Here is the 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.
Replacing lowercase letters to uppercase
And here is an example of the above command for replacing lower case letters in a text file with upper case letters:
cat myfile | tr az AZ > output.txt
Replacing lowercase letters in the file to uppercaseAutomatic generation of argument list for commands: xargs
The
xargs
utility is probably worthy of the title of one of the most useful Linux command line features. It can be used to transfer the output of some command as an argument to another. For example, here’s how you can 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, perhaps, you have a file with a list of URLs, and you want to upload resources to these addresses, or somehow process them:
cat urls.txt | xargs wget
Here we must bear in mind that the output of the first command is passed as an argument at the end of the
xargs
command. If, when constructing the second command, you must explicitly specify the place where the output data of the first one should go, it is enough to use a pair of curly braces,
{}
and the
–i
parameter to replace the argument in the right place:
ls /etc
Xargs commandResults
Linux command line utilities are an incredibly extensive topic. Therefore, any list like ours can be replenished for a very, very long time. For example, many unexpected things are hidden in the
awk
and
sed
commands. Perhaps the most important thing is for such lists to be in business.
And what interesting techniques do you use on the command line?