#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2008
    Posts
    22
    Rep Power
    0

    Traversing Directories using shell script


    Hello,

    I have a requirement to removes files in folder A and retain files in folder B after some time. Both A and B folders are subfolders in parent folder called "APP/logs"

    I tried to write a sample script as below for this:

    sample.sh

    Code:
    cat folderpath.txt | while read folName
    do
    if [ $folName = "./logs/A" ]
    then
    echo "Reached folder A"
    #Retain files here
    else
    echo "Reached other folders"
    #delete all the files here using find command
    fi
    done
    -------------------------------------------
    folderpath.txt contains

    ./logs/A
    ./logs/B

    while running sample.sh from APP folder..I am getting error message as
    [ : := unary operator expected

    please help and let me know where i am doing wrong..
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,442
    Rep Power
    1688
    You may be over-engineering this. If you retain the files in folder A and delete those on folder B then ...why not just do so?
    Unless there's a reason not to, why not just do a rm logs/B?
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2008
    Posts
    22
    Rep Power
    0
    Thanks Simon

    Currently, both the files in folder A and B are deleted at the end of the day. So now..I need to delete only files in folder A not in B. The files in B will be deleted after a week.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,442
    Rep Power
    1688
    Ooops, sorry - got my As and Bs mixed up ... I think!

    Ok, so we are running this via cron, yes? Once per day at a fixed time?
    1) Files in A get deleted, regardless
    2) Files in B get deleted once they are more than a week old?

    1) is easy - rm ./logs/A/* (assuming no sub-directories)
    That is, delete all entries in the logs/A directory under the current directory. If doing this in cron beware your current directory - by default it will be the home directory of the user.
    2) is also quite easy:
    Code:
    find ./logs/B -type f -mtime +6 -exec rm {} \;
    That is find any file under logs/B under the current directory (see above) and delete them if they have not been modified within last 6 days (logs files are not usually modified unless being written to, so that, generally, means they are that old ...)
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2008
    Posts
    22
    Rep Power
    0
    Yes, we are running this through cron job.

    But we are giving input of path through while loop, so how do we check it if the folder is A or B. Using while loop is existing design..and hence I am restricted to play around with the existing.

    While I tried to do so in the loop (script provided) above, I am getting an error.

    Please help
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,442
    Rep Power
    1688
    Ok, I still think it's not the best way of doing it, but ..

    Change the conditional to put the variable name in quotes:
    Code:
    if [ "$folName" = "./logs/A" ]
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2008
    Posts
    22
    Rep Power
    0
    Thanks.

    The error of unary operator expected is resolved. However, the condition in the if loop is not evaluated. Hence it always go to else loop.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2008
    Posts
    22
    Rep Power
    0
    Sorry..my mistake. Condition is working perfectly.

    You think this is not the best way because the path in the loop is hardcoded? Is that the reason or is there any other problem.

    If this is not the best way, please let me know if there is any other best option to go ahead.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,442
    Rep Power
    1688
    Take for granted I am not aware of the setup and history of the server ...
    In this script example we see you looping over a file which contains two directories. If the current line that was read is "./logs/A" you do one thing, if it is anything else you do another. As the file contains two directories you will always do both things, so why have the loop? If you are not going to have the loop you don't need the file to drive the loop. From what you say you'll always delete one set of log files, so you don't really need the external file to tell you that.
    The only benefit you are getting out of this mechanism is the external file which could be used to drive various options by editing that file instead of the script, but with the current script that's not really a great benefit as it will either delete files in logs/A or if the external file contains anything else, the files in logs/B.
    If you are doing 90% of the same thing every day with a 10% variation on one day (such as just deleting the files in one of the directories on one day of the week) then deal with that in the script (if day = Friday; then ... etc.)
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2008
    Posts
    22
    Rep Power
    0
    Thanks Simon.

    I have one more query..but will start new thread for that

IMN logo majestic logo threadwatch logo seochat tools logo