Ruby sample scripts - file operations

Submitted by Chandrashekar Babu on August 15, 2010 - 4:08pm.
::

calc-dir-size.rb

#!/usr/bin/env ruby
size = 0
Dir.open(ARGV[0]).each { |file| size += File.size? ARGV[0] + "/" + file }
puts size




calc-dir-size.sh

#!/bin/bash
SIZE=0
DIR=$1

for file in $DIR/*
do
((SIZE+=$(stat -c "%s" "$file")))
done
echo $SIZE




calc-dir-size2.rb

#!/usr/bin/env ruby
# Run as below:
# ls -l /bin | ruby calc-dir-size2.rb

total = 0
STDIN.read.each {|line| total += line.split(" ")[4].to_i }
puts total




calc-dir-size2.sh

#!/bin/sh
DIR=$1

expr $(ls -l $DIR | sed 's/ \+/ /g' | cut -f5 -d' ' | xargs | sed 's/ / + /g')




calc-dir-size3.sh

#!/bin/sh
DIR=$1
ls -l $DIR | awk '{ total += $5 } END { print total }'




check-file.rb

#!/usr/bin/env ruby
if ARGV.length < 1
puts "Enter filename/path: "
file = gets
else
file = ARGV[0]
end

# Generic redundant code below. Commented.
#puts "File #{file} exists." if File.exist? file
#puts "File #{file} is a regular file." if File.file? file
#puts "File #{file} is a directory." if File.directory? file
#puts "File #{file} is a symlink." if File.symlink? file
#puts "File #{file} is a block device file." if File.blockdev? file
#puts "File #{file} is a character device file." if File.chardev? file
#puts "File #{file} is a socket file." if File.socket? file
#puts "File #{file} is a FIFO file." if File.pipe? file
#puts "-" * 40
#puts "File #{file} is readable." if File.readable? file
#puts "File #{file} is writable." if File.writable? file
#puts "File #{file} is executable/searchable." if File.executable? file
#puts "File #{file} has SETUID-bit set." if File.setuid? file
#puts "File #{file} has SETGID-bit set." if File.setgid? file
#puts "File #{file} has sticky-bit set." if File.sticky? file
#puts "File #{file} is owned by current effective user." if File.owned? file
#puts "File #{file} is owned by current effective group." if File.grpowned? file
#puts "File #{file} is empty." if File.zero? file

# Smarter code below.
file_methods = {
:exist? => "exists",
:file? => "is a regular file",
:directory? => "is a directory",
:symlink? => "is a symbolic link",
:blockdev? => "is a block device file",
:chardev? => "is a character device file",
:pipe? => "is a FIFO file",
:socket? => "is a UNIX socket file",
:readable? => "is readable",
:writable? => "is writable",
:executable? => "is executable/searchable",
:setuid? => "has set-UID bit set",
:setgid? => "has set-GID bit set",
:sticky? => "has sticky bit set",
:owned? => "is owned by current effective user",
:grpowned? => "is owned by current effective group",
:zero? => "is an empty file"
}

file_methods.each {|method, message| puts "File #{file} #{message}" if File.send method, file }




chmod.rb

#!/usr/bin/env ruby
File.chmod 0644, "test.txt"




cp.rb

#!/usr/bin/env ruby

# Validations... validations... validations...
if ARGV.length < 2
STDERR.write "usage: #{$0} source-file destination-file.\n"
exit 1
end

unless File.file? ARGV[0]
STDERR.write "#{$0}: #{ARGV[0]}: not a regular file.\n"
exit 2
end

if File.exists? ARGV[1]
print "Destination #{ARGV[1]} already exists. Overwrite (y/n) ? "
exit 3 unless gets.upcase.start_with? "Y"
end

# Phew, this is where the actual work gets done.

# The commented code below is the pythonic way of doing it.
# File.open(ARGV[0]) do |src|
# File.open(ARGV[1], "w") do |dst|
# dst.write src.read
# end
# end

# The code below is the Rubyist way of doing it ;-)
puts "Copied #{File.open(ARGV[1], "w").write(File.open(ARGV[0]).read)} bytes from #{ARGV[0]} to #{ARGV[1]}"




delete-file.rb

#!/usr/bin/env ruby
if ARGV.length < 1
print "Enter file to delete: "
file = gets.chomp!
else
file = ARGV[0]
end

File.delete file




listdir.rb

d = Dir.new(ARGV[0])
d.each {|file| puts file }




recursive-copy.rb

#!/usr/bin/env ruby
require 'Fileutils'

if ARGV.length < 2
STDERR.write "usage: #{$0} source-file destination-file.\n"
exit 1
end

unless File.file? ARGV[0]
STDERR.write "#{$0}: #{ARGV[0]}: not a regular file.\n"
exit 2
end

if File.exists? ARGV[1]
print "Destination #{ARGV[1]} already exists. Overwrite (y/n) ? "
exit 3 unless gets.upcase.start_with? "Y"
end

Fileutils.cp_r ARGV[0], ARGV[1]




rename-file.rb

#!/usr/bin/env ruby
if ARGV.length < 2
STDERR.write "usage: #{$0} source-file destination-file.\n"
exit 1
end

unless File.file? ARGV[0]
STDERR.write "#{$0}: #{ARGV[0]}: not a regular file.\n"
exit 2
end

if File.exists? ARGV[1]
print "Destination #{ARGV[1]} already exists. Overwrite (y/n) ? "
exit 3 unless gets.upcase.start_with? "Y"
end

File.rename ARGV[0], ARGV[1]




scan-setuid-files.rb

#!/usr/bin/env ruby
Dir.open(ARGV[0]).each { |file| puts file if File.setuid? ARGV[0] + "/" + file }