linux - 对文件进行计数,如果计数大于给定限制,则删除剩余文件

标签 linux bash shell

我使用下面的脚本来递归计算目录中的文件数量。在每个子目录中,如果count大于3,我需要删除剩余的文件。

是否需要为每个子目录添加另一个for循环,如果超过3个则删除剩余文件?

START=$HOME

# change your directory to command line if passed
# otherwise use home directory
[ $# -eq 1 ] && START=$1 || :

if [ ! -d $START ]
then
        echo "$START not a directory!"
        exit 1
fi

# use find command to get all subdirs name in DIRS variable
DIRS=$(find "$START" -type d)

# loop thought each dir to get the number of files in each of subdir
for d in $DIRS
do
   [ "$d" != "." -a "$d" != ".." ] &&  echo "$d dirctory has $(ls -l $d | wc -l) files" || :

done

最佳答案

find 已经提供了 START 下面的子目录数量。由于包含 PWD (.),计数增加了 1。因此,要获取 START 下面的子目录数量(不包括 START 本身):

find "$START" -type d | echo "subdirs: $(($(wc -l)-1))"

输出示例:

subdirs: 6

要获取文件数量,只需使用-type f(无需减1):

find "$START" -type f | echo "files: $(wc -l)"

输出示例:

files: 128

您可以将所有这些放在一起,同时将 find 文件的 -maxdepth 限制为 1 并完成您想要的任务。 注意:下面的脚本仅打印是否保留删除文件(每个目录中的前3个被保留,其余的被删除)。您将需要实现自己的保留/删除方案以满足您的需求。这将使您了解您可以做什么:

#!/bin/bash

start="${1:-$PWD}"

[ -d "$start" ] || {
    printf "error: directory not accessible '%s'\n", "$start"
    exit 1;
}

declare -a files                                # declare an array to hold files

for i in $(find "$start" -type d); do           # find all subdirs

    [ "$i" = "$PWD" ] && continue               # skip the current (top) dir

    printf "\nprocessing: %s\n\n" "$i"          # simple dir info output (not needed)

    files=( $(find "$i" -maxdepth 1 -type f) )  # fill array w/files in each dir (no subs)
    if [ "${#files[@]}" -gt 0 ]; then           # if subdir has files
        echo "num files: ${#files[@]}"          # No. of files in dir info (not needed)
        for ((j=0; j<${#files[@]}; j++)); do    # for each file
            if [ "$j" -lt 3 ]; then             # if first 3, keep
                printf "  keeping : %s\n" "${files[j]}"
            else                                # files 4+ delete
                printf "  deleting: %s\n" "${files[j]}"
            fi
        done
    fi
done

示例输出:

$ bash del3dirs.sh

processing: /home/david/scr/tmp/stack/sav

num files: 5
  keeping : /home/david/scr/tmp/stack/sav/dna.sh
  keeping : /home/david/scr/tmp/stack/sav/dict-sort_1.sh
  keeping : /home/david/scr/tmp/stack/sav/_20140702-211202.tar.xz
  deleting: /home/david/scr/tmp/stack/sav/dict-sort.sh
  deleting: /home/david/scr/tmp/stack/sav/dict-sort_2.sh

processing: /home/david/scr/tmp/stack/tmp

num files: 13
  keeping : /home/david/scr/tmp/stack/tmp/File1950text.doc
  keeping : /home/david/scr/tmp/stack/tmp/vcs1dump
  keeping : /home/david/scr/tmp/stack/tmp/File2014text.xls
  deleting: /home/david/scr/tmp/stack/tmp/File307list.cvs
  deleting: /home/david/scr/tmp/stack/tmp/vcsa1dump
  (snip)

processing: /home/david/scr/tmp/stack/tmp/a

num files: 2
  keeping : /home/david/scr/tmp/stack/tmp/a/b/b.txt
  keeping : /home/david/scr/tmp/stack/tmp/a/b/b.txt.bac

processing: /home/david/scr/tmp/stack/dat

num files: 68
  keeping : /home/david/scr/tmp/stack/dat/outlier.dat
  keeping : /home/david/scr/tmp/stack/dat/3line.dat.sav
  keeping : /home/david/scr/tmp/stack/dat/f1f2.dat
  deleting: /home/david/scr/tmp/stack/dat/datb.dat
  deleting: /home/david/scr/tmp/stack/dat/lline.dat
  deleting: /home/david/scr/tmp/stack/dat/vowels.txt

关于linux - 对文件进行计数,如果计数大于给定限制,则删除剩余文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27074331/

相关文章:

linux - 为什么RAM的写入速度远低于上面所说的?

php - PHP cli 中没有颜色

linux shell - 检查今天的文件,如果今天没有就上传到hdfs 如果不是今天就不要上传。给出文件已存在的消息

linux - 如何读取上一个命令的输出,然后继续执行下一个命令

linux - nginx 作为运行 apache 的反向代理

linux - 如何使用 ssh + 密码连接到 Aptana studio 3? (SFTP)

linux - Bash:分离下标,但保持交互

windows - 安装包后找不到 Git Bash 命令

linux - Bash - 为什么 sfdisk 在此脚本中不起作用?

android - 如何通过 shell 命令启用 Android 网络共享?