Need a little help with my first shell script. Basic image resize script...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need a little help with my first shell script. Basic image resize script...
# 8  
Old 10-03-2015
Quote:
Originally Posted by mozzles
[...]
Here is the script I am running now...

Code:
#!/bin/bash
for f in `find . -name "folder.jpg"`
do
    convert $f -resize 200x200\!  $f-thumb.jpg
done

[...]
A few things to be aware:
Using a for loop here limits to files that do not have spaces, if any file matched by find contains spaces the script would introduce a behavior you do not want. And by the way, find will process any directory tree recursively.

find . -name "folder.jpg" will not be limited to only files but to directories that contains that name as well.

Here's another untested version that might process any files named folder.jpeg or folder.jpg in any directory starting in the current one.
If you like what echo produces, comment it and uncomment the convert.
As an example it does change the name with one of your new names suggestions.

Code:
#!/bin/bash

find . -type f -name "folder.jp*g" | while read f
do
  path="${f%/*}"
  extension="${f##*.}"
  echo "$f -> ${path}/album_art_small.${extension}"
  # convert "$f" -resize 200x200\!  "${path}"/album_art_small."${extension}"
done

# 9  
Old 10-03-2015
Thank you! That solved one problem. I tested it, and it will actually work a couple directories down, too. Only it won't if there are any spaces in the directory name. Is there any way to fix this?
# 10  
Old 10-03-2015
Quote:
Originally Posted by mozzles
Sorry... I was trying to be as clear as possible. Maybe this is just a strange request, and that's why I can't find a script for it already.

I am not looking to convert all my .jpg files, only the ones named folder.jpg (the standard I decided on, for album cover art that I want to appear). If possible it'd be great to have it work for Folder.jpg and .jpeg of each too, since I'm sure I missed some.... but if I need to I can just edit the script and run a few times.

I was able to finally get it to work in the main dir and one below it, but I don't think it will go any further than that. The way that my filesystem is set up, I'm really going to need it to, or this will still take a good deal of manual labor.

Here is the script I am running now...

Code:
#!/bin/bash
for f in `find . -name "folder.jpg"`
do
    convert $f -resize 200x200\!  $f-thumb.jpg
done

The way it's set up now, the thumbnail files end up with names like folder.jpg-thumb.jpg. Is there any way I can change that to either adding something before the filename (i.e. small_folder.jpg) or an entirely new filename (i.e album_art_small.jpg)?

Tried just putting the filename I want instead of the $f-.jpg, and then it would only work in the main directory for some reason.

And again... pretty sure this script as I have it now will only work one subdirectory down. That's something I'll need to fix one way or another.

Thanks for your help! It is much appreciated.

Zac
If the above script is working for you except for the set of filenames to be processed and the output names your want, let's try the following:
Code:
#!/bin/bash
find . -name '[Ff]older.jpg' -o -name '[Ff]older.jpeg' |
while read -r f
do
	convert -resize 200x200\! "$f" "${f%/*}/thumb-${f##*/}"
done

The find command in this script will identify files named Folder.jpg, folder.jpg, Folder.jpeg, and folder.jpeg anywhere in the file hierarchy rooted in the current directory. If you want to add more filenames to the list of those to be processed, add an additional -o 'pattern' for each additional filename matching pattern desired to the end of the find command. For each of the files found, this script it will create a file in the directory where that file was found with the prefix thumb- added to the final component of the pathname.

Using find ... | while read ... f instead of for f in `find ...` allows the find command to run in parallel with the thumbnail creations (instead of creating a complete list of files to process before starting to create the thumbnails) and requires less memory in the shell to get the job done. The quotes added to the $f parameter expansions protect against the possibility of your directory names containing any spaces, tabs, or pathname expansion characters. The ${f%/*} parameter expansion extracts the pathname of the directory in which the file resides and the ${f##*/} parameter expansion extract the final component of the pathname. Using those two expansions allows us to insert the thumb- filename prefix in the correct spot in the output file pathname. If you just want to use the filename album_art_small.jpg as your output filename, the convert command in the above script can be changed to:
Code:
	convert -resize 200x200\! "$f" "${f%/*}/album_art_small.jpg"

but note that if you do that and you happen to have two or more files to be processed in a single directory, the last one processed will be the only thumbnail that is kept (thumbnails processed earlier will be overwritten by thumbnails processed later).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Beginner bash - basic shell script 'while' help...

Hi everyone, first time visitor to these forums here. Keeping a long story short I've been attempting to learn how to code in bash. I have VERY little previous experience with coding languages besides simply copying and pasting batch scripts for Windows. So, with that in mind I've followed a... (4 Replies)
Discussion started by: Meta
4 Replies

2. Shell Programming and Scripting

Basic Shell script - Not working

Hello, This is basic (i think). I am trying to run a shell script which would go into each folder (folder names defined in the list) and after entering would run some commands, once done, come out of the folder and continue until the list ends. Pretty basic and there are bunch of example online. ... (9 Replies)
Discussion started by: Zam_1234
9 Replies

3. Shell Programming and Scripting

Basic Combination Shell Script

I need to have a script read a file that has a list of words in a single column like below:Black Blue Brown Orange Red Yellow Green White Purple Silver Grey Tan Then print to another file just all of the two-word possible combinations. Example: Black,Blue Anyone want to take a... (4 Replies)
Discussion started by: vespasian
4 Replies

4. Shell Programming and Scripting

Basic question on shell script execution

I have two shell scripts in the different directories listed below, /root/dev/dir1/test.sh /root/dev/dir2/master.sh I am executing the master.sh script from the test.sh like below and getting 'Permission denied' error. #! /bin/sh #test.sh path='/root/dev' $path/dir2/master.sh But it... (2 Replies)
Discussion started by: vel4ever
2 Replies

5. Shell Programming and Scripting

Help! Basic shell script advice

##### (2 Replies)
Discussion started by: AidoPotato
2 Replies

6. Shell Programming and Scripting

Basic shell script help

Im trying to make a script that simply adds a word to the last available line in a txt file without overwriting any previous lines. Ive googled this and there are great examples but no one explain what each function does, and i dont entirely understand how it works. Basically Im looking for... (7 Replies)
Discussion started by: kylecn
7 Replies

7. Shell Programming and Scripting

Basic Shell Script Help

Lets say I wanted to create a script that would show what people are doing on my machine using the w command and refresh in about 6 seconds. What would be the easiest way to do this? I pretty much want the script to loop until I stop it. I'm using the BASH shell by the way. Help is appreciated.... (1 Reply)
Discussion started by: c4391
1 Replies

8. Shell Programming and Scripting

shell script basic doubt

hi, I am new script learner, so my basic doubt is , how to store value of any command in a variable example $ ls | wc -l i want to stote the output of this in a variable c. so that i can use c in if else loop. and when do we use " ` " symbol in script.. can anyone also tell for... (5 Replies)
Discussion started by: hi2_t
5 Replies

9. Shell Programming and Scripting

Basic Shell script syntax help

Hi All, I am new to shell scripting. I have a variable which holds a numeric value.I have to check whether this variable holds a value between(0- 8),(8-17)(17-24).How do i write this syntax using if in shell scripting. Thanks Vignesh (2 Replies)
Discussion started by: vignesh53
2 Replies

10. Shell Programming and Scripting

need a quick basic shell script help

im trying to run the below if command ifconfig -a |grep 10.100.120.21 gives me below output inet addr:10.100.120.21 Bcast:10.100.120.255 Mask:255.255.255.0 i just want a basic shell which says if above exists then continue how would i do this? (6 Replies)
Discussion started by: eb222
6 Replies
Login or Register to Ask a Question