Monday, May 11, 2020

Handy mkdir tip!

Greetings fellow nerds!!!

I thought I would make a short post with a quick tip for you.  I recently discovered this while building Linux From Scratch.  I noticed this syntax being used with the mkdir command, and have found it extremely useful for one line set up of directories for development.

You can create multiple directories from one mkdir command.  I strongly suggest using the -v switch for verbose output so you can see the results of all the directory makes to make sure everything work properly.  So the trick is to use the curly braces {} and you can list comma-separated directories.

So for instance when I've initially created a directory for a dotnet project, here's what I do for making all my required subdirectories.

mkdir -v {Controllers,Models,Views,Views/{Home,Shared},wwwroot,wwwroot/{css,js}}

Notice you can nest the curly braces.  So this creates 4 folders under the current directory: Controllers, Models, Views, and wwwroot.  It also creates 3 subdirectories under Views: Home and Shared, and finally 2 subdirectories under wwwroot; css and js.

Bonus Tip: There is a way to continue a command in bash. You leave a space and then the \ character followed by a new line.  If you are writing this in a shell script it can make things easier to read.

So in this example:

mkdir -v {Controllers, \
          Models, \
          Views, \
          Views/{Home, \
                 Shared},
          wwwroot, \
          wwwroot/{css, \
                   js}}

Very powerful, and time-saving approach to your Gnu/Linux use!!!

Ciao for now!!!

Nerdtek

No comments: