LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to display the last 10 modified files of a folder? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-display-the-last-10-modified-files-of-a-folder-821634/)

nrbasavaraja 07-23-2010 02:20 AM

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...

Dinithion 07-23-2010 02:46 AM

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

hornetbzz 07-23-2010 02:52 AM

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

ghostdog74 07-23-2010 03:15 AM

Code:

ls -ltr | tail -10

druuna 07-23-2010 03:28 AM

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.

David the H. 07-23-2010 03:40 AM

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 "/$"

Dinithion 07-23-2010 03:43 AM

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

druuna 07-23-2010 03:46 AM

@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 :)

David the H. 07-23-2010 12:13 PM

Quote:

Originally Posted by druuna (Post 4042772)
@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.



All times are GMT -5. The time now is 01:44 AM.