LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   ls recursive AND last modified simultaneously (https://www.linuxquestions.org/questions/linux-newbie-8/ls-recursive-and-last-modified-simultaneously-4175450718/)

ted_chou12 02-18-2013 07:04 PM

ls recursive AND last modified simultaneously
 
Hi, I wish to list the directories recursively and at the same time to have the order coming out as last modified:
Code:

sh-3.2# ls -ltR
script:
total 44
-rwxr-xr-x 1 root root  108 Oct 11 17:08 login.sh
-rwxr-xr-x 1 root root 3108 Aug 22 03:13 forumtorrent.sh
-rw-r--r-- 1 root root  385 Apr  7  2012 transmission.py
-rwxr-xr-x 1 root root 1241 Feb 19  2012 blogrss.pl
-rwxr-xr-x 1 root root 1260 Feb 11  2012 signin.sh
-rwxr-xr-x 1 root root  272 Dec 19  2011 blog.sh
-rwxr-xr-x 1 root root  659 Dec 13  2011 forumadd.sh
-rwxr-xr-x 1 root root  392 Dec 13  2011 ftpbackup.sh
-rwxr-xr-x 1 root root 2089 Dec 13  2011 mailtoattachment.sh
-rwxr-xr-x 1 root root 1076 Nov 23  2011 ftpupload.sh
-rwxr-xr-x 1 root root 2179 Nov 19  2011 smbclient.sh

transmission:
total 208
-rwxr--r-- 1 root root  6969 Mar 26  2012 transmission-remote-cli.pod
-rwxr--r-- 1 root root  35147 Mar  5  2012 COPYING
-rwxr--r-- 1 root root    752 Mar  5  2012 NEWS
-rwxr--r-- 1 root root  2714 Mar  5  2012 README.md
-rwxr--r-- 1 root root  10512 Mar  5  2012 screenshot.png
-rwxr-xr-x 1 root root  3749 Mar  5  2012 transmission-remote-cli.1
-rwxr-xr-x 1 root root 139161 Mar  5  2012 transmission-remote-cli.py

If this is done, then the files in two directories are separated, and this is not what I want, I want to sort them seemlessly together:
Code:

-rwxr-xr-x 1 root root  108 Oct 11 17:08 login.sh
-rwxr-xr-x 1 root root 3108 Aug 22 03:13 forumtorrent.sh
-rw-r--r-- 1 root root  385 Apr  7  2012 transmission.py
-rwxr--r-- 1 root root  6969 Mar 26  2012 transmission-remote-cli.pod
-rwxr--r-- 1 root root  35147 Mar  5  2012 COPYING
-rwxr--r-- 1 root root    752 Mar  5  2012 NEWS
-rwxr--r-- 1 root root  2714 Mar  5  2012 README.md
-rwxr--r-- 1 root root  10512 Mar  5  2012 screenshot.png
-rwxr-xr-x 1 root root  3749 Mar  5  2012 transmission-remote-cli.1
-rwxr-xr-x 1 root root 139161 Mar  5  2012 transmission-remote-cli.py
-rwxr-xr-x 1 root root 1241 Feb 19  2012 blogrss.pl
-rwxr-xr-x 1 root root 1260 Feb 11  2012 signin.sh
-rwxr-xr-x 1 root root  272 Dec 19  2011 blog.sh
-rwxr-xr-x 1 root root  659 Dec 13  2011 forumadd.sh
-rwxr-xr-x 1 root root  392 Dec 13  2011 ftpbackup.sh
-rwxr-xr-x 1 root root 2089 Dec 13  2011 mailtoattachment.sh
-rwxr-xr-x 1 root root 1076 Nov 23  2011 ftpupload.sh
-rwxr-xr-x 1 root root 2179 Nov 19  2011 smbclient.sh

Something close to this.
Thanks,
Ted

ted_chou12 02-18-2013 08:10 PM

After some research and trials:
Code:

find . -type f  -printf "%T+\t%p\n" | sort | sed 's/^.*\s.\///i'
It seems a lot more trivial than I thought it would be though.

linosaurusroot 02-18-2013 08:34 PM

Code:

#! /usr/bin/perl -w
    eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
        if 0; #$running_under_some_shell

use File::Find ();

# for the convenience of &wanted calls, including -eval statements:
use vars qw/*name *dir *prune/;
*name  = *File::Find::name;
*dir    = *File::Find::dir;
*prune  = *File::Find::prune;

sub wanted;

%mstamp=();
# Traverse desired filesystems
File::Find::find({wanted => \&wanted}, '.');
foreach my $f (sort {$mstamp{$b} <=> $mstamp{$a}} keys %mstamp){
    system("ls", "-ld", $f);
}
exit;

sub wanted {
    my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
    $atime,$mtime,$ctime,$blksize,$blocks)=();

    ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
    $atime,$mtime,$ctime,$blksize,$blocks)=lstat($_);
    return unless (-f _);
    $mstamp{$name}=$mtime;
}


ted_chou12 02-18-2013 11:12 PM

Thanks for your solution, ill post the test result when i get back.


All times are GMT -5. The time now is 01:02 PM.