I'm looking for some help with a command. I have a large-ish directory of files. What I want to do is sort these a little better.

For example, I want to see how many files and directories I have that have a unique year (so I guess I end up with a range of years in output).

I have been trying by list with ls -l and I added in grep so ls -l | grep -c but its the unique date that is catching me.

Anybody help?

closed as unclear what you're asking by Jeff Schaller, Stephen Rauch, Kusalananda, don_crissti, Anthon Oct 3 '17 at 18:37

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Use find and -regex – Raman Sailopal Oct 3 '17 at 15:09
  • 3
    Any hint about where the year is, please? In a filename? How are the filenames formatted? In a timestamp? Inside the file somewhere? – Kusalananda Oct 3 '17 at 15:45

Try using:

ls -l --time-style=long-iso | grep -c ' 2017-'

Replace 2017 with whatever 4 digit year you want. The --time-style option will cause ls to print ISO 8601 timestamps instead of the short, vague human readable stuff it does by default. The expression passed to grep will then match the year at the beginning of the timestamp, and should just pull out the lines for the files that were modified that year (providing you have no filenames that start with those four digits followed by a dash).

Note that this will likely only work with the GNU implementation of ls (I know for a fact it doesn't work with the busybox, FreeBSD, NetBSD, or MINIX implementations, and I'm pretty sure it doesn't work with the Solaris one either).

As requested in the comments, the following (long and convoluted) command can be used to get the number of individual years represented:

ls -l --time-style=long-iso | tail +2 |  sed -n 's/  */ /gp' - | cut -d ' ' -f 6 | cut -d '-' -f 1 | sort -u | wc -l

The tail command strips off the first (useless) line showing totals. The sed command collapses runs of spaces to single spaces. The first cut command pulls out the year-month-day field, the second pulls just the year out of that, which the sort command then reduces to one instance of each year, and the wc command finally spits out the number.

  • Thanks for that Austin. If I wanted to count all the years, how you I go about doing that. Unique years, so if I have files from 2014, 2016 & 2017 it prints out '3' as in you have three years with files with these years. Would I be going 0-2017 or use some sort of wildcard? Hope that makes sense. – Costacoffee Oct 3 '17 at 19:33
  • @Costacoffee Updated the answer with a command to do that. – Austin Hemmelgarn Oct 3 '17 at 19:49
  • Perfect, thanks Austin. That's some command! Thanks for the help. – Costacoffee Oct 3 '17 at 20:13

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