~ / archivo / stackoverflow / 44827965
How to create multiple directories using shell script
-
I am new to shell script, I am trying to create multiple directories using script at specific locations.i have no idea about that, i am giving here what i have tried so far ,
directory_name="/home/usw9bls7/lib1" if [ -d $directory_name ] then echo "Directory already exists" else mkdir $directory_name fi I have to create directory at locations "/home/usw9bls7/config1" "/home/usw9bls7/DNQ/lib1" "/home/usw9bls7/DNQ/config1"Plesae help
- Jeet 2017-06-29 14:31 @ImAtWar please check my edited post
- Jean-Baptiste Yunès 2017-06-29 14:54 use '-p' as option to
mkdirto create missing intermediate directories. - jww 2017-06-29 20:43 Possible duplicate of How to create nonexistent subdirectories recursively using Bash?
- Miguel Ortiz yo 2017-06-29 23:25 Hi Mandrek, consider using metacharacters and --parents of mkdir to simplify things, I've provided you an example, hope it helps to your purpose.
-
Don't use "if" statements for such simple task, use "-p, --parents" of mkdir to create several directories that doesn't exist and ignore existing ones.
Combine that capability using metacharacters to expand subfolders creations.
In this example I've created the next structure with one command:
-/home |---/usw9bls7 |------- DNQ |---- lib1 |---- config1 user@DESKTOP-RR909JI ~ $ mkdir -p /home/usw9bls7/DNQ/{lib1,config1} user@DESKTOP-RR909JI ~ $ ls -ltr /home/usw9bls7/DNQ/ total 0 drwxr-xr-x+ 1 user user 0 jun. 29 20:17 lib1 drwxr-xr-x+ 1 user user 0 jun. 29 20:17 config1None of these directories existed before in my environment (except for /home). Anyway if you want to create the script try simplifying things with this.
- jpaugh 2017-06-29 23:29 You didn't have a
/homefolder? Gasp
- jpaugh 2017-06-29 23:29 You didn't have a
-
P.... respuesta 2017-06-29 14:39
Using brace expansion if you use
bash:directory_name="/home/usw9bls7/lib1" if [ -d "$directory_name" ];then echo "Directory already exists" else mkdir -p /home/usw9bls7/{config1,DNQ/lib1,DNQ/config1}/"$directory_name" fiExample: with
echocommand.echo /home/usw9bls7/{config1,DNQ/lib1,DNQ/config1} /home/usw9bls7/config1 /home/usw9bls7/DNQ/lib1 /home/usw9bls7/DNQ/config1- Jeet 2017-06-29 14:41 getting : command not found ./test.sh: line 8: syntax error near unexpected token
fi' ./test.sh: line 8:fi' - ImAtWar 2017-06-29 14:47 Your line endings may be corrupted.
- P.... 2017-06-29 14:48 give output of
echo $SHELLand try adding#!/bin/bashto first line of your script. - Jeet 2017-06-29 15:01 @PS now giving -bash: ./test.sh: /bin/ksh^M: bad interpreter: No such file or directory
- Jeet 2017-06-29 14:41 getting : command not found ./test.sh: line 8: syntax error near unexpected token
-
Adam Katz respuesta 2017-06-29 22:46
Portable POSIX solution (works in /bin/ksh, /bin/sh, and /bin/bash)
#!/bin/sh parent_directory="/home/usw9bls7/lib1" for directory_name in config1 DNQ/lib1 DNQ/config1 do if [ -d "$parent_directory/$directory_name" ] then echo "Directory already exists" else mkdir -p "$parent_directory/$directory_name" fi doneThis merely loops over your given names and then makes them. I added the
-poption so that it will silently create missing parents like DNQ as needed.Note that if these exist as files rather than directories, you'll get errors from mkdir. At least you'll get something.
-
Petr Skocik respuesta 2017-06-29 23:47
Pass them all to
mkdir -p:mkdir -p \ "/home/usw9bls7/config1" \ "/home/usw9bls7/DNQ/lib1" \ "/home/usw9bls7/DNQ/config1"If you're super performance-conscious, you can test them for existence from the shell first. Here's what I'm doing in my shell lib:
all_eh() { local predic a; predic=$1; shift for a; do $predic "$a" || return 1 done } mkdir_p() { all_eh 'test -d' "$@" || mkdir -p "$@"; }This is faster if all the directories exist because
test(or[) is a shell builtin (in practically all shells) and so doesn't cost you the usual 1-2ms fork/exec overhead.