Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2007
    Posts
    2

    Unanswered: 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 02:57.

  2. #2
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •