How to work on Linux with filenames that contain blanks
Personally, I always try to avoid filenames with spaces, usually using underscores or dashes in places where less space-phobic people would use them. The filenames are still easy to decipher, and I don’t have to bother with quoting them 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 at the end of text files unless I plan to share them with my Windows system.
Use quotation marks
However, if spaces in filenames are preferable for some reason, there are several easy ways to work with them. To refer to existing files, you can enclose the filenames in single or double quotes. You can even simplify this by starting with a double quote, typing as much of the filename as necessary to distinguish it from other files, and then pressing Tab to initiate filename completion. For example, if you type part of a filename, as shown in the following example, and then press Tab, the rest of the filename should be added to the beginning of “file n”:
$ head -2 “file n<tab>
You should then see the full 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 aren’t part of the filenames, it doesn’t matter which ones you use.
$ head -2 ‘file name with blanks’
Use backslashes
A third option to consider is using backslashes before each space in the filename.
$ head -2 file\ name\ with\ blanks
So to create a file with a filename that contains spaces, any of these three options will work.
$ touch “new file #1” $ touch ‘new file #2’ $ touch new\ file\ #3
The backslash method works because it prevents the shell from seeing the spaces as filename terminators. If you omitted them in the third command above, you would end up with three new files instead of one.
Fill in with asterisks
Another very simple method is to only give parts of the filenames and fill in the rest with asterisks. If you are working with a file named “filename with spaces”, you could use a command like this:
$ cat file*blanks
The asterisk matches any character string.
A question mark included in commands matches any single character. So you could also use a command like this to show a file with spaces in its name:
$ cat file?name?with?blanks
To list all files that have spaces in their names, you could run a command like this:
$ ls *\ * ‘a happy day’ ‘file name with blanks’
The asterisks match the beginning and ending of the filenames, and the space after the backslash ensures that the filenames contain at least one space.
By enclosing a series of characters in square brackets, you can list files that contain any of the specified characters. The following command lists files that contain digits.
$ ls *[1234567890]*
To use this technique with spaces, you must use a backslash or that ls The command sees two arguments instead of one.
$ ls *[1234567890\ ]*
The above command lists filenames that contain spaces or digits.
Wrap up
Dealing with filenames containing spaces isn’t much or a problem, especially when you have several tricks at your disposal to work with them.
Copyright © 2022 IDG Communications, Inc.