17

I have a list of files in a .txt file (say list.txt). I want to delete the files in that list. I haven't done scripting before. Could some give the shell script/command I can use. I have bash shell.

0

7 Answers 7

50
while read -r filename; do
  rm "$filename"
done <list.txt

is slow.

rm $(<list.txt)

will fail if there are too many arguments.

I think it should work:

xargs -a list.txt -d'\n' rm
Sign up to request clarification or add additional context in comments.

6 Comments

Although rm $(<list.txt) is faster, +1 for mentioning the limitation it has on the number of arguments.
$ for i in *; do echo $i; done > ~/a $ rm frd.xml $ while read f; do rm $f; done < ~/a The above code works given by a colleague. This looks very similar to your answer. Thank you!
does this remove files in subdirs too?
xargs: illegal option -- a
xargs option worked for me...of course....i had to keep 1 filename per row, with no spaces in between, no quotes at the beginning and end....a completely sanitized plain text string works....and I had more than 900,000 files to be deleted
|
12

Try this command:

rm -f $(<file)

4 Comments

Your question says you have a list of file names in list.txt. Apparently, you don't. :-)
Either that or you need to give the full path to lists.txt
or gio trash $(<list.txt) to send them to the trash :)
Doesn't work with spaces in dir or file names.
5

If the file names have spaces in them, none of the other answers will work; they'll treat each word as a separate file name. Assuming the list of files is in list.txt, this will always work:

while read name; do
  rm "$name"
done < list.txt

2 Comments

Good point. Problem is the OP doesn't have a file called list.txt so this is causing him problems for some reason ;-)
Right. See my comment on Tim Pote's answer.
3

For fast execution on macOS, where xargs custom delimiter d is not possible:

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

Comments

1

The following should work and leaves you room to do other things as you loop through.

Edit: Don't do this, see here: http://porkmail.org/era/unix/award.html

for file in $(cat list.txt); do rm $file; done

5 Comments

its giving me an error. rm: cannot remove 'cat': No such file or directory. rm: cannot remove 'list.txt': No such file or directory
Changed format slightly. Did you use back ticks before? Note @TimPote answer might be better?
thanks for the answer. There seems to be something wrong with my bash environment. But got no one to check my workstation at my office currently and I cant post that problem here. So, Il just have to wait till Monday(next working day) till someone takes a look at it.
@AdamLiss: Oh wow, I had no idea. Thanks for enlightening! Note your first link was more interesting, especially as it relates to the exact example I posted, repeated here: partmaps.org/era/unix/award.html
@yamen: Thanks for the repost. I couldn't decide which link would be more helpful. In general, you can replace $(cat foo) with $(<foo) to avoid spawning an extra process (and, if you're me, typos as well).
1

I was just looking for a solution to this today and ended up using a modified solution from some answers and some utility functions I have.

// This is in my .bash_profile

# Find
ffe () { /usr/bin/find . -name '*'"$@" ; } # ffe: Find file whose name ends with a given string

# Delete Gradle Logs
function delete_gradle_logs() {
   (cd ~/.gradle; ffe .out.log | xargs -I@ rm@)
}

Comments

0

On linux, you can try:

printf "%s\n" $(<list.txt) | xargs -I@ rm @

In my case, my .txt file contained a list of items of the kind *.ext and worked fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.