<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: files code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 12 Oct 2008 18:43:31 GMT</pubDate>
    <description>DZone Snippets: files code</description>
    <item>
      <title>Search and replace keywords within many files</title>
      <link>http://snippets.dzone.com/posts/show/6254</link>
      <description># search and replace all text files in the temp directory replacing the word 'five' with 'six'&lt;br /&gt;&lt;br /&gt;&lt;code&gt;glob_path, exp_search, exp_replace = 'temp/*.txt','five', 'six'&lt;br /&gt;Dir.glob(glob_path).each do |file| &lt;br /&gt;  buffer = File.new(file,'r').read.gsub(/#{exp_search}/,exp_replace)&lt;br /&gt;  File.open(file,'w') {|fw| fw.write(buffer)}&lt;br /&gt;end&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;output before:&lt;br /&gt;&lt;br /&gt;more t*.txt&lt;br /&gt;::::::::::::::&lt;br /&gt;t1.txt&lt;br /&gt;::::::::::::::&lt;br /&gt;123 five 789&lt;br /&gt;::::::::::::::&lt;br /&gt;t2.txt&lt;br /&gt;::::::::::::::&lt;br /&gt;123 four 6789&lt;br /&gt;::::::::::::::&lt;br /&gt;t3.txt&lt;br /&gt;::::::::::::::&lt;br /&gt;123 ... five five four three&lt;br /&gt;&lt;br /&gt;output after:&lt;br /&gt;&lt;br /&gt; more t*.txt&lt;br /&gt;::::::::::::::&lt;br /&gt;t1.txt&lt;br /&gt;::::::::::::::&lt;br /&gt;123 six 789&lt;br /&gt;::::::::::::::&lt;br /&gt;t2.txt&lt;br /&gt;::::::::::::::&lt;br /&gt;123 four 6789&lt;br /&gt;::::::::::::::&lt;br /&gt;t3.txt&lt;br /&gt;::::::::::::::&lt;br /&gt;123 ... six six four three&lt;br /&gt;</description>
      <pubDate>Sat, 11 Oct 2008 21:48:21 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/6254</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Search and replace filenames using Ruby</title>
      <link>http://snippets.dzone.com/posts/show/6249</link>
      <description>This script searches and replaces file names within a file directory.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/ruby&lt;br /&gt;#file: bulkrenamefiles.rb&lt;br /&gt;&lt;br /&gt;class BulkRenameFiles&lt;br /&gt;  def initialize(h)&lt;br /&gt;    h[:dir] = '.' if [:dir].empty?&lt;br /&gt;&lt;br /&gt;    files = Dir.entries(h[:dir])&lt;br /&gt;&lt;br /&gt;    files.each do |f|&lt;br /&gt;      next if f == "." or f == ".."&lt;br /&gt;      oldFile = h[:dir] + "/" + f&lt;br /&gt;      newFile = h[:dir] + "/" + f.gsub(/#{h[:exp_search]}/,h[:exp_replace])&lt;br /&gt;      File.rename(oldFile, newFile)&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;if __FILE__ == $0 then&lt;br /&gt;  brf = BulkRenameFiles.new(:dir =&gt; '.', :exp_search =&gt; '^_', :exp_replace =&gt; '')&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;output before:&lt;br /&gt;&lt;code&gt;-rwxr-xr-x 1 apache apache  178 Aug  2 16:29 class_template.xml&lt;br /&gt;-rwxr-xr-x 1 apache apache  106 Aug  2 16:29 _tasks_data_template.xml&lt;br /&gt;-rwxr-xr-x 1 apache apache  100 Aug  2 19:31 _tasks_today.xml&lt;br /&gt;-rwxr-xr-x 1 apache apache  909 Aug  2 19:31 _tasks.xsl&lt;br /&gt;-rwxr-xr-x 1 apache apache  106 Aug  2 19:56 _tasks2_data.xml&lt;br /&gt;-rw-r--r-- 1 apache apache  267 Aug  2 22:43 _tasks.xml&lt;br /&gt;-rwxr-xr-x 1 apache apache   56 Aug  5 02:10 _tasks_template.xml&lt;br /&gt;-rwxr-xr-x 1 apache apache 2686 Aug  8 19:53 index.xml&lt;br /&gt;-rwxr-xr-x 1 apache apache 2685 Aug  8 19:53 _tasks_data.xml&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;output after:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;-rwxr-xr-x 1 apache apache  106 Aug  2 16:29 tasks_data_template.xml&lt;br /&gt;-rwxr-xr-x 1 apache apache  178 Aug  2 16:29 class_template.xml&lt;br /&gt;-rwxr-xr-x 1 apache apache  100 Aug  2 19:31 tasks_today.xml&lt;br /&gt;-rwxr-xr-x 1 apache apache  909 Aug  2 19:31 tasks.xsl&lt;br /&gt;-rwxr-xr-x 1 apache apache  106 Aug  2 19:56 tasks2_data.xml&lt;br /&gt;-rw-r--r-- 1 apache apache  267 Aug  2 22:43 tasks.xml&lt;br /&gt;-rwxr-xr-x 1 apache apache   56 Aug  5 02:10 tasks_template.xml&lt;br /&gt;-rwxr-xr-x 1 apache apache 2685 Aug  8 19:53 tasks_data.xml&lt;br /&gt;-rwxr-xr-x 1 apache apache 2686 Aug  8 19:53 index.xml&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Source: &lt;a href="http://www.rubyonrailsexamples.com/ruby-files/ruby-files-renaming/"&gt;Renaming files using ruby! - by Ruby on Rails&lt;/a&gt; [rubyonrailsexamples.com]&lt;br /&gt;&lt;br /&gt;*update: 12-Oct-08 @ 16:00 *&lt;br /&gt;&lt;br /&gt;Here's a great one liner from &lt;a href="http://rush.heroku.com/handbook/globbing"&gt;rush&lt;/a&gt; [heroku.com]:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;dir['*.jpg'].each { |f| f.rename f.name.gsub(/thumb-/, 'thumbnail_') }&lt;/code&gt;</description>
      <pubDate>Sat, 11 Oct 2008 14:08:47 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/6249</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>List the 10 most recent files: Groovy version.</title>
      <link>http://snippets.dzone.com/posts/show/5924</link>
      <description>&lt;code&gt;&lt;br /&gt;def recents = new File(".").listFiles().findAll { it.file }.sort { it.lastModified() }.reverse()[0..9]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 15 Aug 2008 05:16:03 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5924</guid>
      <author>marcospereira (Marcos Silva Pereira)</author>
    </item>
    <item>
      <title>List the 10 most recent files</title>
      <link>http://snippets.dzone.com/posts/show/5892</link>
      <description>&lt;code&gt;&lt;br /&gt; aimage_file = Dir.entries(".").select{|f| File.file? f }.sort_by { |f| File.mtime(f) }.reverse[0..9]&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;=&gt; ["14_12_10GMT_1.jpg", "14_12_09GMT_1.jpg", "14_12_09GMT.jpg", "14_12_10GMT.jpg", "14_12_08GMT.jpg", "14_11_38GMT_1.jpg", "14_11_38GMT.jpg", "14_11_37GMT_1.jpg", "14_11_37GMT_2.jpg", "14_11_37GMT.jpg"]&lt;br /&gt;&lt;br /&gt;References: &lt;br /&gt; - &lt;a href="http://www.ruby-doc.org/core/classes/Dir.html"&gt;Class: Dir&lt;/a&gt; [ruby-doc.org]&lt;br /&gt; - &lt;a href="http://www.ruby-doc.org/core/classes/File.html"&gt;Class: File&lt;/a&gt; [ruby-doc.org]&lt;br /&gt;&lt;br /&gt;</description>
      <pubDate>Sun, 10 Aug 2008 14:13:15 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5892</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Toggle hidden files in Mac OS X Finder</title>
      <link>http://snippets.dzone.com/posts/show/5855</link>
      <description>Works in Leopard, should work in all versions of OS X.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/bin/bash&lt;br /&gt;&lt;br /&gt;if [ `defaults read com.apple.finder AppleShowAllFiles` == 1 ]&lt;br /&gt;then &lt;br /&gt;  echo "Hiding hidden files."&lt;br /&gt;  defaults write com.apple.finder AppleShowAllFiles -bool false&lt;br /&gt;else &lt;br /&gt;  echo "Showing hidden files."&lt;br /&gt;  defaults write com.apple.finder AppleShowAllFiles -bool true&lt;br /&gt;fi   &lt;br /&gt;     &lt;br /&gt;KillAll Finder&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 31 Jul 2008 02:00:25 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5855</guid>
      <author>Aupajo (Pete)</author>
    </item>
    <item>
      <title>Decompress several filetypes with a single script</title>
      <link>http://snippets.dzone.com/posts/show/5785</link>
      <description>// Decompresses Z, gz, bz2, zip, rar, tar, and 7z with a single 'decompress' command&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/bin/bash&lt;br /&gt;&lt;br /&gt;# decompress - will decompress a file, regardless of compression type.&lt;br /&gt;&lt;br /&gt;Z="compress -d"&lt;br /&gt;gz="gunzip"&lt;br /&gt;bz="bunzip2"&lt;br /&gt;zip="unzip -qo"&lt;br /&gt;rar="unrar x -id -y"&lt;br /&gt;tar="tar xf"&lt;br /&gt;7z="p7zip -d"&lt;br /&gt;&lt;br /&gt;if [ $# -eq 0 ]; then&lt;br /&gt;    echo "Usage: decompress file or files to decompress"&gt;&amp;2&lt;br /&gt;    exit 1&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;for name&lt;br /&gt;do&lt;br /&gt;	if [ ! -f "$name" ] ; then&lt;br /&gt;		echo "$0: file $name not found. Skipped." &gt;&amp;2&lt;br /&gt;		continue&lt;br /&gt;	fi&lt;br /&gt;&lt;br /&gt;	if [ "$(echo $name | egrep '(\.Z$|\.gz$|\.bz2$|\.zip$|\.rar$|\.tar$|\.tgz$|\.7z$)')" = "" ] ; then&lt;br /&gt;		echo "Skipped file ${name}: it's already decompressed." &lt;br /&gt;      		continue&lt;br /&gt;	fi&lt;br /&gt;&lt;br /&gt;	extension=${name##*.}&lt;br /&gt;&lt;br /&gt;	case "$extension" in&lt;br /&gt;		Z ) echo "Filetype is Z. Decompressing..."&lt;br /&gt;		    $Z "$name"&lt;br /&gt;		    ;;&lt;br /&gt;		gz ) echo "Filetype is gz. Decompressing..."&lt;br /&gt;		     $gz "$name"&lt;br /&gt;		     ;;&lt;br /&gt;		bz2 ) echo "Filetype is bz2. Decompressing..."&lt;br /&gt; 		     $bz "$name"&lt;br /&gt;		     ;;&lt;br /&gt;		zip ) echo "Filetype is zip. Decompressing..."&lt;br /&gt;		      $zip "$name"&lt;br /&gt;		      ;;&lt;br /&gt;		rar ) echo "Filetype is rar. Decompressing..."&lt;br /&gt;		      $rar "$name"&lt;br /&gt;		      ;;&lt;br /&gt;		tar ) echo "Filetype is tar. Decompressing..."&lt;br /&gt;              	      $tar "$name"&lt;br /&gt;              	      ;;&lt;br /&gt;		tgz ) echo "Filetype is tgz. Decompressing..."&lt;br /&gt;              	      $tar "$name"&lt;br /&gt;                     ;;&lt;br /&gt;               7z ) echo "Filetype is 7z. Decompressing..."&lt;br /&gt;                     $7z "$name"&lt;br /&gt;	esac&lt;br /&gt;&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;exit 0&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Fri, 18 Jul 2008 02:07:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5785</guid>
      <author>zdenton (Zach Denton)</author>
    </item>
    <item>
      <title>Listing the files and subdirectories in C - Linux</title>
      <link>http://snippets.dzone.com/posts/show/5734</link>
      <description>// program lists the files and subdirectories within a given directory in full path&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;#include &lt;stdlib.h&gt;&lt;br /&gt;#include &lt;string.h&gt;&lt;br /&gt;#include &lt;dirent.h&gt;&lt;br /&gt;&lt;br /&gt;char *path_cat (const char *str1, char *str2);&lt;br /&gt;&lt;br /&gt;int main () {&lt;br /&gt;	struct dirent *dp;&lt;br /&gt;&lt;br /&gt;        // enter existing path to directory below&lt;br /&gt;	const char *dir_path="/path/to/directory/to/list";&lt;br /&gt;	DIR *dir = opendir(dir_path);&lt;br /&gt;	while ((dp=readdir(dir)) != NULL) {&lt;br /&gt;		char *tmp;&lt;br /&gt;		tmp = path_cat(dir_path, dp-&gt;d_name);&lt;br /&gt;		printf("%s\n", tmp);&lt;br /&gt;		free(tmp);&lt;br /&gt;		tmp=NULL;&lt;br /&gt;	}&lt;br /&gt;	closedir(dir);&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;char *path_cat (const char *str1, char *str2) {&lt;br /&gt;	size_t str1_len = strlen(str1);&lt;br /&gt;	size_t str2_len = strlen(str2);&lt;br /&gt;	char *result;&lt;br /&gt;	result = malloc((str1_len+str2_len+1)*sizeof *result);&lt;br /&gt;	strcpy (result,str1);&lt;br /&gt;	int i,j;&lt;br /&gt;	for(i=str1_len, j=0; ((i&lt;(str1_len+str2_len)) &amp;&amp; (j&lt;str2_len));i++, j++) {&lt;br /&gt;		result[i]=str2[j];&lt;br /&gt;	}&lt;br /&gt;	result[str1_len+str2_len]='\0';&lt;br /&gt;	return result;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 08 Jul 2008 01:13:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5734</guid>
      <author>Tvrtko (Tvrtko)</author>
    </item>
    <item>
      <title>Linux - Change File/Directory Permissions Separately</title>
      <link>http://snippets.dzone.com/posts/show/5569</link>
      <description>Change permission of all files or all directories separately from a set starting point.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;// Directories:&lt;br /&gt;find . -type d -exec chmod XXX {} \;&lt;br /&gt;&lt;br /&gt;// Files:&lt;br /&gt;find . -type f -exec chmod XXX {} \;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 02 Jun 2008 10:21:46 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5569</guid>
      <author>k1mmeh (Kim Chirnside)</author>
    </item>
    <item>
      <title>Open an arbitrary number of resources safely in ruby</title>
      <link>http://snippets.dzone.com/posts/show/5420</link>
      <description>I'm too lazy to work out what happens if I try &lt;code&gt;filenames.map {|f| File.open(f) }&lt;/code&gt; and the thirteenth file doesnt exist, but I bet I don't like it.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;module Enumerable&lt;br /&gt;  # Example:&lt;br /&gt;  # ['a','b'].with_files {|f,g| ... }&lt;br /&gt;  # is the same as&lt;br /&gt;  # File.open('a') {|f| File.open('b') {|g| ... } }&lt;br /&gt;  # You can specify modes with&lt;br /&gt;  # [['a', 'rb'], ['b', 'w']].with_files ...&lt;br /&gt;  def with_files(&lt;br /&gt;      meth = File.method(:open),&lt;br /&gt;      offset=0,&lt;br /&gt;      inplace=false,&lt;br /&gt;      &amp;block&lt;br /&gt;  )&lt;br /&gt;    if inplace then&lt;br /&gt;      if offset &gt;= length then&lt;br /&gt;        yield self&lt;br /&gt;      else&lt;br /&gt;        fname,mode = *self[offset]&lt;br /&gt;        File.open(fname,mode) {|f| &lt;br /&gt;          self[offset] = f&lt;br /&gt;          self.with_files(meth,offset+1,true,&amp;block)&lt;br /&gt;        }&lt;br /&gt;      end&lt;br /&gt;    else&lt;br /&gt;      dup.with_files(meth,offset,true,&amp;block)&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;/code&gt;</description>
      <pubDate>Tue, 22 Apr 2008 00:44:04 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5420</guid>
      <author>tunah (Sam McCall)</author>
    </item>
    <item>
      <title>copy files using rsync and ssh</title>
      <link>http://snippets.dzone.com/posts/show/4717</link>
      <description>source: http://www.mikerubel.org/computers/rsync_snapshots/#Abstract ; switches: -a = archive mode -e specifies the remote shell to use&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;rsync -a -e ssh source/ username@remotemachine.com:/path/to/destination/&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 30 Oct 2007 13:03:36 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4717</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
  </channel>
</rss>
