Question

I have 2 variables, which contains dates like this: 2001.10.10

And i want to use ls with a filter, that only list files if last modified were between the first and second date

Was it helpful?

Solution 2

Sorry, it is not the simplest. I just now developed it, only for you. :-)

ls -l --full-time|awk '{s=$6;gsub(/[-\.]/,"",s);if ((s>="'"$from_variable"'") && (s<="'"$to_variable"'")) {print $0}}';

The problem is, that these simple commandline tools doesn't handle date type. So first we convert them to integers removing the separating "-" and "." characters (by you is it ".", by me a "-" so I remove both, this can you see in

gsub(/[-\.]/,"",s)

After the removal, we can already compare them with integers. In this example, we compare them with the integers $from_variable and with $to_variable. So, this will list files modified between $from_variable and $to_variable .

Both of "from_variable" and "to_variable" need to be environment variables in the form 20070707 (for 7. July, 2007).

OTHER TIPS

The best solution I can think of involves creating temporary files with the boundary timestamps, and then using find:

touch -t YYYYMMDD0000 oldest_file
touch -t YYYYMMDD0000 newest_file
find -maxdepth 1 -newer oldest_file -and -not -newer newest_file
rm oldest_file newest_file

You can use the -print0 option to find if you want to strip off the leading ./ from all the filenames.

If creating temporary files isn't an option, you might consider writing a script to calculate and print the age of a file, such as described here, and then using that as a predicate.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top