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:

/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
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
1 Like

No need to mkdir -p /foo if you already mkdir -p /foo/dir1 as the -p option creates parents.

#!/bin/bash
for d1 in boo doo foo; do
for d2 in dir1 dir2; do
mkdir -p /$d1/$d2
touch /$d1/$d2/file1
done
touch /$d1/${d1}_file
done

1 Like

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.

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

1 Like

It fails because you are trying to create those in `/' and you are not root.
Remove the `/' in front of directories in the script

1 Like

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.