Useful Linux shell tricks. Using Brace expansions to create folders.

Posted: April 24, 2017. At: 7:17 PM. This was 7 months ago. Post ID: 3237
Page permalink: http://securitronlinux.com/debian-testing/useful-linux-shell-tricks-using-brace-expansions-to-create-folders/

Now, we must convince Congress to stop the FCC. Can you display an alert?

The Linux command-line offers many useful methods of creating many folders on your computer at once.  This command will create a few folders on your system named one,two,three,four and create the folders five,six,seven,eight within each folder created.

~$ mkdir -p {one,two,three,four}/five/six/seven/eight

This command will create four folders named one,two,three,four and put the folder stuff within each folder.

~$ mkdir -p {one,two,three,four}/stuff

if you have five directories that are named stuff1, stuff2, stuff3 et cetera and you wish to remove all files within these directories, then brace expansions can come to your rescue.

rm -f stuff{1,3}/*

This is another similar example using wild-cards to view only certain files in a folder.

~$ ls Albu*_*.jpg
-rw------- 2 neo neo  12K Mar 29 17:49 AlbumArt_{1C0CD43A-16DE-4593-A7E6-97C2A0C2A76E}_Large.jpg
-rw------- 2 neo neo 2.8K Mar 29 17:49 AlbumArt_{1C0CD43A-16DE-4593-A7E6-97C2A0C2A76E}_Small.jpg
-rw------- 2 neo neo  13K Mar 29 17:49 AlbumArt_{2C1F95BE-3FC6-4923-8473-8E6FB36E488B}_Large.jpg
-rw------- 2 neo neo 2.7K Mar 29 17:49 AlbumArt_{2C1F95BE-3FC6-4923-8473-8E6FB36E488B}_Small.jpg
-rw------- 2 neo neo  10K Mar 17 05:58 AlbumArt_{68ACDAA4-7771-4014-ABBE-418369D5576C}_Large.jpg
-rw------- 2 neo neo 2.6K Mar 29 17:49 AlbumArt_{68ACDAA4-7771-4014-ABBE-418369D5576C}_Small.jpg
-rw------- 2 neo neo  13K Mar 29 17:49 AlbumArt_{7615CCBC-3DE3-4AFA-B981-00EFB1C32601}_Large.jpg
-rw------- 2 neo neo 3.0K Mar 29 17:49 AlbumArt_{7615CCBC-3DE3-4AFA-B981-00EFB1C32601}_Small.jpg
-rw------- 2 neo neo  10K Mar 29 17:49 AlbumArt_{8C29EF88-951E-46E5-AB50-556B0C3B55D2}_Large.jpg
-rw------- 2 neo neo 2.5K Mar 29 17:49 AlbumArt_{8C29EF88-951E-46E5-AB50-556B0C3B55D2}_Small.jpg
-rw------- 2 neo neo 7.5K Mar 29 17:49 AlbumArt_{9F7710F2-539B-4A1B-A2AF-421C208CCDD1}_Large.jpg
-rw------- 2 neo neo 2.8K Mar 29 17:49 AlbumArt_{9F7710F2-539B-4A1B-A2AF-421C208CCDD1}_Small.jpg
-rw------- 2 neo neo 7.0K Mar 29 17:49 AlbumArt_{C6457525-4720-498C-BFF1-D3C87EC78137}_Large.jpg
-rw------- 2 neo neo 1.9K Mar 29 17:49 AlbumArt_{C6457525-4720-498C-BFF1-D3C87EC78137}_Small.jpg
-rw------- 2 neo neo 7.6K Mar 29 17:49 AlbumArt_{CAE2AE03-C9AE-4DF1-873A-7EDC50E41DE1}_Large.jpg
-rw------- 2 neo neo 2.4K Mar 29 17:49 AlbumArt_{CAE2AE03-C9AE-4DF1-873A-7EDC50E41DE1}_Small.jpg
-rw------- 2 neo neo 8.9K Mar 17 05:59 AlbumArt_{D2F0F89D-F691-4D4A-AE4F-FFBC1D11CD9F}_Large.jpg
-rw------- 2 neo neo 2.6K Mar 17 05:59 AlbumArt_{D2F0F89D-F691-4D4A-AE4F-FFBC1D11CD9F}_Small.jpg
-rw------- 2 neo neo 6.1K Mar 29 17:49 AlbumArt_{F7CDDDB4-A774-49E7-B937-D8D658A875EA}_Large.jpg
-rw------- 2 neo neo 2.1K Mar 29 17:49 AlbumArt_{F7CDDDB4-A774-49E7-B937-D8D658A875EA}_Small.jpg
[neo@deusexmachina]:/media/Elements/Music/More>

To use braces in the wildcards, precede them with a backslash.

~$ ls Albu*_\{*.jpg
-rw------- 2 neo neo  12K Mar 29 17:49 AlbumArt_{1C0CD43A-16DE-4593-A7E6-97C2A0C2A76E}_Large.jpg
-rw------- 2 neo neo 2.8K Mar 29 17:49 AlbumArt_{1C0CD43A-16DE-4593-A7E6-97C2A0C2A76E}_Small.jpg
-rw------- 2 neo neo  13K Mar 29 17:49 AlbumArt_{2C1F95BE-3FC6-4923-8473-8E6FB36E488B}_Large.jpg
-rw------- 2 neo neo 2.7K Mar 29 17:49 AlbumArt_{2C1F95BE-3FC6-4923-8473-8E6FB36E488B}_Small.jpg
-rw------- 2 neo neo  10K Mar 17 05:58 AlbumArt_{68ACDAA4-7771-4014-ABBE-418369D5576C}_Large.jpg
-rw------- 2 neo neo 2.6K Mar 29 17:49 AlbumArt_{68ACDAA4-7771-4014-ABBE-418369D5576C}_Small.jpg
-rw------- 2 neo neo  13K Mar 29 17:49 AlbumArt_{7615CCBC-3DE3-4AFA-B981-00EFB1C32601}_Large.jpg
-rw------- 2 neo neo 3.0K Mar 29 17:49 AlbumArt_{7615CCBC-3DE3-4AFA-B981-00EFB1C32601}_Small.jpg
-rw------- 2 neo neo  10K Mar 29 17:49 AlbumArt_{8C29EF88-951E-46E5-AB50-556B0C3B55D2}_Large.jpg
-rw------- 2 neo neo 2.5K Mar 29 17:49 AlbumArt_{8C29EF88-951E-46E5-AB50-556B0C3B55D2}_Small.jpg
-rw------- 2 neo neo 7.5K Mar 29 17:49 AlbumArt_{9F7710F2-539B-4A1B-A2AF-421C208CCDD1}_Large.jpg
-rw------- 2 neo neo 2.8K Mar 29 17:49 AlbumArt_{9F7710F2-539B-4A1B-A2AF-421C208CCDD1}_Small.jpg
-rw------- 2 neo neo 7.0K Mar 29 17:49 AlbumArt_{C6457525-4720-498C-BFF1-D3C87EC78137}_Large.jpg
-rw------- 2 neo neo 1.9K Mar 29 17:49 AlbumArt_{C6457525-4720-498C-BFF1-D3C87EC78137}_Small.jpg
-rw------- 2 neo neo 7.6K Mar 29 17:49 AlbumArt_{CAE2AE03-C9AE-4DF1-873A-7EDC50E41DE1}_Large.jpg
-rw------- 2 neo neo 2.4K Mar 29 17:49 AlbumArt_{CAE2AE03-C9AE-4DF1-873A-7EDC50E41DE1}_Small.jpg
-rw------- 2 neo neo 8.9K Mar 17 05:59 AlbumArt_{D2F0F89D-F691-4D4A-AE4F-FFBC1D11CD9F}_Large.jpg
-rw------- 2 neo neo 2.6K Mar 17 05:59 AlbumArt_{D2F0F89D-F691-4D4A-AE4F-FFBC1D11CD9F}_Small.jpg
-rw------- 2 neo neo 6.1K Mar 29 17:49 AlbumArt_{F7CDDDB4-A774-49E7-B937-D8D658A875EA}_Large.jpg
-rw------- 2 neo neo 2.1K Mar 29 17:49 AlbumArt_{F7CDDDB4-A774-49E7-B937-D8D658A875EA}_Small.jpg
[neo@deusexmachina]:/media/Elements/Music/More>

And another example.

~$ ls Albu*_\{*Small.jpg
-rw------- 2 neo neo 2.8K Mar 29 17:49 AlbumArt_{1C0CD43A-16DE-4593-A7E6-97C2A0C2A76E}_Small.jpg
-rw------- 2 neo neo 2.7K Mar 29 17:49 AlbumArt_{2C1F95BE-3FC6-4923-8473-8E6FB36E488B}_Small.jpg
-rw------- 2 neo neo 2.6K Mar 29 17:49 AlbumArt_{68ACDAA4-7771-4014-ABBE-418369D5576C}_Small.jpg
-rw------- 2 neo neo 3.0K Mar 29 17:49 AlbumArt_{7615CCBC-3DE3-4AFA-B981-00EFB1C32601}_Small.jpg
-rw------- 2 neo neo 2.5K Mar 29 17:49 AlbumArt_{8C29EF88-951E-46E5-AB50-556B0C3B55D2}_Small.jpg
-rw------- 2 neo neo 2.8K Mar 29 17:49 AlbumArt_{9F7710F2-539B-4A1B-A2AF-421C208CCDD1}_Small.jpg
-rw------- 2 neo neo 1.9K Mar 29 17:49 AlbumArt_{C6457525-4720-498C-BFF1-D3C87EC78137}_Small.jpg
-rw------- 2 neo neo 2.4K Mar 29 17:49 AlbumArt_{CAE2AE03-C9AE-4DF1-873A-7EDC50E41DE1}_Small.jpg
-rw------- 2 neo neo 2.6K Mar 17 05:59 AlbumArt_{D2F0F89D-F691-4D4A-AE4F-FFBC1D11CD9F}_Small.jpg
-rw------- 2 neo neo 2.1K Mar 29 17:49 AlbumArt_{F7CDDDB4-A774-49E7-B937-D8D658A875EA}_Small.jpg
1 responses to “Useful Linux shell tricks. Using Brace expansions to create folders.

Reading the LDP Bash documentation is educational about things like this. It sure improved my shell usage, and revealed why a few things worked that I’d been using for years. Recommended.

Leave a Reply