In this article the latest content address: www.yanbinghu.com/2019/03/24/…
preface
There are a few Linux hacks that can make a big difference in your productivity. Here are some Linux hacks that can make a big difference in your productivity or that are short but effective.
Command editing and cursor movement
There are many shortcuts to help us correct our commands. Next, use cursor in place of cursor position.
Deletes command text from the beginning to the cursor
CTRL + U, for example:
cd/proc/tty; The ls - al the cursorCopy the code
If you use CTRL + U, the command will be cleared without holding backspace.
Deletes command text from cursor to end
CTRL + K, for example:
cdThe/proc/tty cursor; ls -alCopy the code
If you use CTRL + K, the command text from the beginning of the cursor to the end will be deleted.
There are other operations, such as:
- CTRL + A: Move the cursor to the beginning of the command
- CTRL + E: Move the cursor to the end of the command
- Alt F: Move the cursor forward a word
- Alt B: Move the cursor back one word
- CTRL W: Delete a word (whitespace separated string)
History commands are quickly executed
We all know that history records the execution of history commands, while using! + Indicates the number before a history command. You can run a history command quickly. See “Amazing Use of the word!” in Linux. Alternatively, you can use CTRL + R to search for executed commands.
Partial history command view
History shows a large number of history commands, while fs -l shows only part of them.
Viewing Logs in Real Time
$ tail -f filename.log
Copy the code
Tail -f Add the file name to display the log file content in real time. Of course, a similar effect can be achieved by using the less command to view the contents of the file and using the Shift + F key.
Source: public number [programming Abecas] website: www.yanbinghu.com
View disk or memory information
How do I know if the current disk is full?
$df -h /dev/sda14 4.6G 10M 4.4g 1% / TMP /dev/sda11 454M 366M 61M 86% /boot /dev/sda15 55G 18G 35G 35% /home/sda1 256M 31M 226M 12% /boot/efi tmpfs 786M 64K 786M 1% /run/user/1000Copy the code
You can run the df command to quickly check the disk usage of each mount path.
Space occupied by subdirectories of the current directory
The home directory occupies a large amount of space, and you would like to know the usage of each directory in the home directory:
$du -h -- -max-depth=1 /home(or-d 1)
18G /home/hyb
16K /home/lost+found
18G /home/
Copy the code
The directory depth is specified here, otherwise it will recursively calculate the size of subdirectories, so try it yourself.
Current memory usage
$free -h Total Used Free shared buff/ Cache available Mem: 7.7g 3.5g 452M 345M 3.7g 3.5g Swap: 7.6g 0B 7.6gCopy the code
With free results, it’s easy to see how much total memory is currently available, how much free memory is left, and so on.
Use the -h argument
I don’t know if you’ve noticed, but we used the -h argument in the previous commands to render the result in a human-readable way, so we see it rendered in units of G,M, etc. If you don’t use the -h argument, you can try it out for yourself.
Find the process ID by name
To quickly and directly find the process ID, use:
$ pgrep hello
22692
Copy the code
Or:
$ pidof hello
22692
Copy the code
Where hello is the process name.
Kill the process by name
You can use the kill -9 pid command to kill a process, but you need to find the process ID first. In fact, you can also kill a process by its name, for example:
$ killall hello
Copy the code
Or:
$ pkill hello
Copy the code
View the process running time
You can use the following command to check the running time of the process:
$ ps -p 24525 -o lstart,etime
STARTED ELAPSED
Sat Mar 23 20:52:08 2019 02:45
Copy the code
24525 is the id of the process you want to view.
Fast directory switching
- CD – Go back to previous directory
- The CD returns to the user’s home directory
Executing multiple Commands
We know that multiple commands can be executed using semicolons, such as:
$ cd /temp/log/; rm -rf *Copy the code
But if the current directory is/and the /temp/log directory does not exist, something exciting happens:
bash: cd: /temp/log: No such file or directory (suddenly silent)Copy the code
Because; You can run multiple commands, but the subsequent commands will not be executed because the previous command fails. Therefore, after the CD fails to be executed, the rm -rf * command is still executed. The result is predictable because the CD is in the/directory. So you still think this kind of accident happened without knowing the power of RF-RF *?
What if we solve it? Very simple, use &&, for example:
$ cd /temp/log/&&rm -rf *
Copy the code
This ensures that the first command is executed successfully before the next command is executed.
View compressed log files
Sometimes log files are compressed, so why not be lazy and not decompress them? B: Sure. Such as:
$ zcat test.gz
test log
Copy the code
Or:
$ zless test.gz
test log
Copy the code
Delete garbled files
Whether it is their own accidental creation or abnormal creation of the program, there will inevitably be some strange or garbled files, how to delete it? See Multiple Ways to Delete special Name files in Linux.
Clear file contents
Say you have a large file that you want to quickly delete, or don’t want to delete, but want to empty:
>filename
Copy the code
The log is simultaneously logged to a file and printed to the console
When executing shell scripts, logs are often redirected, but then the console does not print. How do you make it possible to log files and output logs to the console?
$ ./test.sh |tee test.log
Copy the code
Terminates and resumes process execution
We pause a process using CTRL + Z, or we can resume it using FG. For example, we use
$ cat filename
Copy the code
When we find that the contents of the file may be too much, we pause the program with CTRL + Z, and if we want to pick up where we left off, we can just use the FG command to resume execution. Or use bg to keep the process running in the background.
Calculate the program running time
We might be writing a small program and want to know how long it will run. In fact, we can use the time command to help us calculate, for example:
$time./fibo 30 The 30 result is 832040 real 0m0.088s user 0m0.084s sys 0m0.004sCopy the code
It shows the system time, the user time, and the total time actually used.
View the top 10 processes in memory usage
$ ps -aux|sort -k4nr |head -n 10
Copy the code
Here is a comprehensive use of ps, sort, head commands, can be respectively refer to the ps command description, “a command to help you sort the text” and “Linux common commands – text view article”.
Quickly find the commands you need
We all know that man can look at the help manual for commands, but what if we want a function but don’t know which command to use? Don’t worry, you can still use man:
$ man -k "copy files"
cp (1) - copy files and directories
cpio (1) - copy files to and from archives
git-checkout-index (1) - Copy files from the index to the working tree
gvfs-copy (1) - Copy files
gvfs-move (1) - Copy files
install (1) - copy files and set attributes
Copy the code
With the -k parameter, all the help manuals related to Copy Files are displayed.
Command line copy and paste
As we know, on the command line, copy can no longer be CTRL + C because it terminates the current process, and copy and paste on the command line requires the following shortcut keys:
- ctrl + c
- shift + insert
Search for files that contain a certain string
For example, to find a file containing the test string in the current directory:
$ grep -rn "test"
test2.txt:1:test
Copy the code
It can then find the line in which file the string is in.
The screen is frozen
When the program is running, the terminal may output a large number of logs. If you want to have a brief view but do not want to record the log file, you can use CTRL + S to freeze the screen so that the log output does not continue. If you want to restore, you can use CTRL + Q to exit the freeze.
Edit a text file without an editor
If you don’t have a basic VI editor on some systems, you can edit the content as follows:
$ cat >file.txt
some words
(ctrl+d)
Copy the code
After editing, CTRL + D can be saved.
Viewing ELF Files
View elf file header information
Such as:
$ readelf -h filename
Copy the code
In the display result, we can see the running platform, elf file type, size side, etc.
Check to see if the library contains an interface
$ nm filename |grep interface
Copy the code
Check whether the interface interface is included from the file filename if the file contains a symbol table. For more information, see Linux Common Commands – Development and Debugging.
Filtering logs
For example, if you want to view a log line that contains the test keyword:
$ sed -n "/test/p" logfile
Copy the code
This will display only logs that contain the test line.
conclusion
The content mentioned in this article suggests their own computer operation, experience the effect. This article summarizes some common Linux hacks. What other Linux hacks do you have? Welcome to share with us.
Wechat public account [Programming] : focus on but not limited to sharing computer programming basics, Linux, C language, C++, data structures and algorithms, tools, resources and other programming related [original] technical articles.