i am using the below script to zip the files with respect to their timestamp of the files.It is working fine.
For example lets take I have two files in this directory /path/folder1 with the timestamp as this month and previous month. When i run this script first time it is creating two folders under this directory path
also getting the zipped folders of these respectively.
when i un the script second time, again the zipped file
sep_bkp_files.zip is getting zip and and adding with the oct_bkp_files.zip folder.
If no files exixts in path/folder1 then it should come out and should not do anything.
i.e) If there is no files in path/folder1 directory then it should return "No Files exists to backup".
cd /path/folder1
if [ -f *.* ]; then
ls -lrt | awk -v '/^\-/{print $6,$NF}' | while read month filename
do
mkdir -p /path/folder1/${month}_bkp_files
mv ${filename} /path/folder1/${month}_bkp_files
zip -r ${month}_bkp_files.zip ${month}_bkp_files
done
else
echo "No Files exists to backup"
fi
Also please help me if this script can be write some more better way.
cd /path/folder1
if [ -f *.* ]; then
ls -lrt | awk -v '/^\-/{print $6,$NF}' | while read month filename
do
mkdir -p /path/folder1/${month}_bkp_files
mv ${filename} /path/folder1/${month}_bkp_files
zip -r ${month}_bkp_files.zip ${month}_bkp_files
rm ${month}_bkp_files
done
else
echo "No Files exists to backup"
fi
Notice the double quotes. They will make sure even file names with spaces in them will get handled correctly. Try it:
# mkdir testdir
# cd testdir
# touch a; touch b; touch "a b"
# x="a b"
# rm $x # will remove "a" and "b", but not "a b"
# rm "$x" # will remove "a b" but leave "a" and "b" alone
CAUTION: "eval" will eat up the quotes:
# eval rm "$x" # will remove "a" and "b" again, like the unquoted line