Americas

  • United States
sandra_henrystocker
Unix Dweeb

How to work on Linux with filenames that contain blanks

How-To
Sep 14, 20223 mins
Linux

Filenames that contain blanks can add complexity to the commands you use to work with them. Fortunately, there are several handy ways to make that easier.

filing cabinet files records stokkete shutterstock
Credit: Stokkete / Shutterstock

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

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.

Wrap-Up

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.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author