0

I'm not sure how/why this is happening. Basically I have a command that is supposed to find and clean out all files in the desktop that contain a certain pattern. I am doing the first part using:

find . -iregex '.*._.*'

When I run this by itself, the output is normal i.e. it shows all files that contain that pattern. ex:

./Graph/CMPT 215/makefiles/._makefile6
./Graph/CMPT 215/makefiles/._example3.cmds
./Graph/CMPT 215/makefiles/._makefile5
./Graph/CMPT 215/makefiles/._makefile1
./Graph/CMPT 215/makefiles/._makefile4
./Graph/CMPT 215/makefiles/._makefile2
./Graph/CMPT 215/makefiles/._makefile3
...

However I want to delete all these files, so after some google search, I came up with this:

rm -rf $(find . -iregex '.*._.*')

But the problem is that when I run this command, I get the following output:

rm: cannot remove â?~215/makefiles/._makefile6â?T: No such file or directory
rm: cannot remove â?~./Graph/CMPTâ?T: No such file or directory
rm: cannot remove â?~215/makefiles/._example3.cmdsâ?T: No such file or directory
rm: cannot remove â?~./Graph/CMPTâ?T: No such file or directory
rm: cannot remove â?~215/makefiles/._makefile5â?T: No such file or directory
rm: cannot remove â?~./Graph/CMPTâ?T: No such file or directory
rm: cannot remove â?~215/makefiles/._makefile1â?T: No such file or directory
rm: cannot remove â?~./Graph/CMPTâ?T: No such file or directory
rm: cannot remove â?~215/makefiles/._makefile4â?T: No such file or directory
rm: cannot remove â?~./Graph/CMPTâ?T: No such file or directory
rm: cannot remove â?~215/makefiles/._makefile2â?T: No such file or directory
rm: cannot remove â?~./Graph/CMPTâ?T: No such file or directory
rm: cannot remove â?~215/makefiles/._makefile3â?T: No such file or directory
...

I looked online and it was suggested to reset the terminal by typing reset, but this has not fixed it. What can I do to make sure the output to rm is correct?

If it helps, I am on Windows and using ssh to connect to a remote machine running Linux Mageia and deleting some files on the machine

1

This should work: find . -iregex '.*._.*' -exec rm {} \;.

The results of the find command will be sent to the rm command as its argument, {} is the placeholder for that argument, \; is to complete the -exec flag of the find function.

More info on the find command here.

  • Ok that worked, but why was the command I used producing the weird output it was? – smac89 Nov 12 '14 at 21:28
  • 1
    There could be many answers to that question. Likely was caused by the spaces in the file path. It boils down to the command substitution $(find . -iregex '.*._.*') echoing invalid characters, falsifying the file path that rm was trying to use. The find command escapes those characters before passing it to the rm command in this case. FYI: be VERY careful with rm -rf, it's easy to remove things you do not want to be removed, like the / directory. – Ray Nov 12 '14 at 21:44
  • Is there an alternative to rm? – smac89 Nov 12 '14 at 21:47
  • Just do not use the -f flag to force it unless you are 100% sure of what you are removing. – Ray Nov 12 '14 at 21:48
  • 2
    And the other general lesson that you should learn from this experience is not to use the output of find (which does -print by default) for anything but viewing by a sentient life form. If you must pass the list of files to some other program, use -print0 (which writes the pathnames separated by nuls instead of newlines) and pass it to one of the few programs that can handle that (e.g., xargs, with the -0 option). If you simply want to do something to each found file, use -exec (or -delete, if appropriate, and if your version of find supports it). – G-Man Nov 12 '14 at 22:01
0

I think you should use xargs command with find like that:

find . -iregex '.*._.*' | xargs rm -rf

find will collect all unneeded files in one string and xargs rm -rf will delete all the file paths from those string. It is more efficient to use xargs with find then -exec option because it will run rm -rf only ones unike with -exec option.

  • I tried xargs, but some of the files contain the ' character so xargs tries to interpret that as something else – smac89 Nov 12 '14 at 21:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.