Xargs

From Wiki
Jump to navigation Jump to search

Example:

find . -mtime -40 -print | xargs ls -ldt

If there are too many files:

$ ls -l *.dcm | more
-bash: /bin/ls: Argument list too long

Instead do this:

ls -l | grep dcm | xargs

Problem:

$ mv *.dcm /usr/local/pkg/dc/dicom-import/
-bash: /bin/mv: Argument list too long

Solution:

find . -name "*.dcm" -exec mv {} /usr/local/pkg/dc/dicom-import/ \;

Problem: Trying to remove all the .hdr files in a very full directory:

$ rm -f *.hdr
-bash: /bin/rm: Argument list too long

Solution:

find . -name "*.hdr" -exec rm -f {} \;

WARNING: if the current directory contains any subdirectories, all of the .hdr files will be deleted from them as well.