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

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

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

Essentially, use the OLDPWD environment variable

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

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

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

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.

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

Or

To remove the test order itself. But we can also use the following command without grep -v

11. Use httpie instead of curl

httpie allows you to display more beautiful and colorful results, less boring as shown

Screen Shot 2016-07-01 at 10.29.08 AM

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

Screen Shot 2016-07-01 at 10.29.32 AM

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

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.

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

We can also not need to specify a group, but we will get the default group of that user with the command

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.

ITZone via Kipalog

Share the news now