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

How to deal with file names containing spaces and special characters under Linux

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly explains the "Linux how to deal with spaces and special characters of the file name", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Linux how to deal with spaces and special characters of the file name" bar!

We often see file names and folder names. Most of the time the name of the file / folder is related to the content and begins with a number and a letter. Alphanumeric file names are the most common and widely used, but you always need to deal with file names / folder names that contain special characters.

Note: we may have various types of files, but for simplicity and ease of implementation, we will only use text files (.txt) to demonstrate in this article.

The most common examples of file names are:

Abc.txtavi.txtdebian.txt...

Examples of numeric file names:

121.txt3221.txt674659.txt...

Example of alphanumeric file names:

Eg84235.txt3kf43nl2.txt2323ddw.txt...

Examples of file names that contain special characters are not common:

# 232.txtfubkf.txtfubjsd3469.txtMust121nkfd.txtMush2232.txtlyfbjdew.txtMushi gi32kj.txtMuffin 321.txtMube bk34.txt.

The obvious question is-who on this planet would create and process files / folders containing pound signs (#), semicolons (;), dashes (-), or other special characters?

As you might think, such filenames are not common, but your shell should not go wrong or go on strike when you have to deal with them. And technically, everything under Linux, such as folders, drives, or everything else, is treated as files.

Deal with files whose names contain dashes (-)

Create a file that starts with a dash (-), such as-abx.txt.

Touch-abc.txt test output touch: invalid option-- 'b'Try' touch-- help' for more information.

The reason for the above error is that shell takes the content after the dash (-) as a parameter, and obviously does not have such a parameter, so an error is reported.

To solve this problem, we have to tell Bash shell (yes, this and most of the examples later in this article are based on the BASH environment) not to interpret the characters after special characters (in this case, dashes) as parameters.

There are two ways to resolve this error:

$touch-abc.txt [method # 1] $touch. /-abc.txt [method # 2]

You can check the files created in both ways by running the command ls or ls-l to list the details.

$ls-l total 0Murray rwmurf Rafael-1 avi avi 0 Jun 8 11:05-abc.txt

To edit the above file, you can do this:

$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 simply move the file, you can do this:

$mv-abc.txt-a.txt or $mv-a.txt-abc.txt

To delete such a file, you can do this:

$rm-abc.txt or $rm. /-abc.txt

If there are a large number of files with such names containing dashes in a directory and want to delete them all at once, you can do this:

$rm. /-*

Important:

The rules discussed above can also be applied to files with any number of connection symbols in their names and in any location. That is to say,-a murb murc.txtrecoverabmurc.txtrecoverabcmer.txt, and so on.

The rules discussed above can also be applied to folders with any number of connection symbols in their names and anywhere, except in one case, you have to use rm-rf when deleting a folder:

$rm-rf-abc or $rm-rf. /-abc

Working with files whose names contain a pound sign (#)

The symbol # has a very special meaning in BASH. Everything after # will be considered a comment, so it will be ignored by BASH.

Use examples to deepen understanding:

Create a file named # abc.txt:

$touch # abc.txt test output touch: missing file operandTry 'touch-- help' for more information.

The reason for the above error is that BASH interpreted # abc.txt as a comment and ignored it. So the command touch did not receive any files as arguments, resulting in this error.

To solve this problem, you may need to tell BASH not to interpret # as a comment.

$touch. / # abc.txt or $touch'# abc.txt'

Check the file you just created:

$ls-l total 0Murray rwmurf Rafael-1 avi avi 0 Jun 8 12:14 # abc.txt

Now create a file that contains # in the name except at the beginning.

$touch. / a roombc.txt $touch. / abc#.txt or $touch 'aroombc.txt $touch 'abc#.txt'

Run'ls-l'to check:

$ls-l total 0Mustang rwmurf Rafael-1 avi avi 0 Jun 8 12:16 axibc.txtMurray RWKui Rafael-1 avi avi 0 Jun 8 12:16 abc#.txt

What happens if you create two files at the same time, such as an and # bc:

$touch a.txt # bc.txt

Check the file you just created:

$ls-l total 0Murray rwmurf Rafael-1 avi avi 0 Jun 8 12:18 a.txt

Obviously, in the above example, only the file an is created and the file # bc is ignored. We can do this for the above situation.

$touch a.txt. / # bc.txt or $touch a.txt'# bc.txt'

Check it out:

$ls-l total 0 avi avi Jun 8 12:20 # bc.txt-1 avi avi 0 Jun 8 12:20 a. Txt Mustang RW Mustang

You can move files like this:

$mv. / # bc.txt. / # cd.txt or $mv'# bc.txt''# cd.txt'

Copy like this:

$cp. / # cd.txt. / # de.txt or $cp'# cd.txt''# de.txt'

You can use your favorite editor to edit the file:

$vi. / # cd.txt or $vi'# cd.txt' $nano. / # cd.txt or $nano'# cd.txt'

Delete it like this:

$rm. / # bc.txt or $rm'# bc.txt'

To delete all files that begin with a pound sign (#), you can do this:

# rm. / # * handle files whose names contain semicolons (;)

If you don't already know, the semicolon acts as a command separation in BASH, and the same may be true of other shell. The semicolon as a delimiter allows you to execute several commands at a time. Have you ever come across a file whose name contains a semicolon? If not, here are examples.

Create a file whose name contains a semicolon.

$touch; abc.txt test output touch: missing file operandTry 'touch-- help' for more information.bash: abc.txt: command not found

The reason for the above error is that when running the above command, BASH interprets touch as a command but does not have any file parameters before the semicolon, so the error is reported. Then another error reported could not find the command abc.txt, simply because BASH expects another new command after the semicolon, and abc.txt is not a command.

To solve this problem, we have to tell BASH not to interpret semicolons as command delimiters, such as:

$touch. /'; abc.txt' or $touch'; abc.txt'

Note: we enclose the file name in single quotation marks. This tells BASH that the semicolon is part of the file name rather than the command delimiter.

Other operations on files and folders whose names contain semicolons (that is, copy, move, delete) can simply enclose the name in single quotation marks.

Processing files / folders whose names contain other special characters contain a plus sign (+)

You don't need any special treatment, just do it the usual way, such as the file name of the test below.

The $touch + 12.txt file name contains the dollar sign ($)

You need to enclose the file name in single quotation marks, as you would with a semicolon. And then it's easy.

The $touch'$12.txt' file name contains the percent sign (%)

There is no need for any special treatment, just as an ordinary file.

The $touch .txt file name contains an asterisk (*)

You need to enclose it in single quotes or use a backslash to escape. (LCTT translation note: the original text here is incorrect and has been modified. )

$touch * 12.txt

Note: when you need to delete files starting with an asterisk, do not use commands similar to the following.

$rm * or $rm-rf *

Instead, use this command, (LCTT translation note: here the original text is incorrect, has been modified)

The file name of $rm. /'* .txt 'contains an exclamation point (!)

As long as you enclose the file name in single quotation marks, everything else is the same.

The $touch'! 12.txt' file name contains the mouse (@)

There is nothing special. You can treat a file with a mouse name as an ordinary file.

The file name of $touch'@ 12.txt' contains ^

No special treatment is required. You can treat a file whose name contains ^ as a normal file.

The $touch ^ 12.txt file name contains (&)

Enclose the file name in single quotation marks, and then you can operate.

$touch'& 12.txt' file name contains parentheses ()

If the file name contains parentheses, you need to enclose the file name in single quotation marks.

The $touch'(12.txt) 'file name contains curly braces {}

Enclose in single quotation marks or escape using a backslash. (LCTT translation note: the original text here is incorrect and has been modified)

The file name of $touch'{12.txt} 'contains angle brackets

Files whose names contain angle brackets need to be enclosed in single quotation marks.

$touch''file name contains square brackets []

Enclose in single quotation marks or escape using a backslash. (LCTT translation note: the original text here is incorrect and has been modified)

The $touch'[12.txt] 'file name contains an underscore (_)

This is very common and does not require special treatment. Treat it as an ordinary document at will.

The $touch _ 12.txt file name contains the equal sign (=)

Enclose in single quotation marks or escape using a backslash. (LCTT translation note: the original text here is incorrect and has been modified)

$touch'= 12.txt' handles the backslash ()

The backslash tells shell to ignore the special meaning of the following characters. You must enclose the file name in single quotation marks, just as you do with semicolons. The rest is fine.

Special case where $touch'\ 12.txt' contains a slash

Unless there is something wrong with your file system, you cannot create a file whose name contains a slash. There is no escape slash.

So if you can create files like'/ 12.txt'or 'betc. Txt', either you have a problem with your filesystem or support Unicode, so you can create files that contain slashes. It's just that this is not a real slash, but a Unicode character that looks like a slash.

The file name contains a question mark.

Enclose in single quotation marks or escape using a backslash. (LCTT translation note: the original text here is incorrect and has been modified)

The file name of $touch'? 12.txt' contains a dot (.)

Click (.) in Linux. The opening file is very special and is called a dot file. They are usually hidden configuration files or system files. You need to use the'- a'or'- A 'switch of the ls command to view this file.

Creating, editing, renaming and deleting such files is straightforward.

$touch .12.txt

Note: in Linux you may encounter names that contain many dots (.) Donovan's file. Unlike other operating systems, dots in file names do not mean to separate names from extension suffixes. You can create a file with multiple dots in its name:

$touch 1.2.3.4.5.6.7.8.9.10.txt

Check it out:

$ls-l total 0Mel RWMUR Muffin-1 avi avi 0 Jun 8 14:32 the 1.2.3.4.5.6.7.8.9.10.txt filename contains a comma (,)

You can use commas in the file name, and you can have as many as you want without special treatment. Just like a normal name file.

$touch, 12.txt or $touch, 12jime.txt file name contains a colon (:)

Enclose in single quotation marks or escape using a backslash. (LCTT translation note: the original text here is incorrect and has been modified)

$touch': 12.txt' or $touch': 12pur.txt' filenames contain quotation marks (single and double quotation marks)

To use quotation marks in a file name, we need to use the alternation rule. For example, if you need to use single quotation marks in the file name, enclose the file name in double quotation marks. If you need to use double quotation marks in the file name, enclose the file name in single quotation marks. (LCTT translation note: or if single quotation marks and double quotes are mixed, you can also escape with backslash. )

The file names of $touch "15'.txt" and $touch'15 ".txt 'contain tilde (~)

Some text editors such as emacs under Linux create backup files when editing files. The name of this backup file is appended with a tilde after the original file name. You can use a tilde anywhere in the file name, for example:

$touch ~ 1a.txt or $touch 2b~.txt file name contains spaces

Create a file with spaces between the characters / words of the name, such as "hi my name is avishek.txt".

It is best not to use spaces in file names. If you have to separate readable names, you can use underscores or horizontal bars. However, if you still need to create such a file, you can escape the next character with a backslash. To create a file with the above name, you can do this.

$touch hi\ my\ name\ is\ avishek.txt hi my name is avishek.txt

I've tried to cover all the situations you might encounter. Most of the above tests are done in BASH Shell, which may vary under other shell.

Thank you for your reading, the above is the content of "how to deal with file names containing spaces and special characters under Linux". After the study of this article, I believe you have a deeper understanding of how to deal with file names containing spaces and special characters under Linux. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Servers

Wechat

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

12
Report