Home> System Tutorial> LINUX> body text

Multiple ways to delete special file names in Linux

WBOY
Release: 2024-02-12 09:20:22
forward
1001 people have browsed it

The Linux operating system provides powerful command line tools, such as the rm command, which can delete files and directories. However, some files are specially named and the ordinary rm command may not be able to delete them, which troubles many Linux users. In this article, we will introduce several ways to remove special file names so that you can easily deal with these annoying files.

Multiple ways to delete special file names in Linux

linux file naming rules

Before the introduction, let’s briefly explain the file naming rules in Linux. The file or directory name consists of any sequence of ASCII characters except "/" and the null character "\0". Of course many operating systems allow more types of characters to form file names. But for us, it is not recommended to use some special characters to name files. Unfortunately, we may have accidentally created some files with special names, or the program has accidentally created some files consisting of special characters. At this time, deleting them seems not as simple as imagined.

Put the path when deleting

Suppose there is a file named -static, we delete it using the normal method:

$ rm -static rm: invalid option -- 's' Try 'rm ./-static' to remove the file '-static'. Try 'rm --help' for more information.
Copy after login

It is a pity that this method prompts an error and cannot successfully delete the -static file. Why is this? We know that in Linux, it usually starts with -, followed by some characters, as a command option. Unfortunately, -static is considered a parameter option by the rm command. Unfortunately, in fact, There is no such option, so the final prompt is invalid option — 's', resulting in the -static file being unable to be deleted.

So is there any way to delete it? We noticed that in addition to prompting illegal options, we also tried rm ./-static. Is it possible? Let’s try:

$ rm ./-static $
Copy after login

Finally found that we successfully deleted the -static file. So we get our first method, which is to delete the file with the path.

Use —

when deleting

In addition to prompting us to use rm ./-static, we also asked us to use rm –help to get more information, so let’s take a look:

$ rm --help (省略部分内容) To remove a file whose name starts with a '-', for example '-foo', use one of these commands: rm -- -foo rm ./-foo (省略部分内容)
Copy after login

There is a lot of content, but you can notice the above content. For files starting with -, two deletion methods are provided. The second one is the one mentioned above, and the second one is before the deleted file. add-:

$ rm -- -static $
Copy after login

We found that this method can also delete files starting with -.

Use quotation marks when deleting

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

$ rm \!* rm: missing operand Try 'rm --help' for more information.
Copy after login

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

$ rm "\!*" $
Copy after login

This can be deleted, but this method cannot be used for all files of this type. For example, files named !* cannot be deleted in this way, because !* has other meanings, specifically Please refer to "Do you know the exclamation usage of "!" in Linux?"

Add escape characters to delete files

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

$ rm \!* $
Copy after login

This can be deleted. In Linux, many characters have special meanings, so when you need to use them as ordinary characters, you need to add the \ escape character in front, which is somewhat similar to the escape in C language. For another example, to delete abc files starting with spaces, you can use the following method:

$ rm \ abc $
Copy after login

will be deleted successfully.

Delete according to i node number

Many times, file names cannot be entered at all because they are a bunch of garbled characters. Therefore, it is completely impossible to delete it through the method introduced above. Is there any other way? Yes! Find the i node of the file and delete it according to the i node.

For example:

$ ls -i #找到乱码文件的i节点号 1703907 ç¼?ç¨?ç? ç??é«?æ¸?pdfç??.pdf $ find ./ -inum 1703907 -exec rm {} \; #或者使用-delete参数
Copy after login

For the usage of find command, please refer to "Advanced Usage of Find Command". This method of deleting by i-node number is applicable to any type of file mentioned above.

A few words to expand here. In the operating system, how does it identify a file? It does not identify different files through the file name, but through an iNode number, that is, the i node number. The file name is just one of the attributes of the file.

Use wildcards to delete

Not much explanation:

$ rm *.pdf $
Copy after login

However, this method may cause accidental deletion, because as long as the file name ends with .pdf, it will be deleted, so use it with caution according to the actual situation.

Summarize

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

  1. Bring the path
  2. when deleting
  3. Use —
  4. when deleting
  5. Use quotation marks when deleting
  6. Use escape characters to assist in removing
  7. Delete
  8. according to i node number
  9. Use wildcards to remove

Different types of files can be deleted using the more convenient methods mentioned above.

Removing special filenames in Linux can be a frustrating thing, but we've covered several ways to fix this problem in this article. You can choose one of the methods according to your needs to delete these problem files quickly and efficiently. Now you can use Linux with confidence and quickly resolve special filename issues when needed.

The above is the detailed content of Multiple ways to delete special file names in Linux. For more information, please follow other related articles on the PHP Chinese website!

source:lxlinux.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!