我想将多个文件从该目录移动(存档)/users/wahasan
到/users/wahasan/old
.归档文件应该自动更新日期。
文件的格式为CHKBOI.pos
, CHKUTI.pos
, CHKSBI.pos
, 等。此处CHK
和.pos
在每个文件名中都很常见。
我需要一个 shell 脚本来将所有文件从源目录移动到带有日期戳的目标。
答案1
纯 BASH 版本:
dir='/users/wahasan';stamp=$(date +%Y%m%d_%H%M%S); for file in $dir/CHK*.pos; do nn=${file/CHK/old/CHK}; nn=${nn/pos/pos_$stamp}; echo "$file ---> $nn";mv $file $nn; done
尝试一下,应该可以
急诊室:
dir='/users/wahasan'; ## path to work directory
stamp=$(date +%Y%m%d_%H%M%S); ## date_time stamp
for file in $dir/CHK*.pos; do ## getting list of all files to be moved
nn=${file/CHK/old/CHK}; ## generating new absolute path [1] (adding '/old/')
nn=${nn/pos/pos_$stamp}; ## generating new absolute path [2] (adding stamp)
echo "$file ---> $nn"; ## showing what will be moved to what (can be removed)
mv $file $nn; ## moving
done