LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-20-2010, 11:25 AM   #1
ziphem
Member
 
Registered: Feb 2004
Location: US / EU
Distribution: Fedora 20
Posts: 158

Rep: Reputation: 19
Problem with variable in find -exec {}


Hi, I'm having a hard time with the following:

find /home/windhamcam/driveway/test/drive/ -iname \*.jpg -exec convert '{}' -resize 33% -quality 75% thumbs/'{}'_thumbnail.jpg \;

Ultimately I'm looking to have directory "drive" searched (not recursively, so maybe I would try with . -maxdepth?) and all images converted and put into drive/thumbs. The script will not sit in the same directory as the .jpg files.

The {} preserves the entire long path, so I'm trying to figure out how to strip that out somehow - otherwise instead of converting the files to director thumbs/image.jpg, it attempts to convert them to thumbs//home/windhamcam/driveway/test/drive/image_thumbnail.jpg.

I am actually going to put two of the above arguements, each for a different directory, into one file. In any case, I'm having a major problem with this entire piece. Any help is appreciated. Thank you!!!!!!


EDIT:

Ok, I see recursive directories can be avoided this way:
find * -prune -name '*.jpg' \
-exec convert '{}' -resize 33% -quality 75% thumbs/'{}'_thumbnail.jpg \;

But stripping out the path is still an issue...

Last edited by ziphem; 10-20-2010 at 11:35 AM. Reason: Changed code!
 
Old 10-20-2010, 11:38 AM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
I suggest doing what you;re doing by using a loop. Pipe the output of `find` into the loop, and read each line as it comes in and deal with it by getting the `basename` of the line (that'll give you the filename) and the `dirname` of the line, if you actually need the long dir-path. It would be something like this:

Code:
find /home/what/ever/it/is/drive/ -iname blah blah | while read line; do
 FILENAME=$(basename "$line")
 convert "$line" -resize blah blah blah thumbs/${FILENAME}_thumbnail.jpg
done
So, that's pretty rough pseudo-code, but looking at what you have so far, I suspect you should be able to fix up this code and get it working; nice job so far, congrats! I just think that to get precisely what you wish, you need to use a little more than `find` although I would not be surprised if someone comes in here with a great one-liner using only find!

Good luck - keep us posted. If you have problems, please explain exactly what isn't working - don't just say "major problem" - show us what the problem is.

EDIT - However an idea: try using $(basename {}) in place of the '{}' in the destination path in your existing find command (in the thumbnail path)- you may find that that is enough to make it work (I'm not sure it will work at all though). And look up the manpage for `basename` if you need to know more about that or don't understand its purpose..

Last edited by GrapefruiTgirl; 10-20-2010 at 11:42 AM.
 
Old 10-20-2010, 02:12 PM   #3
ziphem
Member
 
Registered: Feb 2004
Location: US / EU
Distribution: Fedora 20
Posts: 158

Original Poster
Rep: Reputation: 19
This is fantastic. I relied on your example and it is working just right. Thank you!!
 
Old 10-20-2010, 03:36 PM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Hey, good stuff! Which did you do, the loop? Or did you use `basename` right on the end of the original `find` command?

Also, I notice in your first post, you seem to be wondering about (preventing) recursion?

With `find`, you can use the -maxdepth or -depth options to control recursion, i.e. to control how many subdirectories deep you wish to search through. The -prune option is for removing path(s) or name(s) or any sort of regex, from the the search criteria.

They sound sort of similar... But -prune says where/what not to look at; -maxdepth says how deep to recurse into what you do want to look at.
 
Old 10-20-2010, 03:55 PM   #5
ziphem
Member
 
Registered: Feb 2004
Location: US / EU
Distribution: Fedora 20
Posts: 158

Original Poster
Rep: Reputation: 19
Hi,

The loop worked well and so I used that. I did add a -maxdepth 1 as well as -mindepth 1 for good order's sake. I'm very happy how it works. There is actually another question I have that relates to this (and really any) bash script. Let's I have several commands in the script - so, say that after resizing the images, I want to move the originals to another directory, amongst other things. Are the commands run after completion of the prior task, or are they run only at the completion of the command itself? The resizing should take longer than the file moving. I ran a test and it seemed to move the files before I could resize them, I think. What can I do to ensure the commands and tasks run sequentially? I saw the command "done", but when I looked it up, it looks grouped with arguements.

Thanks a ton again!!
 
Old 10-20-2010, 04:09 PM   #6
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Far as I know (and this could be wrong, so don't quote me), tasks generally run in order that they are received, when it comes to shell scripts. Unless you background a particular operation, the next operation should wait till the first is done. If you're in doubt and believe you have a race condition, use the `wait` command after any command that you wish the shell to await its completion before proceeding - and also, look up the `wait` command in the Bash manpage to first make sure that it doesn't apply only to backgrounded processes.

However, with this sort of loop, I think there's a good chance that the `find` command will be done "finding stuff" before the loop is finished looping through the found stuff; in other words, the pipe will "back up" as it fills with the output of `find`, but the loop will still continue looping through each inputted result in order, until the pipe is empty.

I suggest you verify that the files are being resized and then moved sequentially. Maybe put a "sleep 1" after each move or resize, and perhaps put some "echo" statements in your code to tell what the code is doing at a given time, and follow the progress. If you aren't seeing "file not found" or other piles of errors, chances are the things are happening in the right order. But if you're still not sure, you'd need to post for us the code you're using, and maybe someone (or myself) can better explain what you're asking, if I have not done a clear job (which is of course entirely possible )
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash redirection with exec: how to specify fd with variable? ta0kira Programming 3 02-21-2010 08:09 PM
Find with -exec argument - not giving proper output..how to find... hinetvenkat Linux - Server 4 01-25-2010 07:19 AM
Find variable name by variable content using a subshell UltramaticOrange Programming 3 11-17-2008 04:51 PM
How to pass a result of exec command in perl to a variable??? HyperTrey Programming 3 05-23-2008 01:47 PM
find -exec syntax: bug in find? mfcarroll Programming 5 06-21-2007 08:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration