Deleting files with spaces in their names

54,717

Solution 1

You can use standard globbing on the rm command:

rm -- *\ *

This will delete any file whose name contains a space; the space is escaped so the shell doesn't interpret it as a separator. Adding -- will avoid problems with filenames starting with dashes (they won’t be interpreted as arguments by rm).

If you want to confirm each file before it’s deleted, add the -i option:

rm -i -- *\ *

Solution 2

I would avoid parsing ls output

Why not :

find . -type f -name '* *' -delete

No problem with rm :-).

Although this is recursive and will delete all files with space in current directory and nested directories, as mentionned in comments.

Solution 3

Look at this Suppose name "strange file"

Solution one

rm strange\ file

solution two

rm "strange file"

solution three

ls -i "strange file"

you see the inode then

find . -inum "numberoofinode" -exec rm {} \;

In case of very strange file names like

!-filename or --filename

use

rm ./'!-filename'

Solution 4

From man xargs

xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is /bin/echo) one or more times with any initial-arguments followed by items read from standard input. Blank lines on the standard input are ignored.

We can (mostly) fix your initial command by changing the xargs delimiter to a newline:

ls | egrep '. ' | xargs -d '\n' rm (don't do this... read on)

But what if the filename contains a newline?

touch "filename with blanks and newline"

Because Unix filenames can contain blanks and newlines, this default behaviour is often problematic; filenames containing blanks and/or newlines are incorrectly processed by xargs. In these situations it is better to use the -0 option, which prevents such problems.

ls is really a tool for direct consumption by a human, instead we need to use the find command which can separate the filenames with a null character (-print0). We also need to tell grep to use null characters to separate the input (-z) and output (-Z). Finally, we tell xargs to also use null characters (-0)

find . -type f -print0 | egrep '. ' -z -Z | xargs -0 rm

Solution 5

You can use:

find . -name '* *' -delete
Share:
54,717

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin over 1 year

    I am trying to delete all the files with a space in their names. I am using following command. But it is giving me an error

    Command : ls | egrep '. ' | xargs rm

    Here if I am using only ls | egrep '. ' command it is giving me all the file name with spaces in the filenames. But when I am trying to pass the output to rm, all the spaces (leading or trailing) gets deleted. So my command is not getting properly executed.

    Any pointers on how to delete the file having atleast one space in their name?

  • Scorion.Poison
    Scorion.Poison almost 9 years
    You will DEFINITELY want to run this through an echo first, to guard from typos. Add echo at the front and it will print out all the files it's going to remove.
  • Stephen Kitt
    Stephen Kitt almost 9 years
    That will delete all the files with spaces in their names in the current directory and all sub-directories, with no more warning than rm...
  • Kevin
    Kevin almost 9 years
    (1) you can use -name '* *' instead of the regex; and (2) you can use -print0 | xargs -0 rm -i to address @StephenKitt's concern.
  • Fahad Parvez Mahdi
    Fahad Parvez Mahdi almost 9 years
    rm -- *\ * seems better
  • G-Man Says 'Reinstate Monica'
    G-Man Says 'Reinstate Monica' almost 9 years
    Also, this was covered by Kevin's comment on solsTiCe's answer.
  • R.. GitHub STOP HELPING ICE
    R.. GitHub STOP HELPING ICE almost 9 years
    No need for xargs. Just use -exec rm -i '{}' +
  • Mohammad
    Mohammad almost 9 years
    Why avoid parsing ls, is there a specific reason or is it just a matter of preference?
  • Daenyth
    Daenyth almost 9 years
    @Mhmd It's got more pitfalls than an 8-bit gaming console. mywiki.wooledge.org/ParsingLs
  • Mike S
    Mike S almost 9 years
    Anuj, the reason why this has the most upvotes is that because though find is powerful, sometimes you don't need to kill the chicken with a machine gun. UNIX administrators would generally not resort to find to (for example) "remove all files beginning with the letter A"... one would simply rm A*. Likewise to remove files containing spaces, rm can do the job. In other words, don't be fooled because space is invisible and is treated specially by the shell. Simply escape it, as Stephen Kitt has done, and you can think of it like any other character.
  • Khang Huynh
    Khang Huynh almost 9 years
    If you want to avoid subdirectories, use find . -maxdepth 1 -name '* *' -delete.
  • Kusalananda
    Kusalananda over 5 years
    -regex matches against the full path. If there is a directory with a space in its name in the path, all its subdirectories and files would be deleted.
  • Benjamin W.
    Benjamin W. over 5 years
    @Kusalananda All the files, but not the directories, no? -type f would skip those.
  • Bernhard Scharrer
    Bernhard Scharrer over 5 years
    @BenjaminW. I just added the -type f after his/her comment
  • Benjamin W.
    Benjamin W. over 5 years
    Ah, I see :) That doesn't fix the problem, though. The file /path/with space/nospace.txt would still be deleted.
  • Stéphane Chazelas
    Stéphane Chazelas over 5 years
    Also ls * when pipes outputs the file names new-line delimited. echo (at least some echo implementations) expands backslash sequences. grep -Z is for writing file names NUL-delimited when using -l, it won't help here. If you meant -z, that won't help either as most ls implementations lack an option to output file names NUL delimited. One could do something like printf '%s\0' * | grep -z ' ' | xargs -r0 rm -f though.
  • Bernhard Scharrer
    Bernhard Scharrer over 5 years
    @BenjaminW. fixed ?
  • Nanhe Kumar
    Nanhe Kumar almost 4 years
    Working fine rm "upload/image/catalog/Varanga/WhatsApp Image 2019-12-29 at 9.45.45 PM.jpeg"