If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Urgent help : traversing directory tree

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-19-07, 23:39
harpreet_walia harpreet_walia is offline
Registered User
 
Join Date: Feb 2007
Posts: 2
Urgent help : traversing directory tree

Hi People

I am currently on shell script in which I am trying to traverse a directory tree. All the sub directories and files in this directory uses timestamp as their file/directory names.

Requirement :- Need to run cat command on all the files in current directory in order of its timestamp i.e filename that has latest timestamp as filename is to cat first.......,and so on ....after doing cat to all the files I need to check the sub directories , and need to do cd into the sub -directory that has latest time stamp among the directories as a dirname....after doing cd need to perform same processign as defined above ....like this need to traverse all the directories present in the main directory .....

I am a newbie .....I have tried it ...code looks like :


process(){

cd $1

if [ $? -ne 0 ]
then
return 0 # Don't have execute permission, return
fi

local count=0


find . -name "*" -type f -maxdepth 1 -mindepth 1 -exec basename {} \; | sort -nr > ~/tempfiles

while read fname
do
cat $fname > /dev/null
done < ~/tempfiles

find . -name "*" -type d -maxdepth 1 -mindepth 1 -exec basename {} \; | sort -nr > ~/tempdir

while read dirname
do
local array[$count]=${1}/${dirname}
count=$(($count + 1))
done < ~/tempdir

if [ $count -eq 0 ]
then
return 0
fi

local count_T=0
while [ $count_T -lt $count ]
do
process ${array[$count_T]}
count_T=$(($count_T + 1))
done

return 0

}

if [ ! -d "$1" ]
then
echo -e "Please Enter Directory\n"
echo -e "EXITING ....."
exit 0
fi

process $1


I need suggestions from you people ...please advise so that i can learn and make can my script more efficient....any alternate solutions are most welcome....this script is recursive in nature ...is there any iterative solution to the problem becoz this script is consuming lot of time for large directories......

Last edited by harpreet_walia; 02-20-07 at 01:57.
Reply With Quote
  #2 (permalink)  
Old 02-20-07, 05:28
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 1,268
What does the timestamp look like? yyyymmddhhnnss
Maybe an alpha sort is all you need
find . -type f |sort -r | while read fn
do
cat $fn >/dev/null
done
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On