PDA

View Full Version : Read file line by line in BASH



Tristanm1
January 5th, 2010, 05:44 AM
I am attempting to delete duplicate files. Using fdupes, I've generated a file that lists the full path of all duplicate files (excluding one copy of each file). Now what I need to do is read from this file so that I can use each filepath with the rm command. I am unsure how to do this, however. Could someone help me with this problem?

falconindy
January 5th, 2010, 05:49 AM
while read file; do
rm "$file"
done < /path/to/dupe.list

Tristanm1
January 5th, 2010, 06:00 AM
That works, thanks. The blank lines in the file throw back an error with rm, but that is fine.

iMisspell
January 5th, 2010, 06:46 AM
That works, thanks. The blank lines in the file throw back an error with rm, but that is fine.


This is untested, but im pretty sure it will work...



while read file; do
# check if file exists, if so, then delete...
if [[ -e "$file" ]] ; then
rm "$file"
fi
done < /path/to/dupe.list