Visit Our UNIX and Linux User Community


Create Directory structure


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create Directory structure
# 1  
Old 03-28-2012
Create Directory structure

Hello ; ) again

Now I have my file like this :

Code:
DIR2/DIR3
DIR2
DIR2/DIR3/DIR4/DIR5

I am looking for help to create a loop that will create the directory structure.

I need something like this :

Code:
If "DIR2" does not exist > Create 
IF "DIR2" exist already > check if onther "DIR" needs to be created
IF not > GO NEXT STEP of the script
otherwise
create "DIR3" and so on

Thanks for your help ....
# 2  
Old 03-28-2012
mkdir

Quote:
Originally Posted by Aswex
Hello ; ) again

Now I have my file like this :

Code:
DIR2/DIR3
DIR2
DIR2/DIR3/DIR4/DIR5

I am looking for help to create a loop that will create the directory structure.

I need something like this :

Code:
If "DIR2" does not exist > Create 
IF "DIR2" exist already > check if onther "DIR" needs to be created
IF not > GO NEXT STEP of the script
otherwise
create "DIR3" and so on

Thanks for your help ....
You can use
Code:
mkdir -p DIR2/DIR3/DIR4/DIR5

.
or else you can loop around this command for all the input.

You dont need to check the directory and create the directory.
-p option create dir not exist else do nothing.
Then do your operations.
This is just a hint.

Cheers,
RangaSmilie
This User Gave Thanks to rangarasan For This Post:
# 3  
Old 03-28-2012
Thanks a lot Ranga for your help,

I will try your hint in my script ... thank you so much ...I will let you know ; )
# 4  
Old 04-02-2012
Hello,

Just for you to know the command
Code:
mkdir -p

is working great.

But I need help to create a function with a condition. Here is what I am trying to do :

Code:
ControleDir () {
for directory in `cat ${fic_cpt_arbo}` ;
do
    mkdir -p ${nfs_depot}/${Directory}
done

But I need to add two condition in my function that will do :

If the directories were create, so continue with message like : "OK" ... If not exit with a warning like "ERROR.

Thanks for your help !
# 5  
Old 04-02-2012
There's a useless use of cat in your code, and variablenames are case-sensitive. For the messages you can use the && and || operators (tested with bash-shell).
Code:
ControleDir () {
   while read directory
   do
      mkdir -p ${nfs_depot}/${directory} && echo "OK" || (echo "ERROR"; exit 1)
   done <${fic_cpt_arbo}
}

This User Gave Thanks to cero For This Post:
# 6  
Old 04-03-2012
Cero, thanks for your help.

Previous Thread | Next Thread
Test Your Knowledge in Computers #644
Difficulty: Easy
The first web browser was called WorldWideWeb.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Create a flat file and directory structure

Hi All, is there any work around to generate the file and directory structure like below at left side at Output? and exclude all file except .abc .txt Current Directory structure |-------------files |---------------Share |-----------------dir1 |-----------------dir2... (11 Replies)
Discussion started by: heros
11 Replies

2. Shell Programming and Scripting

How we can create the master file through shell to show the tree structure of the directory?

Can we create the master file that show the whole tree structure of the directory till a particular folder? Database that contains four sub repository Sybase,sql,oracle,mysql and sql and oracle contains two subrepostories Siebel and plsql and each repositories contains three folders... (1 Reply)
Discussion started by: rohit22hamirpur
1 Replies

3. UNIX for Dummies Questions & Answers

find mv and create directory structure

Hi there, I'm trying to pull all my flacs out of my Music collection. I can do it with following command find b/ -name *.flac -exec mv {} flac/ \; which works great except it moves all the flac files to the flac folder. I want it to recreate the original folder the flacs were found in and mv... (8 Replies)
Discussion started by: fistikuffs
8 Replies

4. Shell Programming and Scripting

How to create a C structure using table description.

There is table 'DEPT' in the database with the following desciption: Name Null? Type ------- -------- ------------------------ DEPTNO NOT NULL NUMBER(2) DNAME NULL VARCHAR2(14) LOC NULL VARCHAR2(13) Using shell script, I need to create a structure for the... (2 Replies)
Discussion started by: ehari
2 Replies

5. UNIX for Dummies Questions & Answers

create tar archive without preserving directory structure?

I am adding some individual files to a tar archive and would like them to be added to the archive without any directory hierarchy, even though the files themselves exist in levels of hierarchy. Unfortunately, tar seems to always preserve the directory hierarchy when it adds the files. Here is... (2 Replies)
Discussion started by: Special_K
2 Replies

6. Shell Programming and Scripting

How to create a directory structure with getting input from a file.

Hi How to create a directory structure with getting input from a file. I have file in that following lines are written. ./activemq-4.1.2/activemq-core-4.1.2.jar ./activemq-4.1.2/backport-util-concurrent-2.1.jar ./camel-1.4.0/apache-camel-1.4.0.jar ./camel-1.4.0/lib/activation-1.1.jar... (12 Replies)
Discussion started by: itsjoy2u
12 Replies

7. Shell Programming and Scripting

Help to Create this file format structure

This data comes form the table and exported into the file in this format File1 Format weboffercode1,sourcecode1,1,1,1,1,1,1 weboffercode1,sourcecode2,1,1,1,1,1,1 weboffercode1,sourcecode1,1,1,1,1,1,1 weboffercode1,sourcecode3,1,1,1,1,1,1 weboffercode1,sourcecode3,1,1,1,1,1,1 ... (4 Replies)
Discussion started by: enigma_83
4 Replies

8. UNIX for Advanced & Expert Users

MV files from one directory structure(multiple level) to other directory structure

Hi, I am trying to write a script that will move all the files from source directory structure(multiple levels might exist) to destination directory structure. If a sub folder is source doesnot exist in destination then I have to skip and goto next level. I also need to delete the files in... (4 Replies)
Discussion started by: srmadab
4 Replies

9. UNIX for Dummies Questions & Answers

Copying a Directory Structure to a new structure

Hi all Is it possible to copy a structure of a directory only. e.g. I have a file with the following entries that is a result of a find :- /dir1/dir2/file.dbf /dir1/dir2/dir3/file1.dbf /dir1/file.dbf I want to copy these to a directory and keep the structure however starting at a new dir... (8 Replies)
Discussion started by: jhansrod
8 Replies

Featured Tech Videos