Those of you who are familiar with Linux know that its efficiency is mainly reflected in the command line. Through the command line, many simple commands can be freely combined to obtain very powerful functions.

The command line is meant to be automated, and automation makes your work more efficient, frees up a lot of manual work, and frees up more time to do more meaningful things.

In this article, I’m going to share some very useful tips that I hope will help you increase your productivity and you’ll be able to use them as soon as you’re done!

1. Quick way to clear files

There are N ways to quickly clear a file. I like the following one because it’s the shortest

$ > access.log
Copy the code

Not enough? Well, I’ll also summarize some of the other most common ways to clear files

  • : > access.log
  • true > access.log
  • cat /dev/null > access.log
  • echo -n "" > access.log
  • echo > access.log
  • truncate -s 0 access.log

In the shell, : is a built-in command that stands for no-op, so: overwrites the file with empty content after executing a command that produces nothing.

2. Quickly generate large files

Sometimes, on Linux, you need a large file to test upload or download speed, and you can quickly generate a large file with the DD command

$ dd if=/dev/zero of=file.img bs=1M count=1024
Copy the code

This command generates a file named file.img with a size of 1 gb.

3. Safely erase disk data

This section describes an efficient and secure method for erasing hard disk data. This can be done easily with the dd command:

$ dd if=/dev/urandom of=/dev/sda
Copy the code

Generate random data with /dev/urandom, write the generated data to the SDA hard disk, equivalent to safely erasing the hard disk data.

In those days, if Chen teacher had learned this command, there may not be yan Zhao Gate incident.

4. Quickly create system disks

To create a system disk in Linux, the old tools are too weak, directly a command to do:

$ dd if=ubuntu-server-amd64.iso if=/dev/sdb
Copy the code

Ha ha, is not very cool, SDB can U disk, can also be ordinary hard disk

5. Check the running time of a process

It is possible that most students will only use ps aux. In fact, the -o parameter can be used to specify that only a specific field is displayed, which will get clearer results.

$ ps -p 10167 -o etimes,etime
ELAPSED     ELAPSED
1712055 19-19:34:15
Copy the code

Using etime to obtain the running time of the process, you can easily see that the process has been running for 19 days

Also, you can specify with -o that RSS can get only memory information for that process.

$ ps -p 10167 -o rss
  RSS
 2180
Copy the code

6. Dynamically view logs in real time

You can use the tail -f option to dynamically monitor log file changes, which is very useful

$ tail -f test.log
Copy the code

To stop tail monitoring immediately when Failed information is displayed in logs, run the following command:

$ tail -f test.log | sed '/Failed/ q'
Copy the code

7. Fast conversion of time stamps

Time manipulation is routine for programmers. Sometimes you want to be able to convert a timestamp to a datetime. On the Linux command line, this can be done quickly:

$ date -d@1234567890 +"%Y-%m-%d %H:%M:%S"
2009-02-14 07:31:30
Copy the code

Of course, you can also view the current timestamp on the command line

$ date +%s
1617514141
Copy the code

8. Elegant calculation of program runtime

On Linux, it is easy to get the running time of a program by using the time command:

$time./test real 0m1.003s user 0m0.000s sys 0m0.000sCopy the code

As you can see, the running time of the program is: 1.003s. If you are careful, you will notice that real is not equal to user + sys, and is far greater than user + sys.

Let’s explain what these three parameters mean:

  • real: represents the clock time, i.e. the time taken from execution to completion of the program;
  • user: indicates the time consumed by the CPU in user space during the runtime.
  • sys: indicates the amount of time the CPU consumes in kernel space during runtime.

Since user and SYS only count CPU consumption, the program may block with sleep and may wait for network or disk I/O, both of which consume a lot of time. So in a similar case, real is greater than the sum of the other two terms.

In addition, there are also scenarios where real is much less than user + sys. What the hell is that?

This is easier to understand. If the program is running parallel on multiple cpus, then user and SYS count multiple CPU times, and the real elapsed time is likely to be less than the sum of the other two

9. View the ASCII code on the CLI

We often need to look at ASCII codes during development, which can be easily viewed from the Linux command line rather than Google or Baidu

$ man ascii
Copy the code

10. Gracefully remove garbled files

On Linux, it is common to encounter files with garbled names. Want to delete it, but can not enter the name through the keyboard, sometimes copy and paste the garbled name, the terminal may not recognize, how to do?

Don’t worry, we’ll show you how elegantly find solves this problem.

$ls -i 138957 a.txt 138959 t.txt 132395 ڹ�� TXT $find. -inum 132395 -exec rm {} \;Copy the code

In the command, -inum specifies the inode number of the file, which is the unique number of each file in the system. Find finds the file using the inum number and deletes the file.

11. Obtain your public IP address on Linux

In an office or home environment, our virtual machine or server is usually configured with an internal IP address. How do we know what our public IP address is when we communicate with the external network?

This is very simple on Linux, one command

$ curl ip.sb
$ curl ifconfig.me
Copy the code

Either of the above commands will work

12. How to download web resources in bulk

Sometimes, colleagues share links to file downloads via a web page, which on Linux can be easily downloaded using the wget command without scripting or crawlers

$wget - r - nd - np - accept = # PDF http://fast.dpdk.org/doc/pdf-guides/ - accept: option specifies the resource type format PDFCopy the code

13. Skills of using historical commands

Share a few tips on how to use history commands to improve your productivity.

  • !!!!!: Repeat the previous command.
  • ! N: Repeat the NTH command in history. N can be viewed using the history command.
  • ! pw: Repeat the latest execution topwThe beginning of the history command, this is very useful, small series use very high frequency;
  • ! $: indicates the last parameter of the last command.

Guess most students have not used! $, here’s a quick example to give you a sense of its efficient use

$ vim /root/sniffer/src/main.c $ mv ! $! $.bak# is equivalent to
$ mv /root/sniffer/src/main.c /root/sniffer/src/main.c.bak
Copy the code

The current working directory is root, want to change main.c to main.c.bak. Normally you might have to type the long parameter main.c twice, or you might want to just copy and paste it.

And I pass by using! The $variable can be easily and elegantly renamed.

14. Quick search for history commands

On Linux, there are a lot of commands to type. How do we find and execute history commands quickly?

Use the up and down keys to view the history command. No, No, No. You can press Ctrl + R, then type the keyword of the command to search, and press Enter.

15. A real hacker can’t ignore skill

Finally, share one more tip that a real hacker can’t ignore. If we add a space before the command to be executed, the command will not be saved by history

In some cases, executing a command contains sensitive information, so this tip can be very useful and you won’t have to worry about forgetting to execute history -c. Recommended reading:

  • A letter for Linux beginners
  • The most detailed load balancing principle diagram in the whole network
  • Sed tutorial details, xiao Bai can understand
  • Linux three Musketeers grep tutorial details
  • Linux file search artifact find actual combat detailed solution, recommended collection!
  • Prerequisites For Linux network analysis: Tcpdump
  • Linux Three Musketeers awK combat tutorial
  • Taobao two face, the interviewer incredibly TCP three handshake asked so detailed

That’s all. Thank you for reading. I’m Chopin. Pay attention to my public number “programming accomplishment”, a large number of dry goods articles waiting for you!

Public number background reply “1024” have surprise!