我每天都会创建很多名为test_file_201701011512000.dat的文件。我需要将这些文件日期或月份目录移动,如果未创建目录,则相应地创建。请建议如何去做。
答案 0 :(得分:0)
您可以这样做,假设您的所有文件都在同一目录中。文件被移动到dest / year / month目录:
#!/bin/bash
dest_dir=_your_destination_directory_
# pattern to grab 4 digits of year and 2 digits of month
file_pattern="_([[:digit:]]{4})([[:digit:]]{2})"
for file in test_file_*; do
[[ ! -f $file ]] && continue # look at regular files only
if [[ $file =~ $file_pattern ]]; then
year="${BASH_REMATCH[1]}"
month="${BASH_REMATCH[2]}"
destination_dir="$dest_dir/$year/$month"
[[ ! -d $destination_dir ]] && mkdir -p "$destination_dir"
echo "Moving $file to $destination_dir"
mv "$file" "$destination_dir"
fi
done