Create a directory when its non-existent


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create a directory when its non-existent
# 1  
Old 09-10-2012
Create a directory when its non-existent

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 ----------

Quote:
Originally Posted by murari83.ds
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.
Code:
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}/ \;

Moderator's Comments:
Mod Comment Please view this code tag video for how to use code tags when posting code and data.

Last edited by Corona688; 09-10-2012 at 03:22 PM..
# 2  
Old 09-10-2012
Quote:
Originally Posted by murari83.ds
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.
could you please post the code please?

---------- Post updated at 09:40 PM ---------- Previous update was at 09:38 PM ----------

Where is PROJ_NAME variable getting populated in your script?
# 3  
Old 09-10-2012
PROJ_NAME=dw_icon
# 4  
Old 09-10-2012
Please use code tags when posting code.

Apart from PROJ_NAME unintialized you are missing two dollar signs:
Code:
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:

Code:
if [ ! -d $source_dir ] 
then 
    mkdir $target_dir
else
     echo"already ArchiveFiles folder created"
fi

Which can be consolidated into:
Code:
if ! mkdir  $target_dir 2>/dev/null ; then #the redirection gets rid of error message
    echo "Dir $target_dir exists already"
fi

This User Gave Thanks to mirni For This Post:
# 5  
Old 09-10-2012
Code:
if [ ! -d ${source_dir} ] 
then
mkdir -p ${target_dir}

# 6  
Old 09-10-2012
That's a bit redundant. mkdir -p does that without the if.

So, just mkdir -p /path/to/folder will create a folder if it doesn't exist, and silently do nothing if it already does.
# 7  
Old 09-10-2012
Quote:
Originally Posted by murari83.ds
[CODE]
if [ ! -d {source_dir} ]
then
mkdir -p {target_dir}
else
echo"already ArchiveFiles folder created"
fi
Quote:
Originally Posted by mirni
The else clause of your if statement will never get executed; mkdir -p dir will return 0 whether the directory exists or not.
What you say about mkdir -p is correct, but you misunderstood the code. The else-clause has nothing to do with mkdir, but with the directory test above it.


Quote:
Originally Posted by mirni
Which can be consolidated into:
Code:
if ! mkdir  $target_dir 2>/dev/null ; then #the redirection gets rid of error message
    echo "Dir $target_dir exists already"
fi

It could fail because the directodry already exists, but it could also fail due to permissions, read-only filesystem, full filesystem, ...

Regards,
Alister
This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Create directory and sub-directory with awk and bash

In the below I am trying to create a parent directory using the R_2019 line from f1 if what above it is not empty. I then create sub-directories under each parent if there is a match between $2 of f1 and $2. Inside each sub-folder the matching paths in $3 and $4 in f2are printed. If there is no... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Shell script cannot create directory and move the file to that directory

I have a script, which is checking if file exists and move it to another directory if then mkdir -p ${LOCL_FILES_DIR}/cool_${Today}/monthly mv report_manual_alloc_rpt_A_I_ASSIGNMENT.${Today}*.csv ${LOCL_FILES_DIR}/cool_${Today}/monthly ... (9 Replies)
Discussion started by: digioleg54
9 Replies

3. Shell Programming and Scripting

How Create new directory and move files to that directory.?

Hi All, We have main directory called "head" under this we have several sub directories and under these directories we have sub directories. My requirement is I have to find the SQL files which are having the string "procedure" under "head" directory and sub directories as well. And create... (14 Replies)
Discussion started by: ROCK_PLSQL
14 Replies

4. Shell Programming and Scripting

List files with date, create directory, move to the created directory

Hi all, i have a folder, with tons of files containing as following, on /my/folder/jobs/ some_name_2016-01-17-22-38-58_some name_0_0.zip.done some_name_2016-01-17-22-40-30_some name_0_0.zip.done some_name_2016-01-17-22-48-50_some name_0_0.zip.done and these can be lots of similar files,... (6 Replies)
Discussion started by: charli1
6 Replies

5. Shell Programming and Scripting

Shebang of non-existent interpreter not giving error

I read that whenever you provide wrong path at sha-bang it will generate an error with message "command not found", but when I run script with wrong path, it runs perfectly without generating any error. any reason ? #!/home/usrname/etc echo "hello" exit 0 (4 Replies)
Discussion started by: Qazi
4 Replies

6. Shell Programming and Scripting

Creating a directory if its non-existent within a script

Hi, is there a way to create a directory when its non-existent when trying to move files to this particular folder? Thanks much. (7 Replies)
Discussion started by: ida1215
7 Replies

7. Homework & Coursework Questions

Create script to add user and create directory

first off let me introduce myself. My name is Eric and I am new to linux, I am taking an advanced linux administration class and we are tasked with creating a script to add new users that anyone can run, has to check for the existence of a directory. if the directory does not exist then it has... (12 Replies)
Discussion started by: pbhound
12 Replies

8. UNIX for Advanced & Expert Users

unable to create any directory that uses numbers as the directory name

Version-Release number of selected component (if applicable): ================================================================== root@server # cat /etc/redhat-release Red Hat Enterprise Linux ES release 4 (Nahant Update 5) root@server # uname -a Linux server.integrityserver.net... (16 Replies)
Discussion started by: fed.linuxgossip
16 Replies

9. Shell Programming and Scripting

Checking for future / non existent dates

'm attempting to script an application for the bash shell. The application needs to check for birthday, but must check the birthday to see if the date is a) in the future b) exists at all (ie Feb 29th during non-leap years). The input is being entered in a YYYYMMDD format, so I was hoping someone... (2 Replies)
Discussion started by: DKNUCKLES
2 Replies

10. Shell Programming and Scripting

add or modify if existent

I want to set these params in /etc/system set shmsys:shminfo_shmmax=2000000000 set shmseg:shminfo_shmseg=200 if this param exists, then I want to modify them if not, I want to add them. I can add them using >>/etc/system but how to do the modify thing? at least I can comment the... (4 Replies)
Discussion started by: melanie_pfefer
4 Replies
Login or Register to Ask a Question