Creating directory in bulk using mkdir in shell script
Solution 1
Apart from the invalid path in the hashbang, which is the cause of the error you get, you also have a problem with the mkdir
command itself.
Remember that globs only work to list existing files, so anything that looks like mkdir /tmp/foo*
will just pass existing directories as arguments to mkdir
. If /tmp/foo1
exists, it will be passed to mkdir
, which will then complain that it already exists. If /tmp/foo2
doesn't exist, it will not be passed to mkdir
, and will not be created.
You'll have to do something else to create new directories.
It's unclear what directories you want to create, but assuming you want to create a directory testdoank
under every directory matching /tmp/report*/testfolder*
, then something like this might do:
for dir in /tmp/report*/testfolder*; do
mkdir "$dir"/testdoank
done
Solution 2
Due to the original formatting of your question, it looked like you were missing the '#' character in your shell declaration. However, the actual error was caused by a missing / in the path of bash.
This is the proper way to determine your shell in a bash script:
#!/bin/bash
Solution 3
The shell script must start with #!
, not simply !
.
Related videos on Youtube
heff
Updated on September 18, 2022Comments
-
heff almost 2 years
I am trying to create directory in bulk using mkdir through shell script below is the script
#!bin/bash mkdir /tmp/report*/testfolder*/testdoank
when i run it i got an error
-bash: ./makefolder.sh: bin/bash: bad interpreter: No such file or directory
what is wrong?
-
Stephen Kitt over 4 years@ajgringo619 that’s the correct answer, please write it up as an answer rather than a comment.
-
-
Stephen Kitt over 4 yearsThat was a formatting problem in the question.
-
heff over 4 yearsthanks this is what im looking for
-
Kusalananda over 4 years@heff Good! If this solves your issue, please consider "accepting" the answer. This is the best way to show gratitude on this site. Accepting an answer not only marks the question as resolved, but also signals to future readers that the accepted answer actually solved the issue. More information about this is available here: unix.stackexchange.com/help/someone-answers