第一个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