Find the answer to your Linux question:
Results 1 to 2 of 2
Hello everyone! I have to do a presentation on a script that i'm creating on file deletion (text files). Basically, I want to take user input (directory path and days ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2012
    Posts
    1

    File deletion with user input - help!


    Hello everyone!

    I have to do a presentation on a script that i'm creating on file deletion (text files). Basically, I want to take user input (directory path and days old before deletion), and delete the .txt files in that directory that are older than X days.

    I'm having an issue with my IF statements.

    I highlighted in red where I believe my error is occurring. The terminal doesn't tell me much, just that there is a syntax error. I'm assuming I didn't branch something right, but i'm not positive.

    Code:
    echo "Enter directory path of the .txt files you would like to delete"
    read dirpath
    if [ -d "$dirpath" ]; then
            echo "Directory path found."
            echo "Number of days old would you like to delete the .txt files?"
            read daysold
            echo "Is $daysold the correct number of days old and then deletion? y/n"
            read answer2
                    if [ "$answer2" == "y" ]; then
                    find $dirpath -mtime +$daysold -name "*.txt" -exec rm {} \;     
                    echo "Complete! Files older than $daysold days old in $dirpath have been deleted!"
                    elif [ "$answer2" == "n" ]; then
                    echo "Quitting.."
                    exit
    else
    echo "File path not found. Quitting..."
    exit
    fi

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Hi,

    I think you are missing a closing "fi", see this chunk of code:
    Code:
    if [ "$answer2" == "y" ]; then
      find $dirpath -mtime +$daysold -name "*.txt" -exec rm {} \;     
      echo "Complete! Files older than $daysold days old in $dirpath have been deleted!"
    elif [ "$answer2" == "n" ]; then
      echo "Quitting.."
      exit
    fi
    Edit: Welcome to LinuxForums!
    Last edited by atreyu; 10-27-2012 at 08:43 PM. Reason: Welcome!

Posting Permissions

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