- The IT complexity puzzle and how modernizing IT service management can help CIOs solve it and unlock growth
- First combined AI-RAN network from Nvidia and SoftBank supports inferencing, claims return of $5 for every $1 invested
- 웨이모, 엠마(EMMA) 논문 공개 "멀티모달 모델을 자율 주행 영역으로 확장"
- 네이버 밴드, 미국 월간 활성 사용자 600만 돌파 "3년 만에 2배 성장"
- 칼럼 | 적절한 의도와 잘못된 주체…오픈AI '심플QA'의 한계
How to work on Linux with filenames that contain blanks
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.
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.
Copyright © 2022 IDG Communications, Inc.