Shell: 删除某时间之前的文件,Argument list too long错误, Remove all files created before a certain date,Argument list too long error

Shell: 删除某时间之前的文件,Argument list too long错误, Remove all files created before a certain date,Argument list too long error
Shell: 删除某时间之前的文件,Argument list too long错误, Remove all files created before a certain date,Argument list too long error

基于 find 来的做:

先了解 find 命令,

  1.  基于名字搜索
    # 单条件 -name只针对文件名
    find . -name '*.jpg' -print
    ./bar/foo.jpg
    
    # or 或条件
    find . \( -name '*.mp3' -o -name '*.jpg' \) -print
    ./bar/foo.jpg
    ./foo.mp3
    
    # and 和条件
    find . -name '*.mp3' -name '*.jpg' -print
    
    # 复合条件
    find . \( -name '*.mp3' -o -name '*.jpg' \) -name 'foo*' -print
    ./bar/foo.jpg
    ./foo.mp3
    
    # -path 针对路径
    find . -path './bar*' -print
    ./bar
    ./bar/foo.jpg
    
    # 非 条件
    griffon:/tmp/greg$ find . ! -path '*bar*' -print
    .
    ./foo.mp3
    ./apple.txt
  2. 基于时间搜索
    # 30天之前
    find /tmp -mtime +30 -print
  3. 基于 size 文件大小搜索
    # 大于 10 megabytes (10485760 bytes)
    find /home -type f -size +10485760c -print

 

搜索并删除

# 30天之前 
find /tmp -type f -mtime +30 -exec rm -f {} \; 

# size 为0 
find /tmp -size 0 -type f -name sess_* -exec rm -f {} \; 

# 1天之前 
find /tmp -mtime +1 -type f -name sess_* -exec rm -f {} \;

移动或者修改权限

# 错误或者不合理,可能会导致Argument list too long报错
chmod -R 755

# 正确
find /tmp -type d -exec chmod 755 {} \;
find /tmp -type f -exec chmod 644 {} \;

其他实例:

xargs is the tool for the job. That, or find with -exec … {} +. These tools run a command several times, with as many arguments as can be passed in one go.

Both methods are easier to carry out when the variable argument list is at the end, which isn’t the case here: the final argument to mv is the destination. With GNU utilities (i.e. on non-embedded Linux or Cygwin), the -t option to mv is useful, to pass the destination first.

If the file names have no whitespace nor any of \"', then you can simply provide the file names as input to xargs (the echo command is a bash builtin, so it isn’t subject to the command line length limit):

echo !(*.jpg|*.png|*.bmp) | xargs mv -t targetdir

You can use the -0 option to xargs to use null-delimited input instead of the default quoted format.

printf '%s\0' !(*.jpg|*.png|*.bmp) | xargs -0 mv -t targetdir

Alternatively, you can generate the list of file names with find. To avoid recursing into subdirectories, use -type d -prune. Since no action is specified for the listed image files, only the other files are moved.

find . -name . -o -type d -prune -o \
       -name '*.jpg' -o -name '*.png' -o -name '*.bmp' -o \
       -exec mv -t targetdir/ {} +

(This includes dot files, unlike the shell wildcard methods.)

If you don’t have GNU utilities, you can use an intermediate shell to get the arguments in the right order. This method works on all POSIX systems.

find . -name . -o -type d -prune -o \
       -name '*.jpg' -o -name '*.png' -o -name '*.bmp' -o \
       -exec sh -c 'mv "$@" "$0"' targetdir/ {} +

 

 

本文:Shell: 删除某时间之前的文件,Argument list too long错误, Remove all files created before a certain date,Argument list too long error