I'm not an expert bash, but I try solve that problem:
*.log
, which I don't know creation date, $date_when_was_file_created
eg. files created 2017-01-12
have to be moved to directory called 2017-01-12
files created 2017-02-12
have to be moved to directory called 2017-02-12
So what I have:
#!/bin/bash
dirs=`ls -la -F --full-time *.LOG | awk '{print $6}'`
filedate=`ls -la -F --full-time *.LOG | awk '{print $6}'>dat`
mkdir $dirs
for data in `cat dat`
do echo " ther are file to be copied:--> `ls -la -F --full-time *.LOG | awk '{print $6 " " $9}' | grep $data | awk '{print $2}'` "
if [[ $data == $dirs ]];
then cp `ls -la -F --full-time *.LOG | awk '{print $6 " " $9}' | grep $data | awk '{print $2}'` $dirs
fi
done
But it doesn't work, mean creates directories, but don't copy files, why? I don't know... Any suggestions please?
Both POSIX shell and bash provide parameter expansions to trim portions of a string. The stat
utility can provide the full create time (including hours, minutes, seconds...). Simply use the stat
function and trim the time from the string create time returned by stat
leaving the date. Then just create a directory using the date and move your log file into it. Eg
for file in *.log ## loop over log files
do
ctime=$(stat -c%y "$file") ## get full create time
cdate="${ctime%% *}" ## extract YYYY-MM-DD date
mkdir -p "$cdate" ## create dir if it doesn't exist
mv "$file" "$cdate" ## move log to directory
done
Look it over and let me know if you have further questions.
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.