LOGO

Create Multiple Subdirectories with a Single Linux Command

October 11, 2016
Create Multiple Subdirectories with a Single Linux Command

Creating Multiple Subdirectories in Linux with a Single Command

Typically, constructing a directory structure with several subdirectories in a Linux command line environment necessitates repeated use of the mkdir command. However, a more efficient method exists to accomplish this task.

Consider a scenario where a directory named 'htg' has been established, and the objective is to generate four subdirectories within it. A conventional approach would involve utilizing the mkdir command to initially create the 'htg' directory. Subsequently, the cd command would be employed to navigate into the newly created 'htg' directory. Finally, the mkdir command would be invoked four additional times to produce the desired subdirectories.

This entire process can be consolidated into a single command, streamlining the directory creation process.

Utilizing Brace Expansion

To establish a new directory complete with multiple subdirectories, simply enter the following command at the prompt and execute it (remember to substitute the directory names with your preferred choices):

mkdir -p htg/{articles,images,note,done}

The -p option instructs the mkdir command to first create the parent directory, if it doesn't already exist (in this instance, 'htg'). The terms enclosed within the curly braces constitute a "brace expansion list". Each element within this list is appended individually to the preceding path ('htg/').

For instance, the command above expands to create htg/articles, htg/images, htg/note, and htg/done, effectively generating all four subdirectories under the 'htg' directory.

Applying Brace Expansion to Existing Directories

The brace expansion list can also be utilized with the mkdir command when creating subdirectories within a directory that already exists, as demonstrated below. Here, the 'htg' directory is pre-existing, and the subdirectories are simply added beneath it.

You can also nest brace expansion lists within the mkdir command. For example, if you want to create two subdirectories, 'new' and 'rewrites', within the 'articles' subdirectory under 'htg', the following command can be used:

mkdir -p htg/{articles/{new,rewrites},images,notes,done}

Furthermore, the complete path can be specified, as illustrated in the following example:

mkdir -p ~/Documents/htg/{articles/{new,rewrites},images,notes,done}

This will create the four subdirectories under the 'htg' directory, and then the 'new' and 'rewrites' subdirectories will be created within the 'articles' subdirectory.

Related Resources

Related: How to Make a New Directory and Change to It with a Single Command in Linux

This method offers a concise and efficient way to create complex directory structures in Linux. It's also possible to combine the mkdir command with the cd command to both create a directory and immediately change into it with a single command.

Linux Commands

Files

tar · pv · cat · tac · chmod · grep · diff · sed · ar · man · pushd · popd · fsck · testdisk · seq · fd · pandoc · cd · $PATH · awk · join · jq · fold · uniq · journalctl · tail · stat · ls · fstab · echo · less · chgrp · chown · rev · look · strings · type · rename · zip · unzip · mount · umount · install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · vi · nano · mkdir · du · ln · patch · convert · rclone · shred · srm · scp · gzip · chattr · cut · find · umask · wc · tr

Processes

alias · screen · top · nice · renice · progress · strace · systemd · tmux · chsh · history · at · batch · free · which · dmesg · chfn · usermod · ps · chroot · xargs · tty · pinky · lsof · vmstat · timeout · wall · yes · kill · sleep · sudo · su · time · groupadd · usermod · groups · lshw · shutdown · reboot · halt · poweroff · passwd · lscpu · crontab · date · bg · fg · pidof · nohup · pmap

Networking

netstat · ping · traceroute · ip · ss · whois · fail2ban · bmon · dig · finger · nmap · ftp · curl · wget · who · whoami · w · iptables · ssh-keygen · ufw · arping · firewalld

RELATED: Best Linux Laptops for Developers and Enthusiasts

#linux#command line#subdirectories#mkdir#create directories#shell scripting