How to handle file names containing spaces and special characters in Linux
We often see the file name and folder name. Most of the time, the file/folder name and content are related and begin with a number and a letter. Letters and numbers are the most common and widely used file names. However, you must always process file names/folder names that contain special characters.
Note: We can have different types of files, but in order to make it simple and convenient, in this document we only use the example file (.txt) for demonstration.
Example of the most common file name:
abc.txt
avi.txt
debian.txt
...
Example of digital file name:
121.txt
3221.txt
674659.txt
...
Letter/number file name example:
eg84235.txt
3kf43nl2.txt
2323ddw.txt
...
Examples of file names containing special characters are not common:
#232.txt
#bkf.txt
#bjsd3469.txt
#121nkfd.txt
-2232.txt
-fbjdew.txt
-gi32kj.txt
--321.txt
--bk34.txt
...
An obvious problem is-who on this planet will create and process the inclusion of a well number?(#)
, Semicolon(;)
, Break number(-)
Or other special characters.
Like you think, this type of file name is really uncommon, but when you have to handle this type of file name, your shell should not go wrong or strike. Technically, everything in Linux, such as folders, drives, and others, is processed as files.
Process a file whose name contains a break (-)
Create with a broken mark(-)
For example, -abx.txt.
$ touch -abc.txt
Test output
touch: invalid option --'b'
Try'touch --help'for more information.
The cause of the above error is that the shell returns a broken mark(-)
The following content is regarded as a parameter, but it is obvious that this parameter is not used, so an error is returned.
To solve this problem, we need to tell Bash shell (yes, most of the examples below are based on the BASH environment) not to include special characters (here it is a break) the following characters are interpreted as parameters.
There are two ways to solve this error:
$ Touch ---abc.txt [method #1]
$ Touch./-abc.txt [method #2]
You can run the ls or ls-l command to list the details and check the files created in the preceding two methods.
$ ls -l
total 0
-rw-r--r--1 avi avi 0Jun811:05-abc.txt
To edit the preceding file, you can:
$ nano ---abc.txt
Or
$ nano ./-abc.txt
Note: You can replace nano with any other editor you like, such as vim:
$ vim ---abc.txt
Or
$ vim ./-abc.txt
If you just move the file, you can do this:
$ mv ---abc.txt -a.txt
Or
$ mv ---a.txt -abc.txt
To delete such an object, you can:
$ rm ---abc.txt
Or
$ rm ./-abc.txt
If a directory contains a large number of files whose names contain broken numbers, you can delete them all at a time as follows:
$ rm ./-*
Important:
The rules discussed above can also be applied to files with names containing arbitrary numbers and connection symbols at any location. In other words, -a-b-c.txt, ab-c.txt, abc-.txt, and so on.
The rules discussed above can also be applied to folders whose names contain any number of and connecting symbols at any location. In addition to one case, you must use this method when deleting a folder.rm -rf
:
$ Rm-rf ---abc or $ rm-rf./-abc
For more details, please continue to read the highlights on the next page: