Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

cwd - copy with date

unset -f cwd
function cwd() {
   declare dirname filename newfile
   if [[ ! -f "$1" ]]; then echo "No such file: ${1}"; return 1; fi
   if [[ $# -eq 1 ]]; then 
      dirname="$(/usr/bin/dirname "${1}")"
   elif [[ $# -eq 2 ]]; then
      if [[ ! -d "$2" ]]; then echo "No such directory: ${2}"; return 1; fi
      dirname="${2%/}"
   else
      echo "argument error"
      return 1
   fi
   #/bin/sleep 1
   filename="$(/usr/bin/basename "${1}")"
   newfile="${dirname}/${filename}.$(/bin/date +%Y-%m-%d-%H.%M.%S)"
   #newfile="${dirname}/${filename}.$(/bin/date +%Z-%Y-%m-%d-%H.%M.%S)"
   /bin/cp -ip "${1}" "${newfile}"
   return 0
}


cwd file
cwd file dir

How to update something using a patch (Wordpress, pmwiki etc.)

After unzipping, taring or whatever archive you used copy thepatch over the old files, replacing them.

Copy the files in your newly extracted directory (pmwik-new/wordpress-patch) over the files of your existing software installation. For example, if your existing PmWiki/Wordpress installation is in a directory called pmwiki/wordpress, then one way to copy the new files over the existing ones is to enter the command:
cp -a pmwiki-new/. pmwiki


Note that BSD systems will not have the -a option as a command-line argument for cp, but that's okay, since it's just shorthand for cp -dpR, so use that instead of -a.

On (some) FreeBSD servers and Mac OS X systems you need to use
cp -Rpv pmwiki-new/. pmwiki 

This works well on TxD shared hosting.

Copy and rename files with wildcards (globs)

To copy files jgarner_* to Alias_*:

Dir.glob("jgarner_*") { |name| `cp #{name} #{name.sub(/jgarner_/, "Alias_")}`}

or

ruby -e 'Dir.glob("jgarner_*") { |name| `cp #{name} #{name.sub(/jgarner_/, "Alias_")}`}'