Article count:1382 Read by:1966155

Account Entry

Check out some practical Linux tips

Latest update time:2024-08-15
    Reads:

1. Check the file checksum

When files are copied or transferred, they may be damaged or modified. In this case, you can check the checksum to confirm.

For example, in our daily work we need to use some docking programs provided by other groups. Every time the program does not run as expected, we will compare the md5 verification values ​​on both sides.

There are many ways to generate file checksums, commonly used ones include md5sum checksum, crc checksum, sum checksum, etc.

The commands are:

md5sum file_name
cksum file_name
sum 算法参数 file_name

For example:

Let's take a test.txt file as an example:

  • md5sum check
md5sum test.txt
  • crc check
cksum test.txt
  • Sum check

There are two algorithms for sum verification, which can be configured through parameters:

-r: indicates the use of the System V algorithm.

-s: indicates the use of BSD algorithm.

When we do not configure, the system v algorithm is used by default.

sum -r test.txt
sum -s test.txt

2. Find the file location

(1) locate

When searching for files, people are usually used to using find, but I think sometimes locate is faster, so I usually use locate first.

locate is different from find: find searches on the hard disk, while locate searches only in the /var/lib/slocate database. locate is faster than find, as it does not actually search, but checks the database.

Some systems may not come with locate and need to be installed by yourself. For example, you can install it in Ubuntu by entering the following command:

apt-get update
apt-get install mlocate

The locate command to find files is simple:

lcoate file_name

for example:

(2) find

The find command can be used to search by name, type, owner, size, etc.

Search file basic syntax:

find path -option file_name

For example, to search for the stdio.h file by name:

find / -name stdio.h

3. Command line editing skills

We accidentally entered something long in the terminal:

LinuxZn@LinuxZn:~$ dsfdsfdddddddddddddddddddddddddddddddddddfsgadgdsgasdgsdhfdkshfkjdshflksdhfkldshfkj

How can we delete it quickly? You can certainly achieve the goal by pressing the backspace key frantically. But there is a faster way:

Enter the shortcut key to delete all the content before the cursor. In addition, there are several practical and commonly used shortcut keys as follows: ctrl+u

  • ctrl+k: delete all contents after the cursor.

  • ctrl+a: Move the cursor to the beginning.

  • ctrl+e: Move the cursor to the end.

In addition, the command line has many practical and commonly used, as well as practical and uncommon shortcuts. Interested friends can learn them by themselves.

4. View the pid of a process

Order:

pidof process_name

like:

5. Check the running status of some processes

The top command can view some information about the process, but there are too many processes running on the system, which is not conducive to viewing the running status of some processes, such as:

At this time, we can view the running status of the specified process through the following command, for example:

Check the status of the kcalc process, command:

top -p `pidof kcalc`

This is much simpler.

Notice:

The "`" here is not a single quote!!!

The "`" here is not a single quote!!!

The "`" here is not a single quote!!!

This symbol is to the left of the exclamation point! key on your keyboard.

View multiple processes, such as:

top -p `pidof kcalc` -p `pidof test_x86`

6. Kill the process

(1) Using kill

First use pidof to view the pid of the process, and then use the kill command:

kill -9 process_pid

(2) Using killall

Use killall, command:

killall process_name

7. The log output by the terminal is saved to the file at the same time

Sometimes we need to save the real-time log information output by the terminal to a file. There are two ways to do this. These three methods have been written in previous articles, so I will briefly mention them here:

(1) tee

tee工具 It is used to redirect data to a file, and on the other hand, it can also provide a copy of the redirected data as stdin for subsequent commands. In short, it is . 把数据重定向到给定文件和屏幕上

Order:

executable_file | tee log_file

The demonstration is as follows:

(2) script

script工具 It is a very useful tool that can record the information output to the terminal. The usage steps are as follows:

  • Enter the command to start saving the information output by the terminal. log.txt is the log file to be written and can be named at will. script log.txt

  • Enter Exit to save. exit

The demonstration is as follows:

(3) Use some terminal tools that can save logs

Some terminal tools have logging capabilities, such as T : Terminator终端

8. View the dynamic libraries that the program depends on

Some programs depend on some dynamic libraries. You can use the ldd command to view the dynamic libraries that they depend on . Command:

ldd executable_file


9. View the ELF file header

There are several types of ELF files. You can check the previous article ? ELF file analysis for more information. A fresh graduate who just came here compiled a program. There was no error in the compilation, but it could not run. Then there was a problem running on the PC, and the error was as follows:

无法执行二进制文件: 可执行文件格式错误

The reason was that his project was set up to use a cross-compiler for compilation, but he ran it on a PC, so an error occurred.

We can use the ELF header of the executable file. The ELF header contains a lot of information, including the system architecture. The command is as follows:

readelf -h elf_file

In addition, you can also view some file information through the file command:

10. Text file viewing

We usually use the cat command to view text files, but in addition to the cat command, there are several other practical commands, which are introduced below:

(1) cat command

The cat command is often used to view files with less content. Many people call this command 小猫咪 the command, but cat is actually concatenate(连续) the abbreviation of , which means to display text content continuously. The command format is:

cat [参数选项] [文件]

like:

cat -n /etc/profile

Among them, adding the parameter can display the number of lines. More parameter options of cat can be viewed by entering . The detailed introduction of other commands listed below can also be viewed by entering . -n man cat man 命令

(2) tac command

The tac command is just the opposite of the cat command, it starts displaying from the end of the file.

(3) more command

The more command is suitable for viewing files with a lot of content, because it can display the percentage in real time to indicate how much content has been read.

The command format is:

more [参数选项] [文件]

like:

You can use or to scroll down to view the following content. 空格键 回车键

(4) less command

Less is also suitable for viewing files with a lot of content. Less is more flexible than more because less can page up and down. Press the key on the keyboard to page up, and press to page down. However, the less command does not display the current reading percentage in real time. PgUp PgDn

The command format is:

less [参数选项] [文件]

(5) head command

The head command is used to view the first n lines of a file. For example, use the command

head -n 20 /etc/profile

View the first 20 lines of the profile file in the /etc directory:

(6) tail command

The tail command is the opposite of the head command. The tail command is used to view the content of the last n lines of a file. For example:

(7) nl command

The nl command can display the content and line number at the same time, which cat -n is similar to the command:

11. Set LD_LIBRARY_PATH

LD_LIBRARY_PATH It is a predefined environment variable in Linux/Unix that sets the path that the linker should look at when linking dynamic libraries/shared libraries. Sometimes you need to add the current path to LD_LIBRARY_PATH, such as:

export LD_LIBRARY_PATH=./:$LD_LIBRARY_PATH

The above are some practical tips shared this time.

大家日常工作、学习过程中有用过哪些Linux实用的小技巧呢?

欢迎留言讨论

end



A bite of Linux


Follow and reply【 1024 】 to get a large amount of Linux information


Collection of wonderful articles

Recommended articles

【Album】 ARM
【Album】 Fan Q&A
【Album】 All original
Album Getting started with Linux
Special Computer Network
Album Linux Driver

Click " Read original text " to view more sharing, welcome Click to share, collect, like, and read


Latest articles about

 
EEWorld WeChat Subscription

 
EEWorld WeChat Service Number

 
AutoDevelopers

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building,Block B, 18 Zhongguancun Street, Haidian District,Beijing, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号