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 can I fix this?

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

$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 command-line 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 interval.

closed as off-topic by muru, Gilles, Mitch Nov 12 '14 at 12:39

  • This question does not appear to be about Ubuntu within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.


Regarding part A of your question, here is a reference to the standard file tests.

In particular, what you can do is:

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

As for part B, see this stackoverflow question.


For part A, a more efficient approach:

find $1 -maxdepth 1 ! -type d -exec stat -c "%y" {} + | cut -d' ' -f1

find is excellent at filtering out based on type, and exec with + should run far fewer stat process. Piping the whole output to cut means we have to run cut only once. And there is really no need to do

echo `some command`

The only benefit is converting multi-line output to single line, which is not applicable to your case anyway.

Not the answer you're looking for? Browse other questions tagged or ask your own question.