Script to create folders and subfolders
I need to create a lot of folders and subfolders and was wondering how I might write a script to do this. I need to have a top level folder, 'images' and under that, subfolders numbered sequentially, '100', 101', 102', etc. Within those subfolders I need three additional folders, '192x128', '384x256', and '768x512'. The second level folders (the sequential ones) will number anywhere from ten to 100 folders. Any thoughts on how I can do this?
I am using ImageMagick to resize images in one set of folder and I need to move them over to this new set and unfortunately the folder structure is not the same as where I am getting the images. Thanks, Todd |
please do man mkdir
|
There's more
As an aside, you are more likely to get help by starting this process, and then asking for help.
First, create the script file name using a text editor. Depending what shell you want it to run -- bash -- make sure the first line is #!/bin/bash. On Red Hat systems #!/bin/sh links to /bin/bash, but that is not guaranteed on other distros. Don't forget to protect the file chmod 775 <script_file_name>, and invoke it (run it) using ./ in front of the script name. Then, look through man bash and/or try this link to find out about for loops http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html You could probably put all your resolution directories in one of those for loops and then use mkdir within the loop. 3) It also helps for you and those answering you to know your distribution. |
man mkdir is one of the first places that I looked. I feel that my needs are a bit more involved than what I can get out of simply the mkdir command. I would imagine that I will need to do some scripting that is a bit more involved than what I am familiar with.
Thanks, Todd |
Quote:
Code:
mkdir -p images/{100..110}/192x128 |
Thanks for the info. I will work on this later when I get home. Seems like I need to set up a 'while' statement to create the first level directories and set up a counter and keep creating directories till the counter increments to a set number. Then I'll need to figure out a way to populate each one of those new directories with three more subdirectories, '192x128', '384x256', and '768x512'. Does that sound like I am on the right track?
Thanks, Todd |
calucix,
Thanks, that sounds like what I need! I just bought 'Learning the Bash Shell' by O'Reilly so I'm sure that that will be a big help. I appreciate the insight. I will be doing a lot of this and this script will be a big time saver for me. Thanks, Todd |
As colucix pointed out, the {n..m} expansion works. You can also use {a,b,c} which will expand to "a" and b" and "c". Combining them, you can quickly make a long list of strings. For example:
Code:
% echo {1..3}{a,b} Code:
mkdir -p images/{100..110}/{192x128,384x256,768x512} |
Matt,
That worked beautifully, that did exactly what I needed, and it was so simple. Now I just need to play with ImageMagick and get the resized files in the appropriate places and I am set. I think that I am going to have to get more familiar with bash, this can be quite the time saver. Thanks so much for everything! Todd |
Yeah, shell scripting can be a really boost to productivity. It's not always pretty, but for things like this, you can often spend 2 minutes doing what would take 2 hours with GUI tools.
|
I completed my project (thanks to you guys) and all went rather well. It went so well in fact that I was handed another one (gotta love working for a nonprofit...). This one is very similar with a bit of a twist. I will have a directory with subdirectories which include images. The subdirectories could be one or more levels deep. I need to convert all of the images to thumbnails and maintain the existing directory structure.
I was looking for a script that would recursively drill down into the directories and convert and images to thumbnails. I found this script and am trying to decipher it: Code:
new=order-thumb Thanks again for everyone's previous help, Todd |
Code:
|
If you put
Code:
set -vx With long scripts, this will often create so much output that you get swamped, but for small fragments it can be very helpful to understand what is going on. for example: Code:
#!/bin/bash Code:
cd $HOME/tmp/test I would like to point out that you might run into problems when there are files with spaces in the file name. Whenever you are iterating over a list of strings, it is prudent to use "double quotes" when taking the value of the iterator variable ($i in this case). Consider this example: Code:
touch "file1.txt" "file 2.txt" Code:
ls -l file 2.txt To correct it, you should add double quotes when using the iterator operator: Code:
for f in *.txt; do In the case where you are calling find like this, it is worse: Code:
for i in $(find /photo1 -name "*jpg"); do A better construct is like this: Code:
find /photo1 -name "*.jpg" | while read i; do This still does not cope with the case where there is a new line character in a file name, but it's probably safe to assume that you won't encounter that very often. |
All times are GMT -5. The time now is 09:02 AM. |