I'm doing a LRU script, but after 20 hours working on it, I have a problem with recursive mode that I can't fix.

I just need a command that show me files sorted by access time (--time=atime); I want to manage the depth too, but if I can't, it's okay too.

  • Main directory :
    • File 1
    • Directory 1 :
      • File 1
      • File2
      • Subdir 1 :
        • file1
        • file 2
    • Directory 2 :
      • File 1
      • File 2
      • Subdir 2 :
        • file 1
        • file 2
        • subdir 3 :
          • file 1

I want to exclude directory, just having file sorted by access time


/Main Directory/Directory 1/file 1

/Main Directory/File 1

/Main Directory/Directory 1/Subdir 1/file 2

/Main Directory/Directory 2/Subdir 2/subdir 3/file 1


  • I forgot to say hello, my bad ! the edit does not work, so hello to everyone that come here ! – Meow Oct 31 '16 at 11:26
  • 1
    Please edit your post and show us what you have tried. – fd0 Oct 31 '16 at 12:59

There are six very commonly used tools to solve similar problems:

  • find, to look for files or directories matching specific entries.

    The -mindepth and -maxdepth options control how deep in the filesystem tree (relative to the specified names, which are always at depth 0) the command will work on.

    The -type option is useful for restricting consideration to files, directories, symbolic links, or devices.

    The -printf option is extremely useful, as it makes the command print out the information on the matching names (directory items) in the desired format. I particularly like %TY%Tm%Td %TT %p\n, which prints the date and time of the last modification, and full path and name of each match on each line, using format YYYYMMDD HH:MM:SS.sss PATH. This format sorts correctly, you see. For last access, use %AY%Am%Ad %AT %p\n, but note that access timestamps are not recorded at all if noatime mount option is used, or if relatime mount option is used, access timestamps are only modified for the first access after a modification; least-recently-used checking is thus not reliable. (Least-recently-modified list, however, is pretty reliable; the users can modify the timestamps by hand, but otherwise they are maintained automatically.)

  • sort to sort the output.

    The -d, -g, -h, -M, and -n options define how items are compared, and the -R option makes the order random.

    The -r option can be used to reverse the sort order (used in addition to one of the above options).

    The -t option redefines how fields (columns) are defined; by default, blanks (spaces and tabs) separate columns.

    The -k option can be used to define which part of each line is considered the sort key; by default, the entire line is considered.

  • uniq is often used after sorting to combine multiple consecutive items into one -- so that only the unique lines are output.

  • cut is the simplest way to pick only specific columns from each line in the output.

    The -f option chooses the fields to be printed. (By default, lines with at most one field (no separators) are printed; option -s suppresses printing such lines.)

    The -d option can be used to redefine the definition of a field; by default, blanks separate fields.

  • sed is a powerful stream editor, which applies regular expressions to the input, filtering and modifying it as needed.

  • awk is an interpreter for the awk language. Awk scripts are basically collections of actions, snippets of code, that are executed for each line (or before or after all processing, or if the line (or record) matches some rule).

This particular problem can be solved using three of the above commands in a simple pipeline: use find to find files at the desired depths of the tree, printing a sortable date and time for each file, plus the relative path to the file; sort the output; remove the date and time part of each line, leaving just the relative path to each file on each line.

  • Thanks a lot but I couldn't create something usefull – Meow Nov 14 '16 at 14:49
  • 2
    @GummyCat: Why didn't you experiment, then? Say, starting with something like find . -maxdepth 3 -type f -printf '%AY%Am%Ad %AT %p\n', until you're satisfied it covers the right files. Then, pipe the output through through | sort -g to see it in order, oldest accesses first. To remove the access time from the output, also pipe through | cut -d ' ' -f 3- (which outputs the third and following fields on each line). Where exactly did you go wrong? Or was it more due to not enough experimentation? Be brave, and experiment! – Nominal Animal Nov 15 '16 at 1:27
  • I'm a newbie and it was my fault to ask this question that late. I should have made a thread way more earlier, because the day I've made that post, it was the day I had to send my work to my teacher. By the way, you answer is god damn usefull ! Just at the moment, I did not had enough time to experiment, it was my fault at all. Still thank you ! – Meow Nov 15 '16 at 8:22
  • 1
    @GummyCat: You're welcome. I do use these commands every single day, myself -- well, at least some of them --, and they come in handy. For that reason, I do recommend you get familiar with them as soon as possible, regardless of your course pace. If you add the -exec option to find -- say, using something like -exec grep -lie 'pattern' '{}' +, you can find for specific files, but automagically grep for pattern in only those files. The -i grep option means case insensitive, and -l (dash ell, for list) that only file names are output. It will be worth your time to learn these. – Nominal Animal Nov 15 '16 at 9:19
  • Yep, someone in my class know how to use them perfectly and he told me that it's really powerfull. I will take a closer look at your answer for my next work ! – Meow Nov 16 '16 at 8:20

The easiest method by far is to use zsh. Its glob qualifiers can match and sort files based on their type, timestamps and other properties.

print -lr -- *(.Doa)
print -lr -- **/*(.Doa)

The first command prints the names of the regular files (.) in the current directory including dot files (D) sorted by access time (oa). The second command lists files in the current directory and in its subdirectories recursively.

  • Well, thanks a lot, I think that you saved a lot of time to a lot of people because the whole class didn't found something usefull, then you came. Thank you ! – Meow Nov 14 '16 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.