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

Why can't linux delete the file?

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains why linux can't delete files. Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn why linux can't delete files.

Preface

Redis 6.0has been released recently, and the latest stable version is 6.0.3 according to the official website. So, I'm going to install it on my own small server. As a result, there is a subsequent worry (files under linux cannot be deleted normally).

Download the latest version, after make in the source package, there are several errors, check the Internet, some people say that there is a problem with the latest version, it is recommended to use 5.0.8. I went to hell, and I believed it without thinking about it. (in fact, it may also have something to do with the fact that my system does not have tcl installed, which is what I will say later.)

Then I downloaded version 5.0.8 of redis and reinstalled it.

Find a problem

After downloading the 5.0.8 source package from the official website, I uploaded it to the server from my local (win system) via the scp command. As follows

Because the source package is downloaded to the desktop location, the address of the local path is written in the absolute path "C:\" and uploaded to the remote server's / root directory. Who would have thought that the file name on the server would look like this. As follows

The file name unexpectedly brought along the path of the local file. It's a headache, I thought. Just rename it.

I'm sure I'll copy and paste such a long file name. However, after pasting, it is found that the front is parsed as a path under linux, according to the path format of the folder under linux (the file delimiter is backslash "\" in windows and "/" in linux), as follows

Obviously, there is no path with a root path of "/ c" by default under linux. Therefore, an error was reported that the directory was not found.

This is very dan pain, renaming is not good, then try to delete it, the big deal I re-upload the file is.

However, the result was not as I expected. After the delete command was executed, the file was still there.

The reason, I guess, is that, like renaming, linux resolves the file name to a path. Then I saw on the Internet that quotation marks on the file name could avoid this situation, so I manipulated it and deliberately typed the name of the original file by hand. (originally backslash)

However, it is of no use. It is impossible to bypass it and parse the file name as a path.

Solve the problem

Later, it was the solution given to me by my little friend in the group, which was really tql. The record is as follows

Resolution Link: https://www.askcto.com/searchInfo?qId=7056cab522d84d0eb9c1a36eba66b324

The idea is to delete through the file Inode number, bypassing the file name. First, through ls-I, display the index node number of all files (below, the index node number of the target file is 477536), and then delete the file by the index node number.

File index node number

Then, some children's shoes will ask, what is the document index node number?

Before you do that, you need to know how files are stored in linux, as well as file index nodes.

Under linux, files are stored on disk in "blocks", which are the smallest storage units of files. Each block also contains multiple sectors, each with a size of 512 bytes. Usually eight sectors form a "block".

Since the data of the file is stored in "blocks", we also need to find an area to hold the meta-information of the file, including the permissions, owner, size, and location of the file. Such areas are called file index nodes (inode).

Meta-information can be understood as information used to save information related to a file. Meta-annotations in Java can be compared (meta-annotations are annotations that are used to label annotations).

Every inode has a number, which is called the file index node number. The operating system ultimately identifies different files by file index node numbers, not by file names.

As shown in the following figure, you can use the stat command to print out all the inode meta-information of a file (take TankServer.zip as an example), as well as the inode number.

The relationship between the file name and the file Inode number is somewhat similar to the mapping between the domain name and the IP.

For example, when we visit Baidu, we will only remember its domain name "www.baidu.com", not its IP address, and IP is not easy for us to remember. This is like, we programmers only need to remember the file name, but do not know its file index node number.

But in fact, in the end, it is necessary for the domain name parser DNS to resolve the domain name to a specific IP address (CDN is not considered here) before it can be recognized by other servers. It is as if the linux system will eventually identify files only by the file Inode number.

Having said that, we just want to know the file Inode number that currently needs to be deleted. Since it cannot be deleted through the file name, I can just cross you and find the corresponding file index node number (equivalent to, if I know the IP address of Baidu, I can access it directly through IP).

PS: for more information about inode, please refer to teacher Ruan Yifeng's blog: http://www.ruanyifeng.com/blog/2011/12/inode.html

Delete command

Then knowing the file index node number, we can delete its corresponding file in the following format.

Find. /-inum inode number-exec rm {}\

In the first half, the find command finds the file corresponding to the inode number in the current directory. The second half-exec indicates that the search is followed by the command to be executed. Rm is the command to delete the file. Which file is deleted? the function of {} is to pass the result of find as a parameter to-exec.

Finally, note that if you use the-exec parameter, you need to end with a semicolon, otherwise an error will be reported as shown below. The "\" backslash is to escape the semicolon.

Conclusion

These are the pits encountered when deleting files in linux. If you encounter a similar problem, or if there are special characters in a file name that make it impossible to delete the file, you can also try the above operation.

Warm Tip: when deleting files, you must be careful, don't delete the library and run away at last!

When I was testing just now, I added fewer horizontal bars in front of inum (correct should be-inum). Causes the entire command to delete other directory files, as shown in the following figure.

Luckily this is a directory file, and luckily I didn't use rm-rf to recursively delete the file, otherwise the folder would be useless. The following continue to execute, but also delete npm, God knows whether the system files will be deleted in the end.

Fortunately, my hands are fast, just in time for Ctrl + C (I have been single for more than 20 years).

At this point, I believe you have a deeper understanding of why linux can't delete files, so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Internet Technology

Wechat

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

12
Report