LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-13-2009, 10:21 PM   #1
matt79
LQ Newbie
 
Registered: Feb 2009
Posts: 1

Rep: Reputation: 0
shell script which makes *something* with every file in folder


how can I make a shell script, which takes every file(I mean it has to work within every folder regardless the amount of files in folder or file names) from specified folder, then some other command runs and does something with file, and finally the original file is saved back to that specified folder with the same name. Or with different name- this doesn't matter so much. After that, the second file goes through the same process and then the third etc. How to accomplish that?

btw regards from Australia!

Last edited by matt79; 02-13-2009 at 10:29 PM.
 
Old 02-13-2009, 10:27 PM   #2
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
find /specified/dir * -exec commandToRunOnEachFileFound {} \;

{} represents the pathname of the current file found so you can reuse it to do whatever to the current file found. If what you want to do it complex, then perhaps you pass it as an argument to the script you write and call with -exec.

Example:
Quote:
find . * -exec echo File: {} \;
'man find' for more info.
 
Old 02-14-2009, 10:48 AM   #3
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

Quote:
Originally Posted by Admiral Beotch View Post
find /specified/dir * -exec commandToRunOnEachFileFound {} \;
No, this doesn't work correctly, because the unescaped asterisk is substituted with all pathnames in the current directory by the shell before find runs. BTW: To get all pathnames in a directory recursively you don't have to add a pattern, find /specified/dir -exec commandToRunOnEachFileFound {} \; will work.

There are some ways, for example:
Code:
find /specified/dir -type f -print | while read f; do
  TMP_FILE=`mktemp` || exit 1
  do_something $f >$TMP_FILE && mv $TMP_FILE $f
done
find searches for all regular files in /specified/dir recursively, these file names are read by the loop, mktemp creates a temporary file with a unique name, then do_something is called for each file, the output is written to the temp file, on success the temp file is moved to the original file.

Code:
find /specified/dir -type f -print | xargs do_something
In this case do_something gets a bunch of pathnames as command line parameters and can process them in a loop. This way does not create a new process for each file.

Jan
 
Old 02-14-2009, 03:09 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Yes, piping the output of find to something like a while loop makes it a lot easier to work with, especially if you need sed replacement, etc. Also, you might consider using -depth with find so that contents are processed before their directories, and also '!' -name . to prevent the top directory (as opposed to its contents) from being processed.
ta0kira
 
Old 02-15-2009, 10:57 AM   #5
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

Quote:
Originally Posted by ta0kira View Post
... Also, you might consider using -depth with find so that contents are processed before their directories, and also '!' -name . to prevent the top directory (as opposed to its contents) from being processed.
ta0kira
Theses options make sense only if you want to process directories too. The -type f option I used limits the output to regular files. The original question was to process all files and store the results in the same file names, so no dependencies to other files exist.

Jan
 
Old 02-17-2009, 03:58 PM   #6
m4rtin
Member
 
Registered: Sep 2007
Posts: 261

Rep: Reputation: 16
thank you for help! This while loop:
Code:
find /specified/dir -type f -print | while read f; do
  TMP_FILE=`mktemp` || exit 1
  do_something $f >$TMP_FILE && mv $TMP_FILE $f
done
works nicely, but if I would like to rename original files to *mp3_old, then I should probably use something like that:

Code:
for f in *.mp3; do mv "$f" "${f}_old"
However, how can I integrate this for loop into while loop?
 
  


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 Shell Scripting -- foreach file in folder??? AC97Conquerer Programming 11 07-06-2011 12:25 AM
BASH Shell script : copying a file to multiple folder zamri Programming 14 04-29-2008 10:27 AM
Shell Script to Get Folder Names firefoxlinux Programming 6 09-27-2007 02:56 PM
Need a script to search and replace text in file using shell script unixlearner Programming 14 06-21-2007 10:37 PM
Shell script deletes folder before due OR13 Linux - General 3 08-04-2006 08:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:20 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