Argument list too long error for rm, cp, mv commands

前端 未结 27 2385
长情又很酷
长情又很酷 2020-11-22 04:50

I have several hundred PDFs under a directory in UNIX. The names of the PDFs are really long (approx. 60 chars).

When I try to delete all PDFs together using the fol

相关标签:
27条回答
  • 2020-11-22 05:03

    you can use this commend

    find -name "*.pdf"  -delete
    
    0 讨论(0)
  • 2020-11-22 05:04

    The reason this occurs is because bash actually expands the asterisk to every matching file, producing a very long command line.

    Try this:

    find . -name "*.pdf" -print0 | xargs -0 rm
    

    Warning: this is a recursive search and will find (and delete) files in subdirectories as well. Tack on -f to the rm command only if you are sure you don't want confirmation.

    You can do the following to make the command non-recursive:

    find . -maxdepth 1 -name "*.pdf" -print0 | xargs -0 rm
    

    Another option is to use find's -delete flag:

    find . -name "*.pdf" -delete
    
    0 讨论(0)
  • 2020-11-22 05:04

    Using GNU parallel (sudo apt install parallel) is super easy

    It runs the commands multithreaded where '{}' is the argument passed

    E.g.

    ls /tmp/myfiles* | parallel 'rm {}'

    0 讨论(0)
  • 2020-11-22 05:05

    find has a -delete action:

    find . -maxdepth 1 -name '*.pdf' -delete
    
    0 讨论(0)
  • 2020-11-22 05:05

    What about a shorter and more reliable one?

    for i in **/*.pdf; do rm "$i"; done
    
    0 讨论(0)
  • 2020-11-22 05:07

    To delete all *.pdf in a directory /path/to/dir_with_pdf_files/

    mkdir empty_dir        # Create temp empty dir
    
    rsync -avh --delete --include '*.pdf' empty_dir/ /path/to/dir_with_pdf_files/
    

    To delete specific files via rsync using wildcard is probably the fastest solution in case you've millions of files. And it will take care of error you're getting.


    (Optional Step): DRY RUN. To check what will be deleted without deleting. `

    rsync -avhn --delete --include '*.pdf' empty_dir/ /path/to/dir_with_pdf_files/
    

    . . .

    Click rsync tips and tricks for more rsync hacks

    0 讨论(0)
提交回复
热议问题