bash script for testing existence of files/folders and creating if neither exist


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash script for testing existence of files/folders and creating if neither exist
# 1  
Old 02-06-2011
Question [Solved] bash script for testing existence of files/folders and creating if neither exist

Hi,

I am new to shell-scripting, and doing a lot of reading. I am having some trouble getting started with a simple testing of scripting. I have been experimenting with if, loops, for, test, etc., but still unsure. I seem to have the hang of it when it comes to creating a single file or directory; but not when it comes to multiple files, directories, and sub-directories. Would like some assistance on evaluating whether files and/or directories exist, if so, then echo "File or folder already exist", if not, then create the files (touch) and/or directories (mkdir). I would like the script for the following directory-tree:
Code:
/boo
/boo/boo_file
/boo/dir1
/boo/dir1/file1
/boo/dir2
/boo/dir2/file1
/doo
/doo/dir1
/doo/dir1/file1
/doo/dir2
/doo/dir2/file1
/doo/doo_file
/foo
/foo/dir1
/foo/dir1/file1
/foo/dir2
/foo/dir2/file1
/foo/foo_file


|-- boo
|   |-- boo_file
|   |-- dir1
|   |   `-- file1
|   `-- dir2
|       `-- file1
|-- doo
|   |-- dir1
|   |   `-- file1
|   |-- dir2
|   |   `-- file1
|   `-- doo_file
|-- foo
|   |-- dir1
|   |   `-- file1
|   |-- dir2
|   |   `-- file1
|   `-- foo_file


Last edited by Scott; 02-06-2011 at 05:59 PM.. Reason: Code tags
# 2  
Old 02-06-2011
Code:
mkdir -p /boo /boo/dir1 /boo/dir2 /doo/dir1 /doo/dir2 /foo /foo/dir1 /foo/dir2
touch /boo/boo_file /boo/dir1/file1 /boo/dir2/file1 /doo/dir1/file1 /doo/dir2/file1 /doo/doo_file /foo/dir1/file1 /foo/dir2/file1/foo/foo_file

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 02-06-2011
No need to mkdir -p /foo if you already mkdir -p /foo/dir1 as the -p option creates parents.

bash code:
  1. #!/bin/bash
  2. for d1 in boo doo foo; do
  3.     for d2 in dir1 dir2; do
  4.         mkdir -p /$d1/$d2
  5.         touch /$d1/$d2/file1
  6.     done
  7.     touch /$d1/${d1}_file
  8. done
This User Gave Thanks to frans For This Post:
# 4  
Old 02-06-2011
Hi,

Thank you for your swift response. I ran the script w/o the -p for the mkdir command and received the following:
Code:
mkdir: cannot create directory `/boo/dir1': No such file or directory
touch: cannot touch `/boo/dir1/file1': No such file or directory
mkdir: cannot create directory `/boo/dir2': No such file or directory
touch: cannot touch `/boo/dir2/file1': No such file or directory
touch: cannot touch `/boo/boo_file': No such file or directory
mkdir: cannot create directory `/doo/dir1': No such file or directory
touch: cannot touch `/doo/dir1/file1': No such file or directory
mkdir: cannot create directory `/doo/dir2': No such file or directory
touch: cannot touch `/doo/dir2/file1': No such file or directory
touch: cannot touch `/doo/doo_file': No such file or directory
mkdir: cannot create directory `/foo/dir1': No such file or directory
touch: cannot touch `/foo/dir1/file1': No such file or directory
mkdir: cannot create directory `/foo/dir2': No such file or directory
touch: cannot touch `/foo/dir2/file1': No such file or directory
touch: cannot touch `/foo/foo_file': No such file or directory

When I ran it with the -p I received:
Code:
mkdir: cannot create directory `/boo': Permission denied
touch: cannot touch `/boo/dir1/file1': No such file or directory
mkdir: cannot create directory `/boo': Permission denied
touch: cannot touch `/boo/dir2/file1': No such file or directory
touch: cannot touch `/boo/boo_file': No such file or directory
mkdir: cannot create directory `/doo': Permission denied
touch: cannot touch `/doo/dir1/file1': No such file or directory
mkdir: cannot create directory `/doo': Permission denied
touch: cannot touch `/doo/dir2/file1': No such file or directory
touch: cannot touch `/doo/doo_file': No such file or directory
mkdir: cannot create directory `/foo': Permission denied
touch: cannot touch `/foo/dir1/file1': No such file or directory
mkdir: cannot create directory `/foo': Permission denied
touch: cannot touch `/foo/dir2/file1': No such file or directory
touch: cannot touch `/foo/foo_file': No such file or directory

Thanks again.

Last edited by Scott; 02-06-2011 at 05:59 PM.. Reason: Code tags
# 5  
Old 02-06-2011
-p is required, yes, or I wouldn't have put it there.

if you already have a file named 'boo', it will fail. delete it.
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 02-06-2011
Quote:
Originally Posted by me2
Hi,

Thank you for your swift response. I ran the script w/o the -p for the mkdir command and received the following:

mkdir: cannot create directory `/boo/dir1': No such file or directory
touch: cannot touch `/boo/dir1/file1': No such file or directory
mkdir: cannot create directory `/boo/dir2': No such file or directory
touch: cannot touch `/boo/dir2/file1': No such file or directory
touch: cannot touch `/boo/boo_file': No such file or directory
mkdir: cannot create directory `/doo/dir1': No such file or directory
touch: cannot touch `/doo/dir1/file1': No such file or directory
mkdir: cannot create directory `/doo/dir2': No such file or directory
touch: cannot touch `/doo/dir2/file1': No such file or directory
touch: cannot touch `/doo/doo_file': No such file or directory
mkdir: cannot create directory `/foo/dir1': No such file or directory
touch: cannot touch `/foo/dir1/file1': No such file or directory
mkdir: cannot create directory `/foo/dir2': No such file or directory
touch: cannot touch `/foo/dir2/file1': No such file or directory
touch: cannot touch `/foo/foo_file': No such file or directory

When I ran it with the -p I received:

mkdir: cannot create directory `/boo': Permission denied
touch: cannot touch `/boo/dir1/file1': No such file or directory
mkdir: cannot create directory `/boo': Permission denied
touch: cannot touch `/boo/dir2/file1': No such file or directory
touch: cannot touch `/boo/boo_file': No such file or directory
mkdir: cannot create directory `/doo': Permission denied
touch: cannot touch `/doo/dir1/file1': No such file or directory
mkdir: cannot create directory `/doo': Permission denied
touch: cannot touch `/doo/dir2/file1': No such file or directory
touch: cannot touch `/doo/doo_file': No such file or directory
mkdir: cannot create directory `/foo': Permission denied
touch: cannot touch `/foo/dir1/file1': No such file or directory
mkdir: cannot create directory `/foo': Permission denied
touch: cannot touch `/foo/dir2/file1': No such file or directory
touch: cannot touch `/foo/foo_file': No such file or directory

Thanks again.
It fails because you are trying to create those in `/' and you are not root.
Remove the `/' in front of directories in the script
This User Gave Thanks to Aia For This Post:
# 7  
Old 02-06-2011
Great information. Thanks everyone. Will report back.

---------- Post updated at 05:30 PM ---------- Previous update was at 04:57 PM ----------

Thank again everyone. It worked. Please close.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need BASH Script Help to Move Files While Creating Directories

I've got this script to loop through all folders and move files that are more than 2 years old. I'm using the install command because it creates the necessary directories on the destination path and then I remove the source. I'd like to change the script to use the mv command since it is much... (4 Replies)
Discussion started by: consultant
4 Replies

2. Shell Programming and Scripting

Bash to move specific files from folders in find file

I have a directory /home/cmccabe/nfs/exportedReports that contains multiple folders in it. The find writes the name of each folder to out.txt. A new directory is then created in a new location /home/cmccabe/Desktop/NGS/API, named with the date. What I am trying to do, unsuccessfully at the moment,... (7 Replies)
Discussion started by: cmccabe
7 Replies

3. Shell Programming and Scripting

Bash to download specific files and save in two folders

I am trying to download all files from a user authentication, password protected https site, with a particular extension (.bam). The files are ~20GB each and I am not sure if the below is the best way to do it. I am also not sure how to direct the downloaded files to a folder as well as external... (7 Replies)
Discussion started by: cmccabe
7 Replies

4. UNIX for Advanced & Expert Users

Help with creating script to delete log files/folders

Hi I am new to Linux / scripting language. I need to improve our Linux servers at work and looking to claim some space my deleting log files/ folders on a 5 day basis. Can someone help me with creating a script to do so. Any sample script will be helpful.:b: Regards (2 Replies)
Discussion started by: sachinksl
2 Replies

5. Shell Programming and Scripting

check if multiple folders exist

I want to check if some directories with common prefix exist under current directory with bash, say, I have dictories like: dirct_1 dirct_2 dirct_3 ... in the current directory. I did: if then echo " directories exist " else echo " directories not exist " fi (3 Replies)
Discussion started by: cristalp
3 Replies

6. Shell Programming and Scripting

Creating folders based on number of files

I have hundreds of files numbered in consecutive number in one single folder What I would like to do is to make as many subfolders as needed (dependeing on the number of individual files) and name them Folder01, Folder02, etc. Then, move file01 to folder01, file02 to folder02 so on and so... (3 Replies)
Discussion started by: Xterra
3 Replies

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

8. Shell Programming and Scripting

moving multiple folders/files in subversion using bash script

Hi, I'm new here an dlearning a lot from this forum. i didnt find any solution for this in the forum. I have already checked in folders in subversion named HTT01,... HTT21.. and have files in each folder like below: HTT01/HTT01_00000.hex HTT01/HTT01_00000_fb_result.hex... (2 Replies)
Discussion started by: ravishan21
2 Replies

9. UNIX for Dummies Questions & Answers

testing if files exist

I am trying to test arguments to see if they are files in any directory. I have : but it's not working (7 Replies)
Discussion started by: skooly5
7 Replies

10. UNIX for Dummies Questions & Answers

Testing existence of a file /directory

hey guys How can i test existence of a file /directory in a directory in a script thanks (2 Replies)
Discussion started by: ajaya
2 Replies
Login or Register to Ask a Question