Linux names with spaces

How to Manipulate Filenames Having Spaces and Special Characters in Linux

We come across files and folders name very regularly. In most of the cases file/folder name are related to the content of the file/folder and starts with number and characters. Alpha-Numeric file name are pretty common and very widely used, but this is not the case when we have to deal with file/folder name that has special characters in them.

Note: We can have files of any type but for simplicity and easy implementation we will be dealing with Text file (.txt), throughout the article.

Example of most common file names are:

Example of numeric file names are:

Example of Alpha-Numeric file names are:

Examples of file names that has special character and is not very common:

One of the most obvious question here is – who on earth create/deal with files/folders name having a Hash (#) , a semi-colon (;) , a dash (-) or any other special character.

I Agree to you, that such file names are not common still your shell should not break/give up when you have to deal with any such file names. Also speaking technically every thing be it folder, driver or anything else is treated as file in Linux.

Dealing with file that has dash (-) in it’s name

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

Sample Output

The reason for above error, that shell interprets anything after a dash (-) , as option, and obviously there is no such option, hence is the error.

To resolve such error, we have to tell the Bash shell (yup this and most of the other examples in the article is for BASH) not to interpret anything after special character (here dash), as option.

There are two ways to resolve this error as:

You may verify the file thus created by both the above ways by running commands ls or ls -l for long listing.

To edit the above file you may do:

Note: You may replace nano with any other editor of your choice say vim as:

Similarly to move such file you have to do:

and to Delete this file, you have to do:

If you have lots of files in a folder the name of which contains dash, and you want to delete all of them at once, do as:

Important to Note:

1. The same rule as discussed above follows for any number of hypen in the name of the file and their occurrence. Viz., -a-b-c.txt, ab-c.txt, abc-.txt, etc.

2. The same rule as discussed above follows for the name of the folder having any number of hypen and their occurrence, except the fact that for deleting the folder you have to use ‘rm -rf‘ as:

Dealing with files having HASH (#) in the name

The symbol # has a very different meaning in BASH. Anything after a # is interpreted as comment and hence neglected by BASH.

Understand it using examples:

create a file #abc.txt.

Sample Output

The reason for above error, that Bash is interpreting #abc.txt a comment and hence ignoring. So the command touch has been passed without any file Operand, and hence is the error.

Читайте также:  Отключить загрузку приложений windows 10 что это

To resolve such error, you may ask BASH not to interpret # as comment.

and verify the file just created as:

Now create a file the name of which contains # anywhere except at the begging.

Run ‘ls -l‘ to verify it:

What happens when you create two files (say a and #bc) at once:

Verify the file just created:

Obvious from the above example it only created file ‘a‘ and file ‘#bc‘ has been ignored. To execute the above situation successfully we can do,

and verify it as:

You can move the file as:

You may edit it as using your choice of editor as:

And Delete it as:

To delete all the files that has hash (#) in the file name, you may use:

Dealing with files having semicolon (;) in its name

In case you are not aware, semicolon acts as a command separator in BASH and perhaps other shell as well. Semicolon lets you execute several command in one go and acts as separator. Have you ever deal with any file name having semicolon in it? If not here you will.

Create a file having semi-colon in it.

Sample Output

The reason for above error, that when you run the above command BASH interpret touch as a command but could not find any file operand before semicolon and hence it reports error. It also reports another error that ‘abc.txt‘ command not found, only because after semicolon BASH was expecting another command and ‘abc.txt‘, is not a command.

To resolve such error, tell BASH not to interpret semicolon as command separator, as:

Note: We have enclosed the file name with single quote » . It tells BASH that ; is a part of file name and not command separator.

Rest of the action (viz., copy, move, delete) on the file and folder having semicolon in its name can be carried out straight forward by enclosing the name in single quote.

Dealing with other special characters in file/folder name

Plus Sign (+) in file name

Don’t requires anything extra, just do it normal way, as simple file name as shown below.

Dollar sign ($) in file name

You have to enclose file name in single quote, as we did in the case of semicolon. Rest of the things are straight forward..

Percent (%) in file name

You don’t need to do anything differently, treat it as normal file.

Asterisk (*) in file name

Having Asterisk in file name don’t change anything and you can continue using it as normal file.

Note: When you have to delete a file that starts with * , Never use following commands to delete such files.

Exclamation mark (!) in file name

Just Enclose the file name in single quote and rest of the things are same.

At Sign (@) in file name

Nothing extra, treat a filename having At Sign as nonrmal file.

^ in file name

No extra attention required. Use a file having ^ in filename as normal file.

Ampersand (&) in file name

Filename should be enclosed in single quotes and you are ready to go.

Parentheses () in file name

If the file name has Parenthesis, you need to enclose filename with single quotes.

Braces <> in file name

No Extra Care needed. Just treat it as just another file.

Chevrons <> in file name

A file name having Chevrons must be enclosed in single quotes.

Square Brackets [ ] in file name

Treat file name having Square Brackets as normal files and you need not take extra care of it.

Under score (_) in file name

They are very common and don’t require anything extra. Just do what you would have done with a normal file.

Equal-to (=) in File name

Having an Equal-to sign do not change anything, you can use it as normal file.

Dealing with back slash (\)

Backslash tells shell to ignore the next character. You have to enclose file name in single quote, as we did in the case of semicolon. Rest of the things are straight forward.

The Special Case of Forward Slash

You cannot create a file the name of which includes a forward slash (/) , until your file system has bug. There is no way to escape a forward slash.

So if you can create a file such as ‘/12.txt’ or ‘b/c.txt’ then either your File System has bug or you have Unicode support, which lets you create a file with forward slash. In this case the forward slash is not a real forward slash but a Unicode character that looks alike a forward slash.

Question Mark (?) in file name

Again, an example where you don’t need to put any special attempt. A file name having Question mark can be treated in the most general way.

Читайте также:  Как активировать windows 10 по телефону 2020

Dot Mark (.) in file name

The files starting with dot (.) are very special in Linux and are called dot files. They are hidden files generally a configuration or system files. You have to use switch ‘-a‘ or ‘-A‘ with ls command to view such files.

Creating, editing, renaming and deleting of such files are straight forward.

Note: In Linux you may have as many dots (.) as you need in a file name. Unlike other system dots in file name don’t means to separate name and extension. You can create a file having multiple dots as:

and check it as:

Comma (,) in file name

You can have comma in a file name, as many as you want and you Don’t requires anything extra. Just do it normal way, as simple file name.

Colon (:) in File name

You can have colon in a file name, as many as you want and you Don’t requires anything extra. Just do it normal way, as simple file name.

Having Quotes (single and Double) in file name

To have quotes in file name, we have to use the rule of exchange. I.e, if you need to have single quote in file name, enclose the file name with double quotes and if you need to have double quote in file name, enclose it with single quote.

Tilde (

Some Editors in Linux like emacs create a backup file of the file being edited. The backup file has the name of the original file plus a tilde at the end of the file name. You can have a file that name of which includes tilde, at any location simply as:

White Space in file name

Create a file the name of which has space between character/word, say “hi my name is avishek.txt”.

It is not a good idea to have file name with spaces and if you have to distinct readable name, you should use, underscore or dash. However if you have to create such a file, you have to use backward slash which ignores the next character to it. To create above file we have to do it this way..

I have tried covering all the scenario you may come across. Most of the above implementation are explicitly for BASH Shell and may not work in other shell.

If you feel that I missed something (that is very common and human nature), you may include your suggestion in the comments below. Keep Connected, Keep Commenting. Stay Tuned and connected! Like and share us and help us get spread!

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

We are thankful for your never ending support.

Источник

How to Read a Filename with Spaces in Linux

It’s not very common in Linux to handle filename with spaces but sometimes files copied or mounted from windows would end up with spaces.

While it is not recommended to have file names with spaces, let discuss how to manage filename with spaces in a Linux system.

We will cover how to create, read and copy a file which has spaces in their filename.

1) Creating file names with spaces

To create files with spaces in file names, run the command as shown

For example, to create a file called ‘linoxide docs‘ use the syntax below

Output

If you want to view such a file with space in the file name, use the same principle of enclosing the file names inside the quotation marks.

2) Read a File with spaces in filename

You can use ‘cat’ command or open the document using your preferred text editor such as vim, nano or gedit.

Alternatively, you can use the syntax below

Let’s add some text to the ‘linoxide docs’ file

To view the file execute the command below

Output

3) Creating directory names with spaces

To create directory names with space in between use the syntax below

Please note the space after the backslash

For example, to create a directory called ‘linoxide files‘ run

Output

4) Navigating to a directory with spaces in the directory name

To navigate to a directory with spaces in its directory name, use the syntax below

Читайте также:  Служба подключения удаленного рабочего стола windows

To navigate to the directory ‘linoxide files’ execute the command below

5) Copying a directory with spaces in the directory name

To copy a directory with spaces in its directory name to a different location use the syntax below

For example to copy ‘linoxide files’ to /home/james path execute

Hope this article explained well on how to manage filename with spaces. Thanks for taking the time to read this article and please leave your comments.

Источник

Dealing With Spaces in File Names in Command-line (GNU/Linux)

Most people that use the GNU/Linux operating system do not like to deal with the command-line at all, though having a basic understanding of it (such as memorizing commands for mounting devices and copying files for instance) can come in real handy sometimes.

That said however, when dealing with files under command-line, say that you had to use it to backup your data because the desktop session was not working anymore, it is pretty common that one should come across files that contain spaces in their names. And unless you are aware of this simple trick, it can be quite frustrating, because for each space you have to add a backslash ( \ ) when using the command-line.

For example, let us assume that I have a file called Data backup 2.tar on my Home folder, and that I need to copy it to /media/pen location using the ‘cp’ command. If I enter the below command then I will receive an error saying that there exists no such file/directory.

sudo cp Data backup 2.tar /media/pen

Note: I am running ‘cp’ with ‘sudo’ (administrative) privileges.

In fact, if you carefully look at the output, you can see that command-line has treated the file name as three different files due to the three spaces that are present in the file name. So how can we overcome this ?

Well, you can add backslashes as mentioned before and below is the correct command using that.

sudo cp Data\ backup\ 2.tar /media/pen

But as you can see, not only it is time consuming, but since we are not that used to seeing backslashes on file names, it can be really confusing as well. So instead, you can put the file name that contains spaces, between two apostrophes. For this example, I will use the below command and the copying will be carried on without errors.

sudo cp ‘Data backup 2.tar’ /media/pen

As you see, once you add the first apostrophe, then you can type the file name freely, and when done entering the name, make sure to add another apostrophe (this is very important). That is all there is to it.

If a path contains two or more file names with spaces …

You can do the same. You can add apostrophes separately to each name, or you can put that particular path which contains names with spaces, between two apostrophes, as a whole, which is much easier. Coming back to the above example, let us say the Data backup 2.tar file is inside a folder called untitled folder that is located on my Home folder, then I will use the below command for copying it to the same destination.

sudo cp ‘untitled folder/Data backup 2.tar’ /media/pen

If both file paths (source and destination) contain such names (or even a mix — a file path that contains names with spaces and names without them), then you can add apostrophe pairs to each path, separately. Let us take the same example and say that this time we need to copy it over to /media/pen 2/new folder (as you can see this path contains two names with spaces in their names and one without).

For that I will use the below command.

sudo cp ‘ untitled folder/Data backup 2.tar ‘ ‘ /media/pen 2/new folder ‘

(Again, please remember that I have used ‘sudo’ with ‘cp’ because I am copying files to a location which requires administrative privileges. Here I have also colored the apostrophe pairs differently so it is easy to understand).

But the rule is, no matter what you do, you should treat the two paths (file source and the destination) individually. In other words, you cannot put the whole command (two file paths) inside two apostrophes, if you do so, then ‘cp’ will treat it as a the file source and will give an error saying the destination is missing.

sudo cp ‘untitled folder/Data backup 2.tar /media/pen 2/new folder’

To finish, not just with ‘cp’, you can use this trick with almost any command that requires you to deal with a file name that contain spaces under the command-line in GNU/Linux. Good luck.

Источник

Оцените статью