Deleting specific files based on filename from terminal

You don't need a loop or extra commands where you have Bash Shell Brace Expansion.

rm -f rho_{0..200000..5000}.txt

Explanation: {start..end..step}. The -f to ignore prompt on non-existent files.


P.s. To keep safety and check which files will be deleted, please do a test first with:

ls -1 rho_{0..200000..5000}.txt

rm doesn't read from standard input. You could do:

for i in $(seq 5000 5000 25000); do
    rm -i rho_${i}.txt
done

I include the -i option to rm to prompt before removal so that you can verify the behavior. Once you're confident it's doing what you want, you could omit that option.

Edit: Alternatively, you could do:

for ((i = 5000; i <= 25000; i += 5000)); do
    rm -i rho_${i}.txt
done

That form may be more familiar if you've done any programming.


You cannot pipe to a program as if the content of the pipe were arguments. It's sent through stdin.

You should use xarg for this purpose :

printf 'rho_%d.txt\n' $(seq 5000 10000 25000) | xargs rm 

but first you can give a try with echo to see if everything is as you intend :

printf 'rho_%d.txt\n' $(seq 5000 10000 25000) | xargs echo

Tags:

Rm

Files