chaoswarriorx
chaoswarriorx

Reputation: 11

xargs and newlines in txt files - " xargs -d '/n' " is failing

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!

Upvotes: 1

Views: 3730

Answers (3)

Cœur
Cœur

Reputation: 38717

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

Upvotes: 2

JosefN
JosefN

Reputation: 952

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

Upvotes: 0

Aur&#233;lien Thieriot
Aur&#233;lien Thieriot

Reputation: 5923

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.

Upvotes: 0

Related Questions