Skip to content

Instantly share code, notes, and snippets.

@MicahElliott
Created November 29, 2010 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MicahElliott/720498 to your computer and use it in GitHub Desktop.
Save MicahElliott/720498 to your computer and use it in GitHub Desktop.
A still-concise extension of the standard ‘file’ info.
#! /usr/bin/env ruby
# about — A still-concise extension of the standard ‘file’ info.
#
# Author: Micah Elliott http://MicahElliott.com
# License: WTFPL http://sam.zoy.org/wtfpl/
#
# Combine standard file utils (eg, file, wc, head, tail, sort, stat) to
# give pieces of information about a file. Informs about file length,
# sorted-ness, and other meta-data.
#
# Should have special accomodations for specific file types. This really
# makes it a “super-file” utility, but that’s a crappy name.
#
# File types to treat specially:
# * Videos: ffmpeg -i
# * Images: identify (from ImageMagick)
# * Archives: tar, zipinfo (NYI)
#
# There are several more file types that have interesting utilties to
# tell you more about them. I’m planning to extend/improve this as I
# encounter more types. Examples:
# * language source files (stats, lint-passing, VCS-dirty, etc)
# * license info
#
# Areas for extension:
# * following of symlinks
# * recognition/printing of line-3 for script “about” summary details
# * more use of special utils for meta-info
# * reporting of extended file attributes
# (http://en.wikipedia.org/wiki/Extended_file_attributes)
# * smarter treatment of non-text files
# * color!
require 'pathname'
$usage = 'about.rb file1 …'
def print_lines lines
if lines[0..2].join.length < 3 * 80 then
puts lines[0..2]
else
puts "lines too long"
end
end
def get_info p
#map = {
# ".mp3"
fileinfo = `file -b #{p.to_s}`
util = case p.extname.downcase
when ".mp3",".mp4",".avi",".flv" then
"\n" + (`mp4info #{p.to_s} 2>&1`) + "\n"
when ".xcf",".png" then
`identify #{p.to_s} 2>&1`
else
`file -b #{p.to_s}` # exclude filename in file’s output.
end
#return `#{util} #{p.to_s} 2>&1`.split("\n")[-4..-2]
end
def about fname
print "NAME: "
puts fname
p = Pathname.new fname
realpath = p.realpath.to_s
if realpath != fname then
puts "REALPATH: #{realpath}"
end
### Don’t include filename in file’s output.
fileinfo = get_info p
print "FILEINFO: " + fileinfo
### Determine size.
print "SIZE: "
f = File.open fname
stat_size = f.stat.size
if stat_size < 10000
puts f.stat.size
else
puts `du -h #{fname}`.split[0]
end
##print "STAT: "
##system "ls -hl #{fname}"
##wc = `wc -l #{fname}`.split[0]
lines = f.readlines
wc = lines.length
puts "LINES: #{wc}"
print "SORTED: "
puts system("sort -C #{fname}") ? "yes" : "no"
if fileinfo.match "text" then
##system "stat #{fname}"
puts
if lines.length < 3
puts "only #{lines.length} lines!\n"
return
end
puts "HEAD...TAIL:"
print_lines lines[0..2]
##system "head -3 #{fname}"
puts "==8<--->8=="
print_lines lines[-3..-1]
puts
##system "tail -3 #{fname}"
#else
# exit 1
end
end
if $0 == __FILE__
if ARGV.size == 0
print $usage
exit
end
ARGV.each do |fname|
puts '---' if ARGV.size > 1
about fname
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment