Linux.com

Home Linux Community Community Blogs General Linux Linux Shell Tip: Remove files with names that contains spaces, and special characters such as -, --

Linux Shell Tip: Remove files with names that contains spaces, and special characters such as -, --

In Linux or Unix-like system you may come across file names with special characters such as:

  • -
  • --
  • ;
  • &
  • $
  • ?
  • *
  • White spaces, backslashes and more.

In this quick tip I am going to show you to delete or copy files with names that contain strange characters on Linux.

Sample file list

Here is a sample list of file names:

file-1

The problem and solution

Your default bash shell considers many of these special characters (also known as meta-characters) as commands. If you try to delete or move/copy such files you may end up with errors. In this example, I am trying to delete a file named '>file':

$ rm >file

Sample outputs:

rm: missing operand
Try `rm --help' for more information.

The rm command failed to delete the file due to strange character in filename.

Tip #1: Put filenames in quotes

The following command is required to copy or delete files with spaces in their name, for example:

$ cp "my resume.doc" /secure/location/
$ rm "my resume.doc"

The quotes also prevent the many special characters interpreted by your shell, for example:

$ rm -v ">file"
removed `>file'

The double quotes preserve the value of all characters enclosed, except for the dollar sign, the backticks and the backslash. You can also try single quotes as follows:

$ rm -v 'a long file   name  here'
$ cp 'my mp3 file.mp3' /backup/disk/

Tip #2: Try a backslash

You can always insert a backslash (\) before the special character in your filename:

$ cp "my\ resume.doc" /secure/location/
$ rm "\*file"

Tip #3: Try a ./ at the beginning of the filename

The syntax is as follows to delete a file called '-file':

$ rm -v ./-file
removed `./-file'

The ./ at the beginning of the filename forces rm not to interpret - as option to the rm command.

Tip #4: Try a -- at the beginning of the filename

A -- signals the end of options and disables further option processing by shell. Any arguments after the -- are treated as filenames and arguments. An argument of - is equivalent to --. The syntax is:

$ rm -v -- -file
$ rm -v -- --file
$ rm -v -- "@#$%^&file"
$ rmdir -v -- "--dirnameHere"

Tip #5: Remove file by an inode number

The -i option to ls displays the index number (inode) of each file:

ls -li

Use find command as follows to delete the file if the file has inode number 4063242:

$ find . -inum 4063242 -delete

OR

$ find . -inum 4063242 -exec rm -i {} \;

Sample session:file-2

For more information and options about the find, rm, and bash command featured in this tip, type the following command at the Linux prompt, to read man pages:

$ man find
$ man rm
$ man bash
 

Comments

Subscribe to Comments Feed
  • Hari. Said:

    The one that worked for me is tip#4 on linux. >> Tip #4: Try a -- at the beginning of the filename

  • Pier Said:

    Hi, I have tried it all but it does not work on my Ububntu 14.10. here is what I get : $ rename -- "Pi?ces du dossier de consultation des entreprises\DC1.doc" DC1.doc Backslash found where operator expected at (eval 1) line 1, near "entreprises\" syntax error at (eval 1) line 1, near "entreprises\" Any idea how to solve this. I cannot open the file that I need to check badly.

  • Howard Lee Harkness Said:

    (Ubuntu 12.04 LTS Another method: I was able to remove a file with special characters (file name was q!?) by rm -v -- q* If there are other files you want to keep in the directory, and the pattern-matching isn't enough to single out only the offending files, you may need to move everything you want to keep somewhere else, then do a rm * or, from the parent directory rm -r

  • Howard Lee Harkness Said:

    (Ubuntu 12.04 LTS Another method: I was able to remove a file with special characters (file name was q!?) by rm -v -- q* If there are other files you want to keep in the directory, and the pattern-matching isn't enough to single out only the offending files, you may need to move everything you want to keep somewhere else, then do a rm * or, from the parent directory rm -r

  • Howard Lee Harkness Said:

    (Ubuntu 12.04 LTS Another method: I was able to remove a file with special characters (file name was q!?) by rm -v -- q* If there are other files you want to keep in the directory, and the pattern-matching isn't enough to single out only the offending files, you may need to move everything you want to keep somewhere else, then do a rm * or, from the parent directory rm -r

Upcoming Linux Foundation Courses

  1. LFD411 Embedded Linux Development
    25 Aug » 29 Aug - Santa Clara, CA
    Details
  2. LFS422 High Availability Linux Architecture
    08 Sep » 11 Sep - Raleigh, NC
    Details
  3. LFS426 Linux Performance Tuning
    08 Sep » 11 Sep - New York
    Details

View All Upcoming Courses


Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Join / Linux Training / Board