Creating a directory if its non-existent within a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating a directory if its non-existent within a script
# 1  
Old 04-07-2012
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.
# 2  
Old 04-07-2012
Code:
mkdir -p ${DIRECTORY}

# 3  
Old 04-07-2012
Thanks skymart. if I have to include this to a script, I just need to add this line or do I need to change something?thanks
# 4  
Old 04-09-2012
Why dont you try it out? Add an if condition first to check that the directory name doesn't exist.

OK
# 5  
Old 04-09-2012
Code:
$ [ ! -d directory ] && mkdir -p directory || echo "mv files directory/"

Remove the echo part after testing ..
# 6  
Old 04-09-2012
@jayan, if there is no directory, then this will create the directory, but then it will not mv the files to it..
# 7  
Old 04-09-2012
My bad Smilie
Code:
$ move="mv file* directory"
$
$ echo $move
mv file* directory
$
$ touch file1 file2 file3 file4 file5
$
$ ls -ltr directory
directory: No such file or directory
$
$ [ ! -d directory ] && mkdir -p directory | eval $move || eval $move
$
$ ls -ltr directory/
total 0
-rw-rw-rw-   1 userid   gid         0 Apr  9 10:15 file5
-rw-rw-rw-   1 userid   gid         0 Apr  9 10:15 file4
-rw-rw-rw-   1 userid   gid         0 Apr  9 10:15 file3
-rw-rw-rw-   1 userid   gid         0 Apr  9 10:15 file2
-rw-rw-rw-   1 userid   gid         0 Apr  9 10:15 file1
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Red Hat

Postfix - want to send email to some non-existent user using alias

Hi, I know how to use email redirection using /etc/aliases file + postfix combination and it is working fine for existing users. The question I have is: I want to send an email to tony@server1.example.com while tony user is actually not there. Rather, I want to redirect that email to... (0 Replies)
Discussion started by: freebird8z
0 Replies

3. Shell Programming and Scripting

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 ---------- filelist=project_name/files/... (7 Replies)
Discussion started by: murari83.ds
7 Replies

4. Programming

Script for creating a directory & move the .tif files in it.

Hi Team, I have thousands of TIF files which are converted from PDF. Below is a sample of it. LH9406_BLANCARAMOS_2012041812103210320001.tif LH9406_BLANCARAMOS_2012041812103210320002.tif LH9406_BLANCARAMOS_2012041812103210320003.tif LH9411_ANGENIAHUTCHINSON_2012041812102510250001.tif... (9 Replies)
Discussion started by: paragnehete
9 Replies

5. UNIX for Dummies Questions & Answers

Need help in creating directory

Hello Experts let say we got a line a file named /user/Oracle/my_catalog/default/root/webcat+backup+testing+07192011.atr now i have to create a directory with name webcat backup testing 07192011 by removing + and removing .atr at the given path...can it be possible Please let me know... (4 Replies)
Discussion started by: aks_1902
4 Replies

6. Shell Programming and Scripting

help needed with creating challenging bash script with creating directories

Hi, Can someone help me with creating a bash shell script. I need to create a script that gets a positive number n as an argument. The script must create n directories in the current directory with names like map_1, map_2 etcetera. Each directory must be contained within its predecessor. So... (7 Replies)
Discussion started by: I-1
7 Replies

7. Shell Programming and Scripting

Creating date directory and moving files into that directory

I have list of files named file_username_051208_025233.log. Here 051208 is the date and 025233 is the time.I have to run thousands of files daily.I want to put all the files depending on the date of running into a date directory.Suppose if we run files today they should put into 05:Dec:08... (3 Replies)
Discussion started by: ravi030
3 Replies

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

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

10. Shell Programming and Scripting

Shell script for Creating Directory with name as system date

Dear Sir/Madam, I need a bit of your help. The problem is as follows : I have to create a directory in unix whose name is that of system date in the dd_mon_yyyy format . I am able to extract a date in required format ina variable , but when i'm using this variable in mkdir it is not... (7 Replies)
Discussion started by: aarora_98
7 Replies
Login or Register to Ask a Question