Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to find the same file under Linux

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today Xiaobian to share with you how to find the same file under Linux related knowledge points, detailed content, clear logic, I believe most people still know too much about this knowledge, so share this article for everyone to refer to, I hope you read this article after harvest, let's learn about it together.

So if you're running out of space, try deleting files like this to free up some space. Under Linux, we can identify identical files in the system by identifying their inode values.

An inode is a data structure that records all information about a file except the file name and file content. If two or more files have the same inode value, even if they have different file names and different locations, their contents, owners, and permissions are actually the same, and we can treat them as if they have the same file.

This type of document is actually called a "hard link." Hard links have the same inode value but different file names. Soft links are shortcuts that point to the target file but have their own inode value.

$ ls -l my*-rw-r--r-- 4 liangxu liangxu 228 Apr 12 19:37 myfilelrwxrwxrwx 1 liangxu liangxu 6 Apr 15 11:18 myref -> myfile-rw-r--r-- 4 liangxu liangxu 228 Apr 12 19:37 mytwin

We can't directly know which files in the same directory have the same inode value, but it's not hard to identify them. In fact, we can find these files directly by using ls -i command and sorting them by inode value.

$ ls -i | sort -n | more... 788000 myfile

In the first column of this result is the corresponding inode value. So from this result we can see at a glance which files have the same inode value.

If you just want to find a hard-linked file for a file, you can quickly find it by using the find command with the-samplefile option.

$ find . -samefile myfile./ myfile./ save/mycopy./ mytwin

These files all have the same inode value, if you don't believe it, you can use the ls command to see more information:

$ find . -samefile myfile -ls788000 4 -rw-r--r-- 4 liangxu liangxu 228 Apr 12 19:37 ./ myfile788000 4 -rw-r--r-- 4 liangxu liangxu 228 Apr 12 19:37 ./ save/mycopy788000 4 -rw-r--r-- 4 liangxu liangxu 228 Apr 12 19:37 ./ mytwin

We can see that, except for the file name, the information of these file names is exactly the same. Careful friends may notice that in column 2 (hard link number) there is 4, but in fact we found only 3 files, which means that there is another file sharing inode value with them, but we did not find it through this command.

As a lazy person, every time you type a command, you have to go directly to the script to find the same file under the directory!

#!/ bin/bash# seaches for files sharing inodesprev=""# list files by inodels -i | sort -n > /tmp/$0# search through file for duplicate inode #swhile read linedo inode=`echo $line | awk '{print $1}'` if [ "$inode" == "$prev" ]; then grep $inode /tmp/$0 fi prev=$inodedone $0# clean uprm /tmp/$0

Run Results:

$ ./ findHardLinks788000 myfile788000 mytwin

Of course, you can also use the find command to find all the same files in the system based on the inode value.

$ find / -inum 788000 -ls 2> /dev/null788000 4 -rw-r--r-- 4 liangxu liangxu 228 Apr 12 19:37 /tmp/mycopy788000 4 -rw-r--r-- 4 liangxu liangxu 228 Apr 12 19:37 /home/liangxu/myfile788000 4 -rw-r--r-- 4 liangxu liangxu 228 Apr 12 19:37 /home/liangxu/save/mycopy788000 4 -rw-r--r-- 4 liangxu liangxu 228 Apr 12 19:37 /home/liangxu/mytwin

In this command, we redirect the error prompt to a special file called/dev/null, so that we don't have a full screen of permission denied when searching for paths we don't have permission to access.

The above is "How to find the same file under Linux" all the content of this article, thank you for reading! I believe everyone has a great harvest after reading this article. Xiaobian will update different knowledge for everyone every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report