Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have files fileName_1, fileName_2 ... fileName_100000 where the fileNames are numbered sequentially. How can I delete the files of index greater than, say 100?

share|improve this question

4 Answers 4

up vote 18 down vote accepted

If filenames are numbered sequentially just run the following command:

rm fileName_{100..100000}
share|improve this answer
3  
Info: This works because bash (your shell) will expand the {x..y} to the whole list. For example if you use echo test{1..3} it will be expanded to echo test1 test2 test3 and then executed. As you can see, it prints test1 test2 test3. Similarly, if the {x..y} is not connected to any argument, it will just put the numbers there. E.g. echo test {1..3} will just print test 1 2 3. –  Luc May 15 at 14:24
1  
Note: there is a limit imposed by the exec call to the number of arguments that a process can have. You can retrieve it with getconf ARG_MAX. For example on my system it is 2097152 (~2.1 millions). This means that rm (which is not a built-in) will fail with more than that many files. Built-in commands don't have this problem, and neither do the other solutions proposed. –  Bakuriu May 15 at 20:49
    
Note that ARG_MAX is a number of bytes, not a number of arguments. rm can't handle deleting 2097152 files at once. It could delete 2048 files which each had a name that was 1023 bytes long (not 1024; the delimiters between arguments (\0 in C-land) count towards the limit). –  godlygeek May 16 at 17:35

You may try this command also,

for i in $(seq 100 100000); do rm fileName_$i; done

It will delete all the files(ranges from 100 to 100000) if the filename contains numbers in an sequential order.

share|improve this answer

In general you have several ways to achieve this:

rm fileName_{100..100000}

will be expanded by the shell into roughly 100,000 filenames. If the basename is a long path and the number is high enough, you might actually exceed the maximum length of a command line with this approach.

The for loop

for i in $(seq 100 100000); do rm fileName_$i; done

does not suffer from this problem, but is a relatively slow way of deleting the files, as the shell has to perform the variable substitution and launching rm about 100,000 times.

Both methods above might cause warnings if some of the files, say filename_101 were missing.

Usually, solutions based on find are better, as they only work on files that are actually there (during discovery). However, there are some subtle differences:

find . -name 'fileName_[100-100000]' -exec rm {} \;

will still launch the rm program about 100,000 times, while replacing the final \; by + will try to minimize the number of subprocesses. Both methods will probably be (much or a bit) slower than using -delete which does not use external commands at all but invokes system calls instead.

However, always check first if the pattern actually matches the files that you want to target:

stefan@tuxedo ~ % mkdir askubuntu
stefan@tuxedo ~ % touch askubuntu/filename_{1..1000}
stefan@tuxedo ~ % find askubuntu -name 'filename_[100-1000]' 
askubuntu/filename_1

So in this case you'd delete a file you wanted to keep and left the files that should have been removed.

share|improve this answer

Use the following command:

find . -name 'fileName_[100-100000]' -exec rm {} \;

That will delete files from 100 to 100000 .

More information: Site

share|improve this answer
    
You'd be better off using + rather than \; -- see here. Or, even better, use find's -delete command. –  evilsoup May 16 at 15:34
    
I tested this, and it only deleted fileName_1. The [100-100000] character class only matches a single character, so it can only match 0 or 1. –  deltab May 16 at 16:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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