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

What is the method of deleting special name files in Linux

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

Share

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

Today, I will talk to you about the deletion method of the special name file in Linux, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

As we all know, you can use the rm command to delete a file in Linux, but some files with special names cannot be deleted using the ordinary rm method. This article introduces several ways to delete files with special names in Linux.

Naming rules for Linux files

Before the introduction, let's briefly explain the file naming conventions in Linux. The file or directory name consists of any sequence of ASCII characters except "/" and the empty character "\ 0". Of course, many operating systems allow more types of characters to form file names. However, for us, it is not recommended to use some special characters to name files. Unfortunately, we may inadvertently create files with special names, or the program may accidentally create files made up of special characters, and it seems that deleting them is not as easy as we might think.

Delete with the path

Suppose you have a file called-static, which we delete in the normal way:

Rm-staticrm: invalid option-- 's'Try' rm. /-static' to remove the file'- static'.Try'rm-- help' for more information.

Unfortunately, this method prompts an error and does not successfully delete the-static file. Why is that? We know that in linux, it usually starts with -, followed by some characters, as an option for a command, and unfortunately,-static is considered by the rm command as a parameter option, and unfortunately, it does not actually have this option, so it eventually prompts invalid option -'s deletion, resulting in the-static file cannot be deleted.

So is there any way to delete it? We noticed that in addition to prompting illegal options, let's try rm. /-static, is that OK? Let's try this:

$rm. /-static$

It turns out that we successfully deleted the-static file. So the first way to get to us is to delete the file with the path.

Use-when deleting

In addition to prompting us to use rm. /-static, let's use rm-help to get more information, so let's take a look:

$rm-- help (omit part) To remove a file whose name starts with a'-', for example'--foo',use one of these commands: rm-foo rm. /-foo (omit part)

There are a lot of content, but you can note that for files that start with -, there are two ways to delete them, the second is mentioned earlier, and the second is to add-before the deleted file:

$rm-static$

We found that files that start with-can also be deleted in this way.

Use quotation marks when deleting

How to delete files composed of special characters, such as! *, etc. For example:

$rm\! * rm: missing operandTry'rm-help' for more information.

For this type of file, we need to enclose the file name in quotation marks:

$rm "\! *" $

This way it can be deleted, but this method does not apply to all files of that type. For example, a file named! * cannot be deleted in this way, because! * has other meanings.

Add escape characters to delete files

Since! * cannot be deleted by adding quotation marks, is there any other way? Yes! When deleting, add escape characters:

$rm\! * $

So that it can be deleted. In Linux, many characters have a special meaning, so when you need to use it as a normal character, you need to precede it with the\ escape character, which is similar to the escape in C language. For another example, to delete an abc file that begins with a space, you can use the following ways:

$rm\ abc$

It can be deleted successfully.

Delete according to I node number

In many cases, file names cannot be entered at all because they are a pile of garbled codes. Therefore, it is completely impossible to delete it through the method described above, is there any other way? Yes! Delete according to the I node by looking for the I node of the file.

For example:

$ls-I # find the I node number of the garbled file 1703907?. Pdf $find. /-inum 1703907-exec rm {}\; # or use the-delete parameter

This deletion through the I-node number applies to any of the previously mentioned types of files.

Here is an extension of a few words. How does it recognize a file in the operating system? It does not identify different files by file name, but by an iNode number, that is, I node number, and the file name is just one of the attributes of the file.

Delete using wildcard characters

Not much explanation:

$rm * .pdf$

However, this method may be mistakenly deleted, because as long as the file name ends with .pdf, it will be deleted, so it is used cautiously according to the actual situation.

There are many ways to delete special file names, which can be summarized as follows:

Delete with path deletion use-delete with quotation marks to use escape characters to assist deletion delete according to I node number delete with wildcard

What is Linux system Linux is a free-to-use and free-spread UNIX-like operating system, is a POSIX-based multi-user, multi-task, multi-threaded and multi-CPU operating system, using Linux can run major Unix tools, applications and network protocols.

After reading the above, do you have any further understanding of the method of deleting special name files in Linux? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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