How To Work On Linux With Filenames That Contain Blanks| ItSoftNews

Personally, I always try to avoid filenames with blanks, usually by filling those places where less blank-phobic people would use them with underscores or hyphens. The filenames are still easy to decipher, and I don’t have to trouble myself with enclosing them in quotes when I want to use them. As a result, some of my files look like this:

locking-accts Lost_World 

I also rarely add .txt file extensions to the end of text files unless I plan to share them with my Windows system.

Use quotes

When blanks in file names are preferable for any reason, however, there are several easy ways to work with them. To reference existing files, you can enclose the filenames in single or double quotes. In fact, you can make this easier by starting with a quote mark, typing as much of the filename as needed to differentiate it from other files and then pressing the tab key to initiate filename completion. For example, typing the portion of a filename as shown in the example below and then pressing tab should add the rest of the filename to the “file n” beginning:

$ head -2 “file n<tab> 

You should then see the complete filename along with other files that match the pattern:

$ head -2 “file name with blanks” This is the content of the file. It should be what you expected to see or maybe not. 

Using single quotes works just as well as double quotes. Since the quotes are not part of the file names, it doesn’t matter which you use.

$ head -2 ‘file name with blanks’ 

Use backslashes

A third option to consider is using backslashes before each blank in the filename.

$ head -2 file name with blanks 

So to create a file with a filename that includes blanks, any one of these three options will work.

$ touch “new file #1” $ touch ‘new file #2’ $ touch new file #3 

The backslash method works because it stops the shell from seeing the blanks as filename terminators. If you omitted them in the third command above, you would end up with three new files rather than one.

Fill in with asterisks

Another very easy method is to specify only parts of the filenames and use asterisks to fill in the rest. If you’re working with a file named “file name with blanks”, you could use a command like this:

$ cat file*blanks 

The asterisk will match any string of characters.

A question mark included in commands will match any single character. So, you could also use a command like this to display a file with a blanks in its name:

$ cat file?name?with?blanks 

To list all files that have blanks in their names, you could run a command like this:

$ ls * * ‘a happy day’ ‘file name with blanks’ 

The asterisks match the beginnings and endings of the filenames and the blank following the backslash ensures the filenames contain at least one blank.

Enclosing a series of characters inside square brackets allows you to list files that contain any of the characters specified. The command below will list files that contain digits.

$ ls *[1234567890]* 

To use this technique with blanks, you need to use a backslash or the ls command will see two arguments instead of one.

$ ls *[1234567890 ]* 

The command above will list filenames containing blanks or digits.


Dealing with filenames that include blanks isn’t much or a problem, especially if you have several tricks at your disposal to work with them.

Leave a Reply

Your email address will not be published. Required fields are marked *