Dealing with files whose filenames contain special shell characters can be tricky. Unless you know how to handle their filenames properly, you may not be able to perform even simple operations on such files. In this post I’ll show you how to deal with files starting with the minus (-) character, as well as with files that contain spaces. Let us consider a simple example.
I’ll start with a pdf file outline.pdf and rename it so that the resulting filenames start with special shell characters.
ls -la # ©2007 gnulinux.ro
produces the following output
total 2308 drwx------ 2 kamil kamil 4096 Nov 26 19:17 . drwx------ 3 kamil kamil 4096 Nov 26 19:13 .. -rw------- 1 kamil kamil 465135 Nov 26 19:14 ~ outline.pdf -rw------- 1 kamil kamil 465135 Nov 26 19:16 ~outline.pdf -rw------- 1 kamil kamil 465135 Nov 26 19:15 -outline.pdf -rw------- 1 kamil kamil 465135 Nov 26 19:15 .outline.pdf -rw------- 1 kamil kamil 465135 Nov 26 19:13 outline.pdf
As you can see number of special shell characters are used, such as the tilde character (~), which in shell points to the home directory of the current user, the minus character (-), which is used for escaping command options for shell programs as well as in IO redirection, and the dot character (.) that stands for the current directory. Now lets try to apply a file operation to all of these files in a typical way. More specifically, lets make these files readable by everyone using the following command.
chmod a+r *.pdf # ©2007 gnulinux.ro
The above command fails with the following error message.
chmod: invalid mode: `-outline.pdf' Try `chmod --help' for more information.
So, the chmod operation fails because the minus in the -outline.pdf filename is considered a mode switch, with a non-existent mode: outline.pdf. Out of the above files, -outline.pdf, is the only file that causes problems since running the following command works fine.
chmod a+r ~outline.pdf .outline.pdf outline.pdf ~\ outline.pdf # ©2007 gnulinux.ro
This is shown in the output below.
total 2308 drwx------ 2 kamil kamil 4096 Nov 26 19:17 . drwx------ 3 kamil kamil 4096 Nov 26 19:13 .. -rw-r--r-- 1 kamil kamil 465135 Nov 26 19:14 ~ outline.pdf -rw-r--r-- 1 kamil kamil 465135 Nov 26 19:16 ~outline.pdf -rw------- 1 kamil kamil 465135 Nov 26 19:15 -outline.pdf -rw-r--r-- 1 kamil kamil 465135 Nov 26 19:15 .outline.pdf -rw-r--r-- 1 kamil kamil 465135 Nov 26 19:13 outline.pdf
Note, that in the above command the space in the ~ outline.pdf filename was escaped using the backslash (\) character, otherwise the chmod a+r operation would be applied to the home directory (~) and the outline.pdf file, instead of the ~ outline.pdf file.
So how to get around the leading minus in filenames under Linux shell? Well it is simple, you just have to make sure that there are no leading minuses in the file names. How is that accomplished? By specifying filenames with leading paths. In our example with the current directory, hence the following command would make all the files readable for all the users.
chmod a+r ./*.pdf # ©2007 gnulinux.ro
In the above command, by pre-pending a ./ to the *.pdf wild card string we eliminate the problem previously caused by the minus. This is because the minus is no longer the leading character in any of the filenames. Instead, all of the files passed to the chmod a+r command begin with ./ string. To change permissions on the - outline.pdf file alone the following command can be used.
chmod a+r ./-outline.pdf # ©2007 gnulinux.ro
- What is your reaction?
- powered by Verysign
1 Distro Windowsfx 11 - Ubuntu + Microsoft Windows 11 interface + Linuxfx speed and ...
2 Opinion Four years a day with Linux Mint!
3 Tutorial GNU + Linux system cleanup script on Debian-based distributions
4 Tutorial Counter-strike installation on Linux with Wine
5 Opinion Advantages of Using a Linux File Server
6 Opinion Benefits of Linux Internet hosting
- powered by Verysign