简体   繁体   中英

Shell Script mkdir if not and mv files to each day

I need to check in a dir that have 150.000 zip files with this kind of format :

log_from_2012_08_14-11:57:12_To_2012_08_14-13:57:12.zip
log_from_2012_08_14-13:57:12_To_2012_08_14-15:57:12.zip
log_from_2012_08_14-15:57:12_To_2012_08_14-17:57:12.zip

if there is a dir with the name equals to the day of the file :

2012_08_14 

then move all files that exist from that From_2012_08_14 to that dir, if not create it

( mkdir date +%Y_%M_%D )

and then check for each day since august until today november , every file from a day that doenst have is own dir create it and move it there.

cd /relevant/log/directory

# Create the necessary sub-directories
ls *from_????_??_??-??:??:??_To_????_??_??-??:??:??.zip |
sed 's/.*from_\([0-9]\{4\}_[0-9][0-9]_[0-9][0-9]\).*/\1/' |
sort -u |
xargs mkdir -p

# Move the files into the sub-directories
ls *from_????_??_??-??:??:??_To_????_??_??-??:??:??.zip |
sed 's/.*from_\([0-9]\{4\}_[0-9][0-9]_[0-9][0-9]\).*/mv & \1;/' |
sh -x

The -x is optional, but shows you what is happening while it is happening. I'm assuming that none of your file names contain spaces or newlines. I'm also assuming there aren't files with alphanumerics where the ? in the shell patterns is expected to match digits. You can expand each ? to [0-9] if you have to be utterly paranoid, or revise the sed scripts to discard names that don't match, etc.

Quick and dirty Bash solution

for file in log_from*.zip; do
    dirname=${file#log_from_}
    dirname=${dirname%%-*}
    [ ! -d "$dirname" ] && mkdir "$dirname"
    mv "$file" "$dirname"
done 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM