我有一个FTP过程,该过程不断将文件放入目录中。创建的日期是文件名的一部分,格式如下:
YYYY-MM-DD-HH-MM-SS-xxxxxxxxxx.wav
我想根据文件创建的日期将文件移动到另一个目录。我可以使用文件名或日期戳,以较容易的一种为准。仅需要考虑月份和年份。我已使用以下格式创建目录:
Jan_2016
Feb_2016
我一直在创建目录并手动移动文件,但是我想使用bash脚本自动执行该操作,如果该目录不存在,它将创建该目录。
到目前为止,我一直在手动创建目录,然后运行以下命令:
mv ./2016-02*.wav Feb_2016 /
### capitalization is important. Space separated.
### Null is a month 0 space filler and has to be there for ease of use later.
MONTHS=(Null Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
cd /your/ftp/dir ### pretty obvious I think
for file in *.wav ### we are going to loop for .wav files
do ### start of your loop
### your file format is YYYY-MM-DD-HH-MM-SS-xxxxxxxxxx.wav so
### get the year and month out of filename
year=$(echo ${file} | cut -d"-" -f1)
month=$(echo ${file} | cut -d"-" -f2)
### create the variable for store directory name
STOREDIR=${year}_${MONTHS[${month}]}
if [ -d ${STOREDIR} ] ### if the directory exists
then
mv ${file} ${STOREDIR} ### move the file
elif ### the directory doesn't exist
mkdir ${STOREDIR} ### create it
mv ${file} ${STOREDIR} ### then move the file
fi ### close if statement
done ### close the for loop.
对于没有经验的人来说,这应该是一个很好的起点。尝试根据这些说明和命令编写脚本。如果遇到问题,可以寻求帮助
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句