Reputation: 9
got a file like "index.txt" that contains a few lines of book titles like:
book 1.pdf<br>
book 1.opf<br>
book2.epub<br>
book3.opf<br>
and so more, 1 title = 1 line
Id'd like to do this thing in bash:
rm -rf from $dir IF $file IS NOT in index.txt
How I can do this?
Upvotes: 1
Views: 414
Reputation: 5341
You can use the below command.
find <dir> -name "*" | grep -vFf index.txt | xargs rm -rf
find
: List all the files in the specified directory
grep-vFf
: Do inverse grep with input from the file (will list files that are not found in the input file. In this case it is index.txt )
xargs rm -rf
: Delete each of the file that are not found in the list. This deletion list is obtained as the output of previous grep command
Edit:
When files names contain white spaces use the below command.
find <dir> -name "*" | grep -vFf index.txt |sed 's/^/"/;s/$/"/' | xargs rm -rf
sed would add quotes to all the files name.
Upvotes: 1
Reputation: 1272
you can remove the listed files in a txt file using
#!/bin/sh
while read line;
do
rm `echo $dir/line`
done<list.txt
Upvotes: 0