LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-23-2009, 03:24 AM   #1
walidaly
Member
 
Registered: Mar 2007
Posts: 64

Rep: Reputation: 15
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
but that gives me results from only one sub-directory and they are different from
Code:
find /dir/ -type f -mmin -10
 
Old 07-23-2009, 04:14 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Use xargs to process all the file together:
Code:
find /dir -type f -iname \*.jpg -print0 | xargs -0 ls -lrt | tail -10
However, if you have a very large number of files the list of arguments passed to xargs can be too long, resulting in an error. In that case we have to find another way.

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
$ find /dir -type f -iname \*.jpg -newer /tmp/dummy.file -print0 | xargs -0 ls -lrt | tail -10

Last edited by colucix; 07-23-2009 at 04:18 AM.
 
Old 07-23-2009, 07:42 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Just FYI, Unix doesn't actually have a creation time:
Quote:
Three fields in the inode structure contain the last access, change, and modification times: atime, ctime, and mtime. The atime field is updated each time the pointer to the file's data blocks is followed and the file's data is read. The mtime field is updated each time the file's data changes. The ctime field is updated each time the file's inode changes. The ctime is not creation time; there is no way under standard Unix to find a file's creation time.
 
Old 07-24-2009, 12:30 AM   #4
jay73
LQ Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 133Reputation: 133
Quote:
Just FYI, Unix doesn't actually have a creation time:
But wouldn't you have something similar if you mounted your filesystem(s) noatime? Look at the difference between access time and the other time variables in the example here:

Quote:
devbox@Selena:~/dumpster$ stat file2
File: `file2'
Size: 7 Blocks: 8 IO Block: 4096 regular file
Device: 826h/2086d Inode: 134498235 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1001/ devbox) Gid: ( 1001/ devbox)
Access: 2009-07-23 00:04:41.012207741 +0200
Modify: 2009-07-24 07:26:07.125808381 +0200
Change: 2009-07-24 07:26:07.125808381 +0200

Last edited by jay73; 07-24-2009 at 12:36 AM.
 
Old 07-24-2009, 01:05 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by chrism01 View Post
Just FYI, Unix doesn't actually have a creation time:
That's right. In this case we don't speak about "creation date". Just a matter of terminology: "file created", in place of "file modified". I would add that the new ext4 filesystem will store creation time as well. Here is a comparison table of filesystems that shows which support cration timestamps and which not.
 
Old 07-24-2009, 01:58 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.
 
Old 07-24-2009, 03:36 AM   #7
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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-
The timestamp in seconds is printed, followed by a tab, and then the filename.
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.

Last edited by jschiwal; 07-24-2009 at 03:53 AM.
 
Old 07-24-2009, 07:51 PM   #8
walidaly
Member
 
Registered: Mar 2007
Posts: 64

Original Poster
Rep: Reputation: 15
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
 
Old 07-25-2009, 02:45 PM   #9
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.

Last edited by jschiwal; 07-25-2009 at 07:32 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sort files under a folder by created date Kunsheng Programming 3 05-11-2009 04:58 AM
Group write access for newly created files/directories without changing umask fhd Linux - Security 3 04-05-2009 05:28 AM
[SOLVED] files / directories are created when we install a RPM vikas027 Linux - Software 3 09-29-2008 09:59 AM
sticky bit: how to protect directories but not files inside alexandrusa Linux - Server 8 03-27-2008 10:35 AM
Getting a list of directories with certain files inside... Banacek Linux - Newbie 7 11-27-2006 05:43 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration