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. 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. Related content how-to How to find files on Linux There are many options you can use to find files on Linux, including searching by file name (or partial name), age, owner, group, size, type and inode number. By Sandra Henry Stocker Jun 24, 2024 8 mins Linux opinion Linux in your car: Red Hat’s milestone collaboration with exida With contributions from Red Hat and critical collaborators, the safety and security of automotive vehicles has reached a new level of reliability. By Sandra Henry Stocker Jun 17, 2024 5 mins Linux how-to How to print from the Linux command line: double-sided, landscape and more There's a lot more to printing from the Linux command line than the lp command. Check out some of the many available options. By Sandra Henry Stocker Jun 11, 2024 6 mins Linux how-to Converting between uppercase and lowercase on the Linux command line Converting text between uppercase and lowercase can be very tedious, especially when you want to avoid inadvertent misspellings. Fortunately, Linux provides a handful of commands that can make the job very easy. By Sandra Henry Stocker Jun 07, 2024 5 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe