search
HomeSystem TutorialLINUXMultiple ways to delete special file names in Linux

Multiple ways to delete special file names in Linux

Feb 12, 2024 am 09:20 AM
linuxlinux tutoriallinux systemLinux operating systemlinux commandshell scriptembeddedlinuxGetting started with linuxlinux learning

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.

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
$

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
(省略部分内容)

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
$

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.

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

$ rm "\!*"
$

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 \!*
$

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
$

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参数

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
$

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!

Statement
This article is reproduced at:良许Linux教程网. If there is any infringement, please contact admin@php.cn delete
How does performance differ between Linux and Windows for various tasks?How does performance differ between Linux and Windows for various tasks?May 14, 2025 am 12:03 AM

Linux performs well in servers and development environments, while Windows performs better in desktop and gaming. 1) Linux's file system performs well when dealing with large numbers of small files. 2) Linux performs excellently in high concurrency and high throughput network scenarios. 3) Linux memory management has more advantages in server environments. 4) Linux is efficient when executing command line and script tasks, while Windows performs better on graphical interfaces and multimedia applications.

How to Create GUI Applications In Linux Using PyGObjectHow to Create GUI Applications In Linux Using PyGObjectMay 13, 2025 am 11:09 AM

Creating graphical user interface (GUI) applications is a fantastic way to bring your ideas to life and make your programs more user-friendly. PyGObject is a Python library that allows developers to create GUI applications on Linux desktops using the

How to Install LAMP Stack with PhpMyAdmin in Arch LinuxHow to Install LAMP Stack with PhpMyAdmin in Arch LinuxMay 13, 2025 am 11:01 AM

Arch Linux provides a flexible cutting-edge system environment and is a powerfully suited solution for developing web applications on small non-critical systems because is a completely open source and provides the latest up-to-date releases on kernel

How to Install LEMP (Nginx, PHP, MariaDB) on Arch LinuxHow to Install LEMP (Nginx, PHP, MariaDB) on Arch LinuxMay 13, 2025 am 10:43 AM

Due to its Rolling Release model which embraces cutting-edge software Arch Linux was not designed and developed to run as a server to provide reliable network services because it requires extra time for maintenance, constant upgrades, and sensible fi

12 Must-Have Linux Console [Terminal] File Managers12 Must-Have Linux Console [Terminal] File ManagersMay 13, 2025 am 10:14 AM

Linux console file managers can be very helpful in day-to-day tasks, when managing files on a local machine, or when connected to a remote one. The visual console representation of the directory helps us quickly perform file/folder operations and sav

qBittorrent: A Powerful Open-Source BitTorrent ClientqBittorrent: A Powerful Open-Source BitTorrent ClientMay 13, 2025 am 10:12 AM

qBittorrent is a popular open-source BitTorrent client that allows users to download and share files over the internet. The latest version, qBittorrent 5.0, was released recently and comes packed with new features and improvements. This article will

Setup Nginx Virtual Hosts, phpMyAdmin, and SSL on Arch LinuxSetup Nginx Virtual Hosts, phpMyAdmin, and SSL on Arch LinuxMay 13, 2025 am 10:03 AM

The previous Arch Linux LEMP article just covered basic stuff, from installing network services (Nginx, PHP, MySQL, and PhpMyAdmin) and configuring minimal security required for MySQL server and PhpMyadmin. This topic is strictly related to the forme

Zenity: Building GTK  Dialogs in Shell ScriptsZenity: Building GTK Dialogs in Shell ScriptsMay 13, 2025 am 09:38 AM

Zenity is a tool that allows you to create graphical dialog boxes in Linux using the command line. It uses GTK , a toolkit for creating graphical user interfaces (GUIs), making it easy to add visual elements to your scripts. Zenity can be extremely u

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool