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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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 -- * *
Method 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.
Method 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'
Method 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
Method 5
You can use:
find . -name '* *' -delete
Method 6
To do it that way, you will need the -Z option of grep, and the -0 option of xargs. But I would not do it that way (ls is not the right tool for the job, there are many problems in getting the computer to read its output).
See other answers for a better way.
Also
ls | … is equivalent to ls -d * and echo * | …
All of which have problems. Therefore don’t use ls like this, use a solution from another answer.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0