0

A)I used the following code to get the modified date of a file. But this is also selecting the subdirectories and thus returning an error. How van I fix this.

for file in $1/*; do
        echo `stat -c %y $file | cut -d ' ' -f1`
done

$1 is the directory I am passing

B) Also is there any way to do this thing. That I need to pass a directory and date range as commandline argument and list the file and modified date which lies in this date range. Eg output.sh /home/user/desktop 2014-10-07 2014-11-17 should list all the files in this directory which are modified in this time intervel

1

1 Answer 1

1

To find all files in directory $1 that are older than 1000 minutes and younger than 2000 minutes:

find "$1" -maxdepth 1 -type f -mmin +1000 -mmin -2000

If you want to search recursively into subdirectories, remove -maxdepth 1.

If you prefer to specify time in integral days, rather than minutes, use -mtime:

find . -maxdepth 1 -type f -mtime +20 -mtime -26

Loop over files while skipping directories

-d can be used to test for directories and continue will skip over them:

for file in "$1"/*; do
    [ -d "$file" ] && continue
    echo $(stat -c %y $file | cut -d ' ' -f1)
done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .