Argument list too long - Unix

后端 未结 4 1979
时光说笑
时光说笑 2020-12-07 01:35

This scripts will sort the files by date then move the first 2500 files to another directory.
When I run below scripts, system prompt out Argument list too long msg. Any

4条回答
  •  [愿得一人]
    2020-12-07 02:25

    Change

    ls -tr  $FROM_DIRECTORY/MSCERC*.Z|head -2500 | \
        xargs -i sh -c "mv {} $DESTINATION_DIRECTORY"  
    

    do something like the following:

    find "$FROM_DIRECTORY" -maxdepth 1 -type f -name 'MSCERC*.Z' -printf '%p\t%T@\n' | sort -k2,2 -r | cut -f1 | head -$NUM_OF_FILES | xargs mv -t "$DESTINATION_DIRECTORY"
    

    This uses find to create a list of files with modification timestamps, sorts by the timestamp, then removes the unneeded field before passing the output to head and xargs

    EDIT

    Another variant, should work with non GNU utils

    find "$FROM_DIRECTORY" -type f -name 'MSCERC*.Z' -printf '%p\t%T@' |sort -k 2,2 -r | cut -f1 | head -$NUM_OF_FILES | xargs -i mv \{\} "$DESTINATION_DIRECTORY"
    

提交回复
热议问题