I have a directory containing files with names


and so on. I would like to delete all those that are a multiple of 5000.

I tried the following: printf 'rho_%d.txt\n' $(seq 5000 10000 25000) | rm, but that gave me the response rm: missing operand.

Is there another way to do this?

  • 6
    Regardless of how you do this, I recommend (for safety) moving the desired files into a directory, then when you've confirmed that all and only the files to be deleted are in the directory, delete the directory.
    – user117529
    Commented Oct 19, 2017 at 1:53
  • Safety is important here. I generally test these these things with ls first.
    – Scot
    Commented Oct 19, 2017 at 4:39

4 Answers 4


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

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

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
  • If your version of seq has the -f/--format option you can use that instead of this work-around with printf and command substitution. Commented Oct 20, 2017 at 15:46
  • Thank, I've just answered using the command used in the question and trying to explain his rm error
    – M4rty
    Commented Oct 20, 2017 at 17:12

Someone correct me if I'm wrong, but if all the files that start with rho_ are of this form, isn't it equivalent to do

rm rho_0.txt rho_*[05]000.txt

Since a number (other than 0) is a multiple of 5000 if and only if it ends in 0000 or 5000.

(0 is a special case, since it is a multiple of 5000, but is typically written without additional leading zeros. If you didn't intend to include 0, then take it out of the above line.)

Of course this should only be used if all the rho_*.txt files in your directory have the desired form. Don't use it if your directory also contains rho_please_dont_delete_me_5000.txt.

(Math note: This works precisely because all the prime factors of 5000, namely 2 and 5, are also prime factors of the base we are working in, 10. Equivalently, there is a sufficiently large power of 10 (namely 10000) which is divisible by 5000. There is no similar trick for matching all those which are multiples of 3000.)

  • This actually is the best answer.
    – cesarv
    Commented Oct 19, 2017 at 22:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .