Find the answer to your Linux question:
Results 1 to 5 of 5
Hi all, what am I doing wrong? ls -l of file1: Code: -rw-r--r-- 1 root root 0 2007-04-11 00:01 /var/tmp/dateFile file2 is a directory in the /tmp directory: Code: drwx------ ...
  1. #1
    Just Joined!
    Join Date
    Apr 2007
    Posts
    3

    Question Bash [[ file1 -ot file2 ]] not working?

    Hi all,

    what am I doing wrong?

    ls -l of file1:
    Code:
    -rw-r--r-- 1 root root 0 2007-04-11 00:01 /var/tmp/dateFile
    file2 is a directory in the /tmp directory:
    Code:
    drwx------   2 root    root    4096 2007-04-21 12:47 kde-root
    Bash script code snippet:
    Code:
    for file in `ls /tmp` ;
    do
            if [[ $file -ot /var/tmp/dateFile ]]
            then
                    echo "File $file is older than /var/tmp/dateFile and will be removed."
                    if [[ $debug == "false" ]]
                    then
                            rm -rf $file
                    fi
            fi
    done
    My log file tells me that kde-root is older than dateFile.
    This seems wrong to me. So, I figure I am abusing something in the language here. Any help would be greatly appreciated.

    GenericHuman

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    That is very strange, since my test script works fine...

    Try replacing the condition with:
    Code:
    if [ $(stat -c '%Z' "$file") -lt $(stat -c '%Z' /var/tmp/dateFile) ]
    Does that give the correct results?
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Just Joined!
    Join Date
    Apr 2007
    Posts
    3
    I just tried it with the suggested line, and I get the same results.
    The code isn't that long.... I'll list the whole thing so you get a better shot at seeing if I'm messing up elsewhere and this is just an unhappy side-effect.

    Note that I have only commented out my original algorithm for the date comparison.

    Any recommendations on general style are appreciated as well.

    Code:
    #!/bin/bash
    
    #
    # This is a script to clean up /tmp using a file with a
    #+ set timestamp and then removing all files that are
    #+ older than this test file.
    #
    
    ERR_NOT_ROOT=10
    logfile=/var/log/dirClean.log
    
    ########################################################
    # With $debug set to "true" no files will be removed.
    #+ $debug can have values of "true" or "false".
    ########################################################
    debug=true
    
    ########################################################
    # If the script is not in $debug mode, it must be run
    #+ as root. If not root, then print explanation and
    #+ exit the process.
    ########################################################
    if [[ "$UID" -ne 0 && $debug == "false" ]]
    then
            echo "You must be root to run this script."
            exit $ERR_NOT_ROOT
    else
            exec 5<&1               # Associate stdout to 5
            exec 6<&2               # Associate stderr to 6
            exec 1>>$logfile                # Redirct stdout to log
            exec 2>>$logfile                # Redirct stderr log
    fi
    
    ########################################################
    # Set up the date variables
    ########################################################
    day=`date +%d`
    year=`date +%Y`
    month=`date +%m`
    
    echo ""
    echo "##########################################################"
    echo "Date run is $month/$day/$year."
    echo "##########################################################"
    
    if [[ $debug == "true" ]]
    then
            echo ""
            echo "It would seem that the debug flag is set."
            echo ""
    fi
    
    ########################################################
    # If the month is January we must change year and month
    #+ to remove all files more than a month old.
    ########################################################
    if [[ month -eq 1 ]]
    then
            ((year--))
            month=12
    else
            ((month--))
    fi
    
    ########################################################
    # If, for some reason, there is a file called "dateFile"
    #+ we will be polite and not step on it.
    ########################################################
    if [ -e /var/tmp/dateFile ]
    then
            filename=`echo -n dateFile$$`
            echo "dateFile exists, using $filename instead."
    else
            filename=dateFile
    fi
    
    echo "$filename is being used."
    
    touch --date="$month/$day/$year 00:01" /var/tmp/$filename
    
    echo "Created the file $filename with timestamp $month/$day/$year."
    echo `ls -l /var/tmp/$filename`
    
    for file in `ls /tmp` ;
    do
    #       if [[ /tmp/$file -ot /var/tmp/$filename ]]
            if [ $(stat -c '%Z' "/tmp/$file") -lt $(stat -c '%Z' /var/tmp/dateFile) ]
            then
                    echo "File $file is older than $filename and will be removed."
                    if [[ $debug == "false" ]]
                    then
                            rm -rf $file
                    fi
            fi
    done
    
    #####################################################
    # Clean up the dateFile in /tmp
    #####################################################
    rm /var/tmp/$filename
    echo "Cleaned up the dateFile, $filename."
    
    #####################################################
    # Clean up the stdout and sterr
    #####################################################
    echo "Restoring stdout and stderr."
    echo ""
    
    exec 1<&5               # Restore stdout
    exec 2<&6               # Restore stderr
    exec 5<&-               # Restore stderr
    exec 6<&-               # Restore stderr
    
    
    exit

  4. #4
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Try find, something like:

    Code:
    echo "These files are older than $filename and will be removed:"
    
    find -type f -newer /var/tmp/dateFile -exec ls -l {} \;
    find -type f -newer /var/tmp/dateFile -exec rm -f {} \;
    Regards

  5. #5
    Just Joined!
    Join Date
    Apr 2007
    Posts
    3
    lol,

    well, Franklin52 certainly has cut to the chase... but I was rather hoping someone might attempt to explain why the [[ file1 -ot file2 ]] code was not working.

    Thanks Franklin, that does solve my more immediate issue.

    Generically Human

Posting Permissions

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