Unix: How to Delete Files Listed in a File

Delete files from a list in a text file

As you would expect, the trick is to get the quoting right:

while read line; do rm "$line"; done < filename

Shell command/script to delete files whose names are in a text file


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

Is there a way to list out files and delete by creator in unix or linux

You can do that with find. First find the right syntax and confirm the matches:

find /path/to -type f -user username -maxdepth 1

If all looks ok, you can go ahead and make it delete the matched files:

find /path/to -type f -user username -maxdepth 1 -delete

Of if your version of find doesn't have -delete then you can do like this:

find /path/to -type f -user username -maxdepth 1 -exec rm {} \;

Delete files 100 at a time and count total files

You can avoid xargs and do this in a simple while loop and use a counter:

destdir='/example-dir/'
count=0

while IFS= read -d '' file; do
rm -rf "$file"
((count++))
done < <(find "$destdir" -type f -print0)

echo "Deleted $count files from $destdir"

Note use of -print0 to take care of file names with whitespaces/newlines/glob etc.

Delete files with string found in file - Linux cli

For safety I normally pipe the output from find to something like awk and create a batch file with each line being "rm filename"

That way you can check it before actually running it and manually fix any odd edge cases that are difficult to do with a regex

find . | xargs grep -l email@example.com | awk '{print "rm "$1}' > doit.sh
vi doit.sh // check for murphy and his law
source doit.sh

Delete a list of files with find and grep


find . -name '*car*' -exec rm -f {} \;

or pass the output of your pipeline to xargs:

find | grep car | xargs rm -f

Note that these are very blunt tools, and you are likely to remove files that you did not intend to remove. Also, no effort is made here to deal with files that contain characters such as whitespace (including newlines) or leading dashes. Be warned.

how to remove file from folder and subfolder single command linux

You can use "find" command with "delete" option. This will remove files with specified name in current directory and sub directories.

find . -name "http.log2019*" -delete


Related Topics



Leave a reply



Submit