LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 12-03-2008, 06:08 PM   #1
jdwilder
Member
 
Registered: Jul 2003
Location: United States
Distribution: Fedora Core 7 and older, Knoppix, Ubuntu
Posts: 121

Rep: Reputation: 15
Running a script on all files in a directory tree


I am attempting to run a c++ program on all the images in a directory and all of its sub directories. To do this I run

find . -type f -exec file '{}' \;

The c++ program outputs the results to standard out, and I want to redirect to a file for each image, so if the program is running on a.jpeg I want a file called a.txt (or a.jpeg.txt would be fine).

I try

find . -type f -exec file '{}' > '{}'.txt \;

But this only creates a file named {}.txt. How do I get it to recognize the {} as the name of the current file after the >?

Is there a way to do this in one command on the command line (or a different post I failed to find that explains how)?
 
Old 12-03-2008, 07:32 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
By not wrapping it in single quotes? Just kidding - that's only
half the problem. The other half is that you can't use the {}
twice ...
Use a wrapper:
Code:
for i in $(find -type f)
do
  file ${i} > ${i}.out
done
 
Old 12-04-2008, 10:06 AM   #3
jdwilder
Member
 
Registered: Jul 2003
Location: United States
Distribution: Fedora Core 7 and older, Knoppix, Ubuntu
Posts: 121

Original Poster
Rep: Reputation: 15
Thumbs up Solved

Works, thank you!
 
Old 12-04-2008, 10:20 AM   #4
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
The problem is solved. However I will make a slight comment for the sake of correctness.


Quote:
Originally Posted by Tinkster View Post
By not wrapping it in single quotes? Just kidding - that's only
half the problem. The other half is that you can't use the {}
twice ...
The real problem is not using it twice. You can easily check that using it twice is not a problem if you do this:

Code:
for i in $HOME -mexdepth 1 -exec echo '{}' '{}' \;
It should work as expected, printing the name of every file in the home folder two times in a row.

The problem with his expression is that the redirection operator takes precedence (and anyway, even if it didn't, find doesn't have a meaning for it anyway. So, the find command ends where the redirection starts, and the second '{}' is not in the land of find, but in the land of bash, outside find. For bash, '{}' has no special meaning, and is a textual string (and so, it's printed as that, without any kind of expansion attached to it.

So, piping the names into a loop is probably the best way:

Code:
find whatever | while read file
do
  file "$file" > "${file}.log"
done
Note proper usage of quotation as well. Otherwise you'll have problems with file names containing spaces or any other special character.

Last edited by i92guboj; 12-04-2008 at 10:22 AM.
 
  


Reply



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 script for monitoring changes in a directory tree librano Linux - Software 4 10-30-2008 07:36 PM
Bash, deleting only files in a directory tree? jon_k Programming 4 05-13-2007 11:36 AM
Bash script to traverse directory tree and rename files intramaweb Programming 3 10-08-2006 12:51 PM
locate and copy files - without directory tree rodda Linux - General 1 01-16-2005 02:10 AM
How to copy a directory tree without copying the files in it? system Linux - General 2 01-15-2002 09:36 PM

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

All times are GMT -5. The time now is 03:16 PM.

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
Open Source Consulting | Domain Registration