3

I have a shell script that needs to delete all of the files in a directory that start with a number. This file set has grown to contain hundreds of thousands of files that need to be deleted each day. The script contains the following lines:

rm -f /my/dir/11*
rm -f /my/dir/12*

(( etc ))

rm -f /my/dir/1*
rm -f /my/dir/2*

And I get the error message for every line

line 1: /usr/bin/rm: Argument list too long

I tried to replace the lines with

ls -d /my/dir/11* | xargs rm

but ls -d gives me the same error message.

How can I delete these files without growing the list to contain hundreds of filename permutations?

2
  • 1
    Which shell are you using? (bash? ksh? zsh? Something else?) Commented Jun 9, 2020 at 14:15
  • And what operating system? Do you have GNU tools?
    – terdon
    Commented Jun 9, 2020 at 14:16

2 Answers 2

8

If you want to get a relative path and pass it onto rm, you can use the find command, for your use case I'd run:

find /my/dir -iname '[0-9]*' -type f

That would return every file that start with a number. If that list is what you want to delete, have find delete them using -delete:

find /my/dir -iname '[0-9]*' -type f -delete

Good luck!

8
  • Of course you MUST cd into the desired folder if you don't want to do something catastrophic. Commented Jun 9, 2020 at 14:21
  • 1
    Using -exec rm -f {} + will probably be significantly faster, since it won't need to run one operation per file.
    – terdon
    Commented Jun 9, 2020 at 14:32
  • Bad habit of mine, @roaima. I'll edit the answer! Commented Jun 9, 2020 at 14:35
  • 2
    Not breaking out a rm subprocess and instead using -delete would be even faster.
    – DopeGhoti
    Commented Jun 9, 2020 at 14:36
  • 1
    find ... -delete isn't POSIX, so find ... -exec rm -f {} + might be necessary for some implementations Commented Jun 9, 2020 at 15:11
1

If your shell has a built-in printf command (e. g. Bash or Dash) it likely accepts longer argument lists which you can pipe to xargs:

printf '%s\0' /path/to/glob* | xargs -0 rm -f

You must log in to answer this question.

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