LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-23-2010, 03:20 AM   #1
nrbasavaraja
LQ Newbie
 
Registered: Jun 2009
Posts: 1

Rep: Reputation: 0
how to display the last 10 modified files of a folder?


Hi All,

I need to know the last 10 modified files in a folder. Can any one help me regarding this?

Thanks in advance for your help...
 
Old 07-23-2010, 03:46 AM   #2
Dinithion
Member
 
Registered: Oct 2007
Location: Norway
Distribution: Slackware 14.1
Posts: 446

Rep: Reputation: 59
There is a lot of ways of doing this. Is the output supposed to be processed by a script of some sort? If not, you could simply do something like this:

Code:
ls -l --sort=time | head -n 11
where '-l' options give a list output, and --sort will sort after the timeflag specified. If no is specified (Like here) the default, modification time, will be used. The last statment will print the top 11 lines. This will include a summary line at the top, therefor 11 lines instead of 10. To remove this, you could filter the bottom ten of the top 11 entries:

Code:
ls -l --sort=time | head -n 11 | tail

There is probably more elegant ways to do this. This is something I cooked up in a few seconds without much thinking :P
 
Old 07-23-2010, 03:52 AM   #3
hornetbzz
LQ Newbie
 
Registered: Jun 2010
Posts: 8

Rep: Reputation: 0
I'd do this as a noob too, so for sure it does exist a better solution :

Code:
find /tmp -type f -mtime 10 | ls -ltsha | tail -n 10
means :

/tmp : search in /tmp directory
-type f : search for files (and not for directories)
-mtime 10: limit to files that have been modified in the last 10 days
ls: show this list result
tail -n 10: keep only the 10 last ones

more details about find command:
Quote:
man find
 
Old 07-23-2010, 04:15 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
ls -ltr | tail -10
 
Old 07-23-2010, 04:28 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

The command posted by ghostdog74 works, but it might have an unwanted side effect: It will also show possible directories.

The OP doesn't mention this, but if only files are wanted and directories need to be left out:

find . -maxdepth 1 -type f | xargs ls -ltr | tail -10

Hope this helps.
 
Old 07-23-2010, 04:40 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
To filter out directories, use the -p option to add a trailing slash to them, then grep it out. And instead of -l (ell) use -1 (one) to get a list of the filenames only. Grep can also output a specified number of lines with -m, so you can avoid running it through head or tail.

Code:
ls -1tp | grep -v -m 10 "/$"

Last edited by David the H.; 07-23-2010 at 04:42 AM.
 
Old 07-23-2010, 04:43 AM   #7
Dinithion
Member
 
Registered: Oct 2007
Location: Norway
Distribution: Slackware 14.1
Posts: 446

Rep: Reputation: 59
I think ghostdogs solution was slick.

druuna: Everything is a file :P

I usually like ls better, so I took the freedom to modify ghostdogs version to remove directories:

Code:
ls -ltr | grep -v ^d | tail -10
 
Old 07-23-2010, 04:46 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
@David the H.: Nice, but....

- It will show links (don't know if the OP wants that).
- It will not show dot files (again, don't know if OP wants these).

I guess the OP has enough possibilities by now to suit his demands

Last edited by druuna; 07-23-2010 at 01:26 PM. Reason: Fixed spelling error.
 
Old 07-23-2010, 01:13 PM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by druuna View Post
@David the H.: Nice, but....

- It will show links (don't know if the OP wants that).
- It will not show dot files (again, don't know if OP wants these).

I guess the OP has enough possibilities by know to suit his demands
True, but not hard to fix. Simply add -a or -A to the ls command to show dot files. As for links and other file-types, the -F or --file-type options can be used instead of -p, and different characters will be appended for the different kinds of files. According to the ls info page:
Code:
`-F'
`--classify'
`--indicator-style=classify'
     Append a character to each file name indicating the file type.
     Also, for regular files that are executable, append `*'.  The file
     type indicators are `/' for directories, `@' for symbolic links,
     `|' for FIFOs, `=' for sockets, `>' for doors, and nothing for
     regular files.  Do not follow symbolic links listed on the command
     line unless the `--dereference-command-line' (`-H'),
     `--dereference' (`-L'), or
     `--dereference-command-line-symlink-to-dir' options are specified.

`--file-type'
`--indicator-style=file-type'
     Append a character to each file name indicating the file type.
     This is like `-F', except that executables are not marked.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
need help - script to display modified date&time of files Pawang Linux - General 3 06-01-2010 03:40 AM
cannot modified files in etc folder..... saqibsh Linux - Software 1 03-26-2010 10:24 AM
Sorting all files of home folder modified after a specific date for backup fc6_user Linux - General 2 03-30-2007 03:24 AM
gnu make recompiles the source files fully even though the files are not modified yasothamani Linux - Software 4 02-07-2007 07:36 AM
Display a list of files in a folder in apache Snorideguy Linux - Software 4 02-26-2004 02:05 PM


All times are GMT -5. The time now is 05:52 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration