listing files recursively, sorted by time, limited head
Hi there!
I'm trying to make a shell script that will list the 50 newest files in a directory with several subdirectories in. I've been trying with the find-command with no luck and now I've figured I should probably use ls. The problem is when I do "ls -lRt | head -50" it will do 1 directory at the time. It will not first make the full list and then sort it. This will display all items in first directory, sorted, then the newest directory will be sorted and displayed. So I figured I have to sort the whole process of ls before I limit the head. So this is where I am at now: ls -lRt | sort <something clever here> | head -50 Only doing a "|sort|" will sort it by name if I understand it right and I don't know how to solve it. Here's also my first attempt if that is of any interrest or help, this was limited by the change status time of files (so some lists got very large). These lists dit not get sorted by time and I could not find any way to do so. find $ftpDir -ctime $time -type f -print > $ftpFileLs Any help on this would be appreciated since I'm sort of stuck now. After reading manuals for all the options I can think of and still there's just a big blur in my head.. |
You can use find with the -printf predicate to list the path of the files along with the last modification time. You can choose a format which is sorted both numerically and by date/time, then simply use sort, tail, cut and so on. Example:
Code:
find . -type f -printf "%TY%Tm%Td%TH%TM%TS %p\n" | sort -k1n | tail -50 | cut -d' ' -f2- |
Hello,
I recently had to come up with something very similar like that to put in a Troubleshooting Guide for a helpdesk of ours. I needed the 20 biggest files when searching through a directory recursively. This is what I came up with: Code:
find . -type f | xargs ls -lSrh | tail -n 20 | awk -F" " '{ print $5,$9 }' Code:
2.0M ./ISOPROD/VT/libvirt-0.6.3-33.el5.x86_64.rpm Kind regards, Eric |
Quote:
I am also please that i get the ./ printout instead of the full path when using the "." in find command. Lots of new nice tricks to my very limited shellscriptingvault. Here's my final result by the way: Code:
find . -type f | xargs ls -Rtlh | tail -n 50 | awk -F" " '{ print $6,$7,$9 }' |
Hi,
Glad you got a solution! And pleased that you learned something from it. That's what Linux is all about. Sharing and learning from each other. I've learned from the command posted by colucix too, so take a look at his proposal if you have the time. The combination of these two commands gives you lot's of possibilities for future problems. Forgot to mention. If you consider your question/problem solved then please mark it as such using the Thread Tools. Kind regards, Eric |
Yeah now it's marked as solved, didn't know there was such an option :) . I ran into a new problem when I tried it on my SSH though. Since the guy who needs it uses characters like ' " $ or # in his filenames for some unknown reason. The xargs return error "unmatched single quote" but I think I'm onto a solution for this with grep -v or sed command to filter it out before the xargs. I'll return with the solution when I've managed.. I get "ls:*:No such file or directory" with the methods I've tried so far.
|
Hi,
Can you post a literal example including those characters so that we could have a look at it? Also, might be worth mentioning what distro the remote machine is running. Normally you can go a long way by 'escaping' the special characters but it would be handy to have a real life example. Kind regards, Eric |
ok, so this is the line and output I'm working on now.
Code:
NAS:/media/# find . -type d | xargs ls -Rtlh | tail -n 50 | awk -F" " '{ print $6,$7,$9 }' Code:
find . -type f -exec ls -Rtlh {} \; | tail -n 50 | awk -F" " '{ print $6,$7,$9 }' I have also tried to add -0 to xargs and "find . -print0" but this will give the error "too long command line" since it will place everything in one line (and there's a whole lot of files in these folders). edit: btw It's some NAS from Netgear with redhat I think and for some reason the disks are on a windows-based system. it's a big mess really. I installed the SSH on it so I could manage this from home because his PC creeps me out. I am not even used to linux, I use OS X myself nowdays. Linux version 2.6.17.14ReadyNAS (gcc version 3.3.5 (Infrant 3.3.5-1)) |
Quote:
Code:
awk '{print substr($0,index($0,$9))}' Quote:
Code:
-exec command {} \; Said that, I would stick with the -printf predicate to print out the desired information, despite the presence of special characters in file names. If you need some clarification about its usage, feel free to ask. |
Quote:
Yeah you'r right, I don't need the -R flag on my ls-command. Thanks, it seems to be working a lot faster now as well :D I exec ls because I could not find a way to use the find-command and sort them by ctime which is what I want. This is my final piece of code and now it's all perfect except from the fact that it seems to run very slow. It's ok though since it will run at night on a server that doesn't do much else. This is what it looks like now, I figured I could likewise include the 8:th column. It looks better and doesn't matter: Code:
find . -type f -exec ls -rcl {} \; | tail -n 50 | awk -F" " '{print substr($0,index($0,""$6""))}' |
I'm sorry but I still not believe the files are sorted by ctime. The problem is that -exec executes the ls command on every single file, so that the -r option has no effect. Moreover according to the ls man page:
Code:
-c with -lt: sort by, and show, ctime (time of last modification of file status information) with Let me do a little example to demonstrate (based on modification time, but the result is the same): Code:
$ touch -t 200809011230 file1 Code:
$ ls -lrt ./file1 Code:
$ find . -type f -printf "%TY%Tm%Td%TH%TM%TS %p\n" Code:
$ find . -type f -printf "%CY%Cm%Cd%CH%CM%CS %p\n" | sort -n Code:
$ find . -type f -printf "%CY-%Cm-%Cd %CH:%CM:%CS %p\n" | sort -n |
Quote:
I tried these lines on the system that it's intended for and it seems to work just right. The "exec ls" method was not functioning properly while also taking a lot of time to complete. I will complete my script and test it properly before I mark the thread as solved this time. Once again, thanks a lot! :) |
You're welcome! :)
|
All times are GMT -5. The time now is 04:27 AM. |