Hi All,
As I m very new for Unix, I need to check for a directory and move a file. If Directory is not found, The script should create a directory and move the file. Can any one help here.
Hi All,
As I m very new for Unix, I need to check for a directory and move a file. If Directory is not found, The script should create a directory and move the file. Can any one help here.
dir="/home/somedirectory"
if [ -d $dir ]
then
echo "The directory $dir exists"
cp pathoffiletocopy .
else
mkdir -p $dir
cd $dir
cp pathoffiletocopy .
fi
try somethink like this. I did not test it but logic should be like this.
Thanks
May I improve this?
I also did not test it, but I noticed this two things at first sight
semicolon worked as a command separator. if you have already wrote the command on the new line. no need of that.
if [ -d $dir ]; then # must need
if [ -d $dir ] # not needed
then
anchal_khare, that's right, thanks! I'm glad I learned something new respectively got reminded
the minimal
dir="/home/somedirectory"
[ -d $dir ] || mkdir $dir
cp pathoffiletocopy $dir/
Not necessary to check the existence of the directory if you use the -p option:
dir="/home/somedirectory"
mkdir -p $dir
cp pathoffiletocopy $dir
Remember, if you are going to use
mkdir -p $dir
make sure that you have an absolute path for $dir
dir="/home/somedirectory"
NOT
dir="./somedirectory"
Or you could be building directory trees all over the place!