【发布时间】:2018-02-16 18:51:18
【问题描述】:
我有数千个采用这种命名格式的文件:
cdr_ABSHCECLUSTER_02_201709072214_987392
我正在使用下面的批处理脚本,但我发现它会根据修改日期重新定位文件,而不是实际创建文件的时间。如何修改它以从文件名中提取年月?
由于文件可以移动,我发现文件可以根据“修改日期”而不是创建日期放在错误的目录中。
stat 显示选项: 使用权 修改的 改变了
for dir in /sftphome/*;
do
echo "Entering parent directory: " $dir
cd $dir;
if [ -d "CDR" ]; then
dirpath="$(pwd)/CDR"
cd $dirpath
echo "Searching CDR directory for files " $dirpath
find . -maxdepth 2 -type f |
while read file ; do
#Check to see if object is a file or directory. Only copy files.
if [[ ! -d $file ]]; then
year="$(date -d "$(stat -c %y "$file")" +%Y)"
month="$(date -d "$(stat -c %y "$file")" +%b)"
#Create the directories if they don't exist. The -p flag makes 'mkdir' create the parent directories as needed
if [ ! -d "$dirpath/$year/$month" ]; then
echo "Creating directory structure $dirpath/$year/$month..."
mkdir -p "$dirpath/$year/$month";
echo "Directory $dirpath/$year/$month created."
fi
echo "Relocating $dirpath/$file to $dirpath/$year/$month"
cp -p $file "$dirpath/$year/$month"
rm -f $file
fi
done
echo "Relocation of all files in $dirpath is complete."
el
如果有任何见解,我将不胜感激。谢谢!
【问题讨论】:
-
看看shell Parameter Expansion 操作符,它可用于使用模式提取变量的一部分。
标签: regex linux bash shell scripting