I have many files named


and files named


and files named




I just want to remove the files that begin with 'sequence_1' and end in '.hmf', but I don't want to remove them one by one, since there are thousands of files. How can I specify to the rm command that I want to remove all that begin with the prefilx 'sequence_1' and end in '.hmf'?

I'm currently working with a RedHat Linux system, but I'd like to know how to do it on other distributions as well.

3 Answers 3

rm sequence_1*.hmf

removes files beginning with sequence_1 and ending with .hmf.

Globbing is the process in which your shell takes a pattern and expands it into a list of filenames matching that pattern. Do not confuse it with regular expressions, which is different. If you spend most of your time in bash, the Wooledge Wiki has a good page on globbing (pathname expansion). If you want maximum portability, you'll want to read the POSIX spec on pattern matching as well / instead.

In the unlikely case you run into an "Argument list too long" error, you can take a look at BashFAQ 95, which addresses this. The simplest workaround is to break up the glob pattern into multiple smaller chunks, until the error goes away. In your case, you could probably get away with splitting the match by prefix digits 0 through 9, as follows:

for c in {0..9}; do rm sequence_1_"$c"*.hmf; done
rm sequence_1*.hmf  # catch-all case
  • This is a very clever work around the 'argument list too long' error. In my case, I have twenty thousand files, so I guess I would have to use a for loop from 0,..,20. Right?
    – Paul
    Apr 26, 2012 at 14:18
  • @Paul Right, just break the list into as many chunks as you need, although at some point the find approach becomes easier than guess and check.
    – jw013
    Apr 26, 2012 at 14:24
  • upvoted for the for solution. Somehow I could not get to figure out how to use find -regex to match my files and your solution works just fine!
    – Adrien
    Nov 2, 2021 at 14:13

Although jw013's answer is correct w.r.t globbing, that command may fail if you have thousands of matches: the expanded command line rm sequence_1_0001.hmf sequence_1_0002.hmf ... generated by the shell may simply be too big.

As Dom suggested, you can also use the -delete option with find:

find . -maxdepth 1 -type f -name 'sequence_1*.hmf' -delete

Both -maxdepth and -delete, while not in the POSIX standard are fairly common in find implementations in the wild. Linux distributions generally use GNU find, which certainly supports those options.

  • 2
    If you can, use the -delete option for find, it doesn't fork for each file...
    – Dom
    Apr 26, 2012 at 14:08
  • Added, cheers. Looks like -delete should be supported on recent GNU and BSD systems, while -print0 is GNU-only. So it's likely more portable too (although it shouldn't make a difference for the OP).
    – Useless
    Apr 26, 2012 at 15:13
  • At least OS X's find has -print0, but it's not included in POSIX.
    – Lri
    Apr 3, 2013 at 20:26
rm sequence_1_{0000..0999}.hmf
rm sequence_1_{1000..1999}.hmf
rm sequence_1_{2000..2999}.hmf

would work too in Bash.

  • The brace expansion requires bash, and the zero padded form needs version 4, I believe.
    – jw013
    Apr 26, 2012 at 23:45

You must log in to answer this question.

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