Suppose I have some directory, which contain a number of subdirectories, and in each of those subdirectories I want to create a directory with the same name:
./dir-1
./dir-2
...
./dir-n
I want to do mkdir */new-dir
but this throws an error. What's the best way of going about this?
To create a directory in MS-DOS or the Windows Command Prompt (cmd), use the md or mkdir MS-DOS command. For example, below, we are creating a new directory called "hope" in the current directory. You can also create multiple new directories in the current one with the md command.
Simply hold down the Shift key and click with the right mouse button in the Explorer on the folder where you want to create additional subfolders.
You can create directories one by one with mkdir, but this can be time-consuming. To avoid that, you can run a single mkdir command to create multiple directories at once. To do so, use the curly brackets {} with mkdir and state the directory names, separated by a comma.
MS-DOS command is used to create a subdirectory is MKDIR. A subdirectory is a directory which is located within another directory.
for dir in $(ls); do
mkdir "$dir/new-dir"
done
find . -type d | xargs -I "{x}" mkdir "{x}"/new-dir
If you want to reduce it to the first level of directories use
find . -maxdepth 1 -type d | xargs -I "{x}" mkdir "{x}"/new-dir
Amazing that this question never obtained a sane answer:
shopt -s nullglob
for i in */; do
mkdir -- "${i}newdir"
done
shopt -s nullglob
so that the glob expands to nothing if there are no matches.--
in mkdir
to mark the end of options (if there's a directory with a name starting with a hyphen, doesn't confuse mkdir
trying to interpret it as an option).This silently ignores the hidden directories. If you need to perform this operation on hidden directories, just replace the line shopt -s nullglob
by the following:
shopt -s nullglob dotglob
The dotglob
so that the globs also consider hidden files/directories.
If you want only one invocation of mkdir
:
shopt -s nullglob
dirs=( */ )
mkdir -- "${dirs[@]/%/newdir}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With