16.2 Finding Oldest or Newest Files with ls -t and ls -uYour directory might have 50, 100, or more files. Which files haven't been used for a while? You might be able to save space by removing them. You read or edited a file yesterday but you can't remember its name? These commands will help you find it. (If you want a quick review of UNIX file times, see article 16.5 .) In this example, I'll show you my bin (4.2 ) directory full of shell scripts and other programs-I want to see which programs I don't use very often. You can use the same technique for directories with text or other files. The ls command has options to change the way it orders files. By default, ls lists files alphabetically - that probably won't help you find old files, but it's a good place to start this explanation. For finding old files, use the -t option. This sorts files by their modification time , or the last time the file was changed. The newest files are listed first. Here's what happens: jerry@ora ~/.bin 60 % I just added a shell script named weather yesterday; you can see it as the first file in the first column. I also made a change to my script named crontab last week; it's shown next. The oldest program in here is echoerr ; it's listed last. [1]
ls -t is also great for file time comparisons in a script (2.15 , 16.27 ) . [Personally, I find ls -t most useful when I've forgotten whether or not I've edited a file recently. If I've changed a file, it will be at or near the top of the ls -t listing. For example, I might ask, "Have I made the changes to that letter I was going to send?" If I haven't made the changes (but only think I have), my letter will most likely appear somewhere in the middle of the listing. -ML ] The -u option shows the files' last-access time instead of the last-modification time. The -u option doesn't do anything with plain ls -you have to use it with another option like -t or -l . The next listing shows that I've recently used the rtfm and rmmer files. I haven't read README in a long time, though - oops: jerry@ora ~/.bin 62 % (Some UNIXes don't update the last-access time of executable files (21.5 ) when you run them. Shell scripts are always read, so their last-access times will always be updated.) The -c option shows when the file's inode information (1.22 , 21.6 ) was last changed. The inode time tells when the file was created, when you used chmod to change the permissions, and so on. That doesn't help you find "stale" files: jerry@ora ~/.bin 64 % If you're wondering just how long ago a file was modified (or accessed), add the -l option for a long listing. As before, adding -u shows the last-access time; -c shows inode change time. If I look at the access times of a few specific files, I find that I haven't read README since 1989... jerry@ora ~/.bin 65 % - |
|