LINUX Write a script that takes the path of a directory as the command line argu
ID: 3796329 • Letter: L
Question
LINUX
Write a script that takes the path of a directory as the command line argument and finds all the files that have read permission set on the ‘user, and then copies them to a folder which is named with the current date. For example if you perform this operation on 2017-02-16 than the folder name should be backup_2017-02-16. Your program should check if the folder already exists and ask the user if he/she wants to back up again and take the action of copying or not copying the files again based on users input. Once all the files are copied to the folder your script should use bzip2 utility to compress them.
Explanation / Answer
#!/bin/bash
if [ -d $1 ];
then
echo $1
val=`ls -l $1|grep "^-r"|tr -s " " " "|cut -d" " -f9`
fi
dd=`date "+%m-%d-%y"`
mydir=$HOME/backup_$dd
if [ -d $mydir ];
then
echo "$mydir exists. Do you want to replace it(Y/n)?"
read ip
if [ "$ip" == "Y" ];
then
rm -rf $mydir
mkdir -p $mydir
fi
else
mkdir $mydir
fi
for file in $val
do
cp $file $mydir
done
tar cf $mydir.tar $mydir
bzip2 $mydir.tar
Output:
/home/naresh/Chegg
/home/naresh/backup_02-22-17 exists. Do you want to replace it(Y/n)?
Y
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.