Some commands and operations are useful on the terminal
I recently presented on the topic Some simple operations and commands that are useful when using Linux for some friends. Although it is quite simple and familiar to those who make the system, I realize that not everyone knows. So I write down here, to hope it will be useful to someone and increase productivity when people work or feel less bored when using the terminal a lot.
1. View log realtime
Usually we use the tail -f /var/log/nginx/access.log command to see realtime log. But we may not need to use the -f option, but using the tailf /var/log/nginx/access.log command also has the same function.
Hold your hands a little more
2. View the directory tree structure with the tree command
The tree command allows us to see the structure and directory level of a directory, like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | ~ $ tree t t ├── a │ ├── x │ ├── y │ └── z ├── b │ ├── x │ ├── y │ └── z └── c ├── x ├── y └── z |
With t is your directory name
3. Create multiple folders at the same time
As above, we see that the parent directory is t , in which there are 3 subdirectories a , b and c in each subdirectory with x , y and z .
In order to be able to create one at a time, the above directory structure can be used
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | ~ $ mkdir -pv t / {a, b, c} / {x, y, z} mkdir: created directory 't' mkdir: created directory 't / a' mkdir: created directory 't / a / x' mkdir: created directory 't / a / y' mkdir: created directory 't / a / z' mkdir: created directory 't / b' mkdir: created directory 't / b / x' mkdir: created directory 't / b / y' mkdir: created directory 't / b / z' mkdir: created directory 't / c' mkdir: created directory 't / c / x' mkdir: created directory 't / c / y' mkdir: created directory 't / c / z |
Note that there is no space between commas in {} .
4. Return to the previous folder
For example, cd to a very long path, then cd again . If you return to the previous directory, you must type very long, you can use the command
1 | ~ $ cd - |
Essentially, use the OLDPWD environment variable
1 2 3 | ~ $ env | grep -i pwd PWD = / root OLDPWD = / var / log / nginx |
5. Copy public-key on the server automatically
We need to ssh on the server with the key. The usual way to do it is
1 2 3 4 5 | ~ $ mkdir ~ / .ssh ~ $ touch ~ / .ssh / authorized_keys ~ $ # copy public-key to authorized_keys ~ $ chmod 700 ~ / .ssh ~ $ chmod 600 ~ / .ssh / authorized_keys |
It is very annoying so we can use the ssh-copy-id user @ ip_address command , then enter the user password, public-key will be copied to the server itself. The .ssh directory and authorized_keys file will also be created to the right we need.
6. Periodically execute a command and export it to the screen
For example, I transfer a 10GB file to another server, I have a habit of checking where the size of that file on the other server is, so I have to repeat it 10sqm .
There is another way to automatically execute commands on every 2s (default) using the following command watch
1 | ~ $ watch 'ls -lh 10GB.txt' |
7. Use vim to a specified line
When configuring nginx, we have the nginx -t option so that check config has the correct syntax. For example, nginx reports the syntax error in line 19. We can open the config file directly to line 19 as follows
1 | ~ $ vim /etc/nginx/nginx.conf +19 |
8. View the latest log files
In the directory / var / log / nginx there are many files, sometimes several hundred files. One tip is to use the ls command with the following options to display the latest changed file at the bottom.
1 | ~ $ ls -larht |
Explain
- -l : to long list
- -a : to display hidden files
- -h : to display the size easy to read
- -t : to sort by modify time
- -r : to reverse, that means the file is last modified at the end
9. Working with compressed files
On the server there is usually a logrotate service for periodic log compression. If we want to investigate the log of the previous days, we will need to extract these log files before using grep , awk …
You can also use commands like zcat , zgrep , zless to read or filter these files without extracting them.
10. Check the process
We often check whether a process is run by the command
1 2 3 4 | ~ $ ps -ef | grep nginx root 944 1 0 Jun13? 00:00:00 nginx: master process / usr / sbin / nginx -c /etc/nginx/nginx.conf nginx 945 944 0 Jun13? 00:22:14 nginx: worker process nginx 946 944 0 Jun13? 00:20:37 nginx: worker proces |
Or
1 2 3 4 | ~ $ ps -ef | grep nginx | grep -v grep root 803 1 0 Jun14? 00:00:00 nginx: master process / usr / sbin / nginx -c /etc/nginx/nginx.conf nginx 806 803 0 Jun14? 00:00:20 nginx: worker process nginx 807 803 0 Jun14? 00:00:07 nginx: worker proces |
To remove the test order itself. But we can also use the following command without grep -v
1 | ~ $ ps -ef | grep [n] ginx |
11. Use httpie instead of curl
httpie allows you to display more beautiful and colorful results, less boring as shown
12. Use ncdu instead of travel
The ncdu command helps check the size of each folder and file in a certain directory. Also can be removed directly by key-binding
13. Handling json, csv files with jq and csvkit
I have written 2 articles about jq and csvkit . You can go there and read more about these 2 tools.
14. Some operations on the terminal
For example, we type a fairly long command as follows
1 | ~ $ grep '200' /var/log/nginx/access.log | awk '{print $ 1}' | sort | uniq -c | sort -sb |
We can use it
- Ctrl + w: to delete each word from the cursor position instead of using backspace to delete each character
- Ctrl + u: to delete from the cursor position to the beginning of the line
- Alt + f: to move to each word
- Alt + b: to move each word backward
- …
15. Some alias are useful
There are many repo dotfiles on github sharing on this issue. Personally, I use two aliases that I define as the p command to get public ip and r for random passwords.
1 2 3 4 5 6 7 8 9 10 | ~ $ alias p alias p = 'curl httpbin.org/ip' ~ $ p { "origin": "17.33.190.22" } ~ $ alias r alias r = 'openssl rand -base64 15' ~ $ r e1LhFM + Qo + Wm7OKivvZT |
In addition, I have a habit of typing mistakenly ll command into l or lll, so I also alias these 2 commands always
16. chown / chgrp
For example, I want to chown files belonging to user and group nginx. Normally we will do this
1 | ~ $ chown -R nginx: nginx / var / log / nginx |
We can also not need to specify a group, but we will get the default group of that user with the command
1 2 3 | ~ $ chown -R nginx: / var / log / nginx or ~ $ chown -R nginx. / var / log / nginx |
Use colons : and dots . right after the user
17. In summary
In general, there are many useful tips to help improve productivity when manipulating commands, but above are the things that you use every day, Hope it will be useful for someone. If everyone has good tips, please share more.