How to work around shell limitation of 'Argument list too long'?

5,317

Solution 1

for i in myLocalFiles*; do rm -f $i; done

Solution 2

This is a little simpler than jáquer suggested:

find /tmp -name "myLocalFiles*" -delete

Solution 3

For those who can't use -delete to find command, this also works:

find /tmp -name "myLocalFiles*" -print0 | xargs -0 rm -rf

Solution 4

find /tmp -name "myLocalFiles*" -exec rm -rf {} +
Share:
5,317

Related videos on Youtube

WilliamKF
Author by

WilliamKF

Updated on September 17, 2022

Comments

  • WilliamKF
    WilliamKF almost 2 years

    How can I work around the bash shell limitation of 'Argument list too long' when using filename wildcards that match a ton of files in /tmp/?

    [my-centos4 tmp] rm -rf /tmp/myLocalFiles*
    bash: /bin/rm: Argument list too long
    
  • WilliamKF
    WilliamKF over 13 years
    My 'find' on centos4 does not support the -delete option.
  • Dennis Williamson
    Dennis Williamson over 13 years
    @WilliamKF: Then your find probably also does not support find ... -exec rm {} +
  • Édouard Lopez
    Édouard Lopez almost 11 years
    from #bash channel: "xargs(1) is dangerous (broken, exploitable, etc.) when reading non-NUL-delimited input. If you're working with filenames, use find's -exec [command] {} + instead or -print0 | xargs -0 provided you don't care about portability and like doing unnecessary extra work. Othwerwise a nice for loop."