Reputation: 145
I'm using macOSx, and i want to count all files in directory then print the result sorted by last Modified time. like this:
1241 2014-12-08 07:00:00 +0700
4123 2014-12-08 08:00:00 +0700
2241 2014-12-08 09:00:00 +0700
etc.. i tried with ruby, but i can only puts count of files with this:
dir = 'original'
puts Dir[File.join(dir, '**', '*')].count { |file| File.file?(file) }
or puts all file and sorts it by time like this:
Dir[File.join(dir, '**', '*')].sort_by{ |f| File.mtime(f) }
So how can i do this? may be with bash script, or ruby...
Upvotes: 0
Views: 439
Reputation: 145
This is my ruby code:
require "Date"
dir = 'original'
for date in 1..7 do
for hour in 0..23 do
d = DateTime.new(2014, 1, 1, hour, 0, 0)
d1 = DateTime.new(2014, 1, 1, hour+1, 0, 0)
time = d.strftime("%H:%M")
time1 = d1.strftime("%H:%M")
puts ("Files on time 12/0#{date}/2014 #{time} (from #{time} -- #{time1}) :")
puts Dir[File.join(dir, '**', '*')].count {|file|
if ("12/0#{date}/2014 #{time}" < File.mtime(file).strftime('%m/%d/%Y %I:%M')) && (File.mtime(file).strftime('%m/%d/%Y %I:%M') < "12/0#{date}/2014 #{time1}")
File.file?(file)
end
}
end
puts ("Files on time 12/0#{date}/2014 23:00 (from 23:00 -- 00:00 next day) :")
puts Dir[File.join(dir, '**', '*')].count {|file|
if ("12/0#{date}/2014 23:00" < File.mtime(file).strftime('%m/%d/%Y %I:%M')) && (File.mtime(file).strftime('%m/%d/%Y %I:%M') < "12/0#{date+1}/2014 #{time1}")
File.file?(file)
end
}
end
Upvotes: 0
Reputation: 4847
You could use tap to count files, like this:
require 'fileutils'
dir = '.'
p Dir[File.join(dir, '**', '*')]
.tap{|el| p el.length }
.sort_by{ |f| File.mtime(f) }
Which prints count and sorted array of files.
Upvotes: 0
Reputation: 136
Here is the bash script to go into folder and execute command from there.
#!/bin/bash
curdir=$(pwd)
for f in $curdir/<parent folder name>/*
do
[ -d $f ] && cd "$f" && echo Entering into $f && wc -l
done;
Upvotes: 0
Reputation: 41
If I understood correctly
Dir["Other/**"].sort_by{ |f| File.mtime(f) }.each do |item|
if File.directory? item
print "#{Dir[File.join(item, '**', '*')].count} #{File.mtime(item)}\n"
end
end
Upvotes: 0
Reputation: 16506
arr = []
count = 0
Dir.glob("/path/**/*").each { |file|
if File.file?(file)
count+=1
arr << File.mtime(file)
end
}
puts count.to_s + " " + arr.sort.last.to_s
#=> 2241 2014-12-08 09:00:00 +0700
This will not consider the sub directory files though
Upvotes: 0
Reputation: 45293
Not sure if I understand the request. do you have subdirectories or need sort all files in subdirectories as well? if not, I use this command
ls -lctr
-l (The lowercase letter ``ell''.) List in long format. (See below.) If the output is to a terminal, a total sum
for all the file sizes is output on a line before the long listing.
-c Use time when file status was last changed for sorting (-t) or long printing (-l).
-t Sort by time modified (most recently modified first) before sorting the operands by lexicographical order.
-r Reverse the order of the sort to get reverse lexicographical order or the oldest entries first (or largest files
last, if combined with sort by size
If you need count the files, use this:
find . -type f |wc -l
Upvotes: 2