1

This may have been addressed somewhere but unable to find command that meets my requirement.

So basically lets say there are 51 files starting with aaa.log aaa.log.1 aaa.log.2 . aaa.log.4 aaa.log.40 aaa.log.41 . . aaa.log.50

Now, I intend to delete files with aaa.log.40 to aaa.log.50.

If i run rm -f aaa.log.4* it will delete everything including aaa.log.4 What tweak should I use so that it deletes only aaa.log.40 through 50 but not aaa.log.4 ?

4

The default shell in Ubuntu is bash. In bash (and probably many other shells as well) you can do it like this:

rm -f aaa.log.{40..50}

The expansion {40..50} expands to the range of number from 40 to 50 and automatically cause the rest of the word to be duplicated with each expanded number. It will not verify the existence of the file names, so it may cause rm to be given names of non-existing files. However since you apparently wanted to use the force option anyway, this is not a problem. When you type rm -f it will not print any error messages about missing files.

It is usually a good idea to test the command with echo in front if you are unfamiliar with the expansion. Then you can see what the arguments expand to:

echo rm -f aaa.log.{40..50}
  • That did the trick:) – avanish May 12 '16 at 8:13
1

In addition to @kasperd's answer, the following may be helpful: if the numbers were not sequential (e.g. 20-30 and 50-60) one could do the following:

rm aaa.log.{2,3,5,6}[0-9]

Which will remove all occurences of files with tails ending in 2, 3, 5, or 6 followed by any number in the range 0-9.

Update: @janos has pointed out this will remove 31-39 and 61-69, as well as the other files listed.

0

If you use zsh with extended globbing you can use rm aaa.<40-50>.

  • Thanks, that did help on my Macbook pro however the ubuntu appliance which I am working on does not recognize zsh . Any other tricks? – avanish May 12 '16 at 6:37
  • @avanish bash has extended globbing as well, though the syntax is different from the one in zsh. – kasperd May 12 '16 at 7:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.