Hi
I need to create a directory when its non-existent
Having an issue with the code here because it doesn't work
can someone point what and how to change, please.
---------- Post updated at 11:08 AM ---------- Previous update was at 11:07 AM ----------
filelist=project_name/files/
source_dir=dw_icon/Temp/
target_dir=dw_icon/Temp/ArchiveFiles
if [ ! -d {source_dir} ]
then
mkdir -p {target_dir}
else
echo"already ArchiveFiles folder created"
fi
find ${filelist} -name "*" -mtime +30 -exec cp {} ${target_dir}/ \;
Apart from PROJ_NAME unintialized you are missing two dollar signs:
filelist=project_name/files/
source_dir=${PROJ_NAME}/Temp/
target_dir=${PROJ_NAME}/Temp/ArchiveFiles"
if [ ! -d ${source_dir} ]
then
mkdir -p ${target_dir}
else
echo"already ArchiveFiles folder created" #will never get here
fi
find ${filelist} -name "*" -mtime +30 -exec cp {} ${target_dir}/ \;
The else clause of your if statement will never get executed; mkdir -p dir will return 0 whether the directory exists or not. You have to omit the -p switch if you want to test on this:
if [ ! -d $source_dir ]
then
mkdir $target_dir
else
echo"already ArchiveFiles folder created"
fi
Which can be consolidated into:
if ! mkdir $target_dir 2>/dev/null ; then #the redirection gets rid of error message
echo "Dir $target_dir exists already"
fi
I'm not sure I understand the logics - if SOURCE_DIR does not exist then create TARGET_DIR? Admittedly the target_dir is contained in source_dir, so when it's created, so is source_dir, but when not so, the echo in the else branch will be wrong, source_dir can exist without Archivefiles.