sort files inside a directory and sub-directories by latest created
I have a huge number of JPG files inside a directory and its sub directories, I need to select the latest 10 files created.
I tried Code:
ls -tR /dir/ | grep .jpg | head -n 20 Code:
find /dir/ -type f -mmin -10 |
Use xargs to process all the file together:
Code:
find /dir -type f -iname \*.jpg -print0 | xargs -0 ls -lrt | tail -10 PS - You can restrict the search using -newer. For example, if you know that the last 10 files are created in the last 3 hours you can do something like: Code:
$ touch -t $(date -d "3 hours ago" +%Y%m%d%H%M) /tmp/dummy.file |
Just FYI, Unix doesn't actually have a creation time:
Quote:
|
Quote:
Quote:
|
Quote:
|
For most people, mtime is what they really care about. Sometimes atime if its a read-only file.
Up until/including ext3, you have to embed the creation date in the filename (or store it elsewhere). eg somelogname_YYYYMMDD-HHMM.log so it sorts easily. |
The output of the a timestamp field needs to be in a form that is sortable. The output of "ls -l ..." can change form depending on the age of the file.
Here I use seconds, which a numeric sort handles easily. Code:
find /path/to/pics/dir/ -iname "*.jpg" -printf "%A@\t%p\n" | sort -nr | head -n 10 | cut -f2- To deal with filenames with whitespace, pipe the output through "tr '\n' '\0'" and use "xargs -0" to process the files. You might want to add an -mtime option in the find command to limit the number of files `find' finds. A very long list could cause the sort command to run to slow. The -mtime argument you use might return 100 filenames, for example, but this is a lot better than piping 10,000 lines into sort. |
that is very helpful jschiwal...thank you
another thing... how to extract the file names from image path and put them to a file I tried Code:
find /path/to/pics/dir/ -iname "*.jpg" -printf "%A@\t%p\n" | sort -nr | head -n 10 | grep "^.*([^/]*).jpg$" > file |
You need the pathnames to move the files.
You could use sed to filter the first list, to produce a list of filenames without the directory part: sed 's/.*\/\([^/]*$\)/\1/' file You could also rerun the original command, but use -printf "%A@\t%f\n". Provided a new file wasn't saved in the meantime. The -printf command is very flexible. Use it to format the results the way you need them. |
All times are GMT -5. The time now is 11:40 AM. |