1

I have a list over about 10k items I need to delete in a txt file.

I started here: Unix: How to delete files listed in a file

but ran into

rm: 721_0199_01.mov\r: No such file or directory

Looks like I need it to ignore / not pass '/r'

So, I go here: xargs and find, rm complaining about \n (newline) in filename

where it looks like maybe :

xargs -d '/r' rm < my-big-list.txt 

but it complains:

xargs: illegal option -- d
usage: xargs [-0opt] [-E eofstr] [-I replstr [-R replacements]] [-J replstr]
         [-L number] [-n number [-x]] [-P maxprocs] [-s size]
         [utility [argument ...]]

I can't find -d in any man page I look at for xargs, so not sure where to go from here....

Am I way off, did I miss read?

THANK YOU!

Well - Again, thank you all, I did get rid of the /r, but I have tons of whitespace in these filenames, which leads me to:

How can I make xargs handle filenames that contain spaces?

and

-0 Change xargs to expect NUL (``\0'') characters as separators, instead of spaces and newlines. This is expected to be used in concert with the -print0 function in find(1).

And that to:

Linux: Redirecting output of a command to "find"

But that's not really helping...

rad vans Skype Testccc995141.vid

rad vans Skype Testccc995142.vid

rad vans Skype Testccc995143.aiff

rad vans Skype Testccc995144.aiff

rad vans Skype Testccc995145.qtc

rad vans Skype Testccc998bf.mov

rad vans Skype Testccc998bf1.vid

rad vans Skype Testccc998bf2.vid

rad vans Skype Testccc998bf3.aiff

rad vans Skype Testccc998bf4.aiff

rad vans Skype Testccc998bf5.qtc

is a good example of what I'm trying to delete - I suspect this list is about 60% of the contents of the directory...

Is there some trick to use Find?

THANKS!

Community
  • 1
  • 1
chaoswarriorx
  • 11
  • 1
  • 4
  • did you prepare you txt file in windows? In this case simply remove CR first and it would work. tr -d '\r' < infile > outfile – JosefN Jan 20 '14 at 20:51
  • I used TextEdit on OS X... – chaoswarriorx Jan 21 '14 at 21:34
  • Text edit in plain text mode use only LF so it is little bit weird. Anyway it is no harm to run tr -d '\r' my-big-list2.txt and compare length. If new file is smaller CR were removed and with such list xargs can work. I am only worry about command line limit. – JosefN Jan 21 '14 at 21:46
  • I did go Numbers -> TextEdit -> Switch to Text Mode from RTF. I'm guessing the /r s came from there maybe? – chaoswarriorx Jan 21 '14 at 23:09
  • probably, I like vi editor :) Simple check is "head | your_file |od -n ". I suppose that you see combination cr ln. Simply process your file by tr -d '\r'. – JosefN Jan 22 '14 at 06:51

3 Answers3

2

When your xargs doesn't support option d (for delimiter), like on macOS, then you may sometimes use option 0:

< my-big-list.txt tr "\n" "\0" | xargs -0 rm

This solution is faster than while ... do ... done

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    So dumb that BSD xargs doesn't let you specify the delimiter, but this seems just about as good! – Max Jul 07 '21 at 19:15
  • Doesn't work for me on MacOS :( – yegor256 Oct 20 '22 at 18:07
  • Possibly, raise a new question stating clearly what isn't working, yegor256, because honestly, here, the StackOverflow question was about "illegal option -- d", which is sometimes (not always) circumvented by option 0. Note that maybe your line endings aren't "\n" but something different like "\r\n"? First normalise your line endings if that's the case. – Cœur Oct 20 '22 at 23:17
0

It's just a guess but it looks like your original file was made under a Windows operating system.

Under Windows, to express a new line, you use \r\n while under Uni* it is generally \n only. That would explains why xargs don't remove this character.

I suggest you to run your file through the program: dos2unix and it should fix your issue.

Aurélien Thieriot
  • 5,853
  • 2
  • 24
  • 25
0

you can try little bit different way:

remove.sh

#!/bin/bash
while read line
do 
    rm $line
done

remove.sh < my-big-list.txt

xargs approach can reach bash limit. echo $ARG_MAX should be bigger than your file. You can increase it by export ARG_MAX=new_value

JosefN
  • 952
  • 6
  • 8