linux下用脚本将目录内的文件分类到各自的创建年份(如2023)或年月份(如2023/202308)目录内

第一个if判断语句中判定只有是文件的时候才执行mv操作,并忽略一些特定

第二个if判断目录不存在时创建目录

最后mv文件到目录内

脚本执行前目录内容:

2022-01-file

2023-02-file

脚本执行后目录内容:

2022

|2022-01-file

2023

|2023-02-file

bash 复制代码
#!/bin/bash
#for line in `ls -lhrt`
for line in `ls -lhrt | grep -v moveToModifyDir.sh | awk -F " " '{ print $9}'`
do
    if [ -f "$line" ] && [ "$line" != "" ] \
        && [ "$line" != "." ] \
        && [ "$line" != ".." ] \
        && [ "$line" != "desktop.ini" ] \
        && [ "$line" != "@eaDir" ] \
        && [ "$line" != "Thumbs.db" ]
    then
        modifyYear=`stat $line | grep Modify | awk -F " " '{ print $2 }' | awk -F "-" '{ print $1 }'`
        modifyMonth=`stat $line | grep Modify | awk -F " " '{ print $2 }' | awk -F "-" '{ print $2 }'`
        echo "file: $line modifyYear is: $modifyYear modifyMonth is: $modifyMonth"
        realDir="${modifyYear}${modifyMonth}/"
        if [ ! -d $realDir ]
        then
            echo "$realDir not exist, ready to mkdir"
            mkdir $realDir
        elif [ -d $realDir ]
        then
            echo "$realDir exist..."
        fi
        echo "realDir is: $realDir"
        mv $line $realDir
    fi
done

第二个脚本是先创建年份目录,再创建年+月子目录,并将文件移动到子目录内

脚本执行前目录内容:

2022-01-file

2023-02-file

脚本执行后目录内容:

2022

|2022-01

|2022-01-file

2023

|2023-02

|2023-02-file

bash 复制代码
#!/bin/bash
for line in `ls -lhrt | grep -v "moveToModifyDir.sh\|moveToYearDateDir.sh" | awk -F " " '{ print $9}'`
do
    if [ -f "$line" ] && [ "$line" != "" ] \
        && [ "$line" != "." ] \
        && [ "$line" != ".." ] \
        && [ "$line" != "dl.sh" ] \
        && [ "$line" != "desktop.ini" ] \
        && [ "$line" != "@eaDir" ] \
        && [ "$line" != "urls.txt" ] \
        && [ "$line" != "Thumbs.db" ]
    then
        modifyYear=`stat $line | grep Modify | awk -F " " '{ print $2 }' | awk -F "-" '{ print $1 }'`
        modifyMonth=`stat $line | grep Modify | awk -F " " '{ print $2 }' | awk -F "-" '{ print $2 }'`
        echo "file: $line                       modifyYear is: $modifyYear modifyMonth is: $modifyMonth"
        realDir="${modifyYear}/${modifyYear}${modifyMonth}/"
        echo "realDir is: $realDir"
        if [ ! -d $realDir ]
        then
            echo "\"$realDir\" dir not exist, ready to mkdir: $realDir"
            mkdir -p $realDir
        elif [ -d $realDir ]
        then
            echo "\"$realDir\" dir exist"
        fi
        mv $line $realDir
    fi
done
相关推荐
sunshine-sm19 分钟前
CentOS Steam 9安装 Redis
linux·运维·服务器·redis·centos
小熊h28 分钟前
MySQL集群高可用架构——组复制 (MGR)
linux·数据库·mysql
棒棒的唐1 小时前
armbian平台ubuntu环境下telnet安装及启动,给pantherX2增加一个应急通道
linux·运维·armbian·telnetd
bug攻城狮1 小时前
CentOS 7 设置静态 IP 地址
linux·tcp/ip·centos
纳切威1 小时前
CentOS 7部署Zabbix5.0
linux·运维·centos·zabbix
sunshine-sm1 小时前
CentOS Steam 9安装 MySQL 8
linux·运维·服务器·数据库·mysql·centos·centos stream
bug攻城狮1 小时前
CentOS 7 快速检查软件包是否已安装的5种方法
linux·运维·centos
DONG9993 小时前
ubuntu 22 安装轻量级桌面Xfce并使用xrdp远程桌面连接
linux·运维·ubuntu
呆萌小新@渊洁3 小时前
linux升级系统,重启出现Minimal BASH-like line editingis supported
linux·服务器
ajassi20003 小时前
开源 C++ QT Widget 开发(十四)多媒体--录音机
linux·c++·qt·开源