LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 07-09-2006, 03:57 PM   #1
drkstr
Senior Member
 
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Rep: Reputation: 45
using recursion in bash script


I need to write a script that preforms an operation on all files in a directory and it's sub-directories. How does one go about doing this in bash? Also, what's the best way to distinguish between a file and a folder?

thanks!
...aaron
 
Old 07-09-2006, 04:11 PM   #2
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 48
use find:

to run "dosomething file" on all the files:
Code:
find . -type f -exec dosomething ’{}’ \;
the "-type f" chooses only files
 
Old 07-09-2006, 04:19 PM   #3
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
This is how it works:
Code:
#! /bin/bash
for NAME in $(find /some/directory)
do
  if [[ -d $NAME ]]
  then
    echo "$NAME is a directory."
  fi
  if [[ -f $NAME ]]
  then
    echo "$NAME is a regular file."
  fi
done
If there are many files, it's probably better to use the following loop:
Code:
find /some/directory | while read NAME
do
  ...
done
 
Old 07-09-2006, 05:09 PM   #4
drkstr
Senior Member
 
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Original Poster
Rep: Reputation: 45
Thanks! a mixture of both worked perfectly for me:
Code:
...
find $DIR -type f | while read file
do
  cat $file | sed -e 's/'$IN_TXT'/'$OUT_TXT'/g' > $file
done
...
One other thing though, and sorry this is a bot off topic. There seems to be a limit on how much I can 'cat' ( 68 Kb ). Any way around this?

thanks!
...aaron

**edit**
I'm going to start a new topic since so the information will be easier to find if other people have the same question.

Last edited by drkstr; 07-09-2006 at 08:02 PM.
 
Old 07-09-2006, 08:48 PM   #5
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 48
Quote:
Originally Posted by drkstr
Code:
...
find $DIR -type f | while read file
do
  cat $file | sed -e 's/'$IN_TXT'/'$OUT_TXT'/g' > $file
done
...
why not just something like
Code:
find $DIR -type f -exec sed -i 's/'$IN_TXT'/'$OUT_TXT'/g' '{}' \;
note that doing sed on a file and then redirecting it back to the same file doesn't really work, since ">" truncates the file, so you should use "sed -i" to operate on the file in-place
 
  


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 Local Variable Recursion With Array jshivers Programming 0 06-16-2006 04:31 PM
tar: '--no-recursion' option doesn't prevent recursion Earl Parker II Slackware 12 08-17-2004 02:49 AM
Writing bash script with recursion.. ray5_83 Programming 4 08-04-2004 05:44 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration