LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 02-21-2013, 09:25 AM   #1
vdamgo
LQ Newbie
 
Registered: Feb 2013
Posts: 11

Rep: Reputation: Disabled
Linux script that reads thru folders and execute command


how to write a script that reads thru the bunch of folders using while loop.
Please let me know.

Thanks!
 
Old 02-21-2013, 09:31 AM   #2
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-19-live-usb/ aix
Posts: 3,840

Rep: Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590
where are you stuck ?
i would use the find command to get a list of the directories then pipe it into whatever program i would want to run. the basic structure would be:
Code:
while read line
do
 something $line
done
 
Old 02-21-2013, 09:39 AM   #3
vdamgo
LQ Newbie
 
Registered: Feb 2013
Posts: 11

Original Poster
Rep: Reputation: Disabled
Thank you for the reply.

I have one command as below which will sum the file size in each folder, want to execute in 50 folders and get the size of all folders. instead of executing the command in each folder.

find . -type f -newer folder1 -not -newer folder2 -printf "%s\n" | awk '{sum += $0} END{print sum}'
 
Old 02-21-2013, 09:47 AM   #4
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-19-live-usb/ aix
Posts: 3,840

Rep: Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590
Quote:
Originally Posted by vdamgo View Post
Thank you for the reply.

I have one command as below which will sum the file size in each folder, want to execute in 50 folders and get the size of all folders. instead of executing the command in each folder.

find . -type f -newer folder1 -not -newer folder2 -printf "%s\n" | awk '{sum += $0} END{print sum}'
not really sure from your description what you are after but heres my stab at it:
Code:
find . -type d | while read dir
do
 find "$dir" -type f -newer "$dir"/folder1 -not -newer "$dir"/folder2 -printf "%s\n" | awk '{sum += $0} END{print sum}'
done

Last edited by schneidz; 02-21-2013 at 09:48 AM.
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
script execute command problem ninja master Linux - Newbie 3 02-21-2012 07:26 PM
shell script reads all file names passed on the command line path_finder Linux - Kernel 1 04-01-2010 04:01 PM
script to execute a command multiple times snr8fl3 Programming 5 11-17-2008 12:49 PM
Command would not execute within a script pramka Suse/Novell 2 07-16-2008 10:11 AM


All times are GMT -5. The time now is 01:27 PM.

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