Tagged: UNIX

Rsync backup

The scripts, below, allow you to sync directories or drives easily. Great for backups.

Save the scripts to text files with the .sh extension. Make them executable (chmod +x).

syncDir.sh

#!/bin/sh if [ $# -ne 2 ]; then echo 1>&2 "USAGE: syncDir.sh /source/dir /backup/dir/" exit 127 fi rsync --delete -av "$1" "$2"

Usage, in terminal window: path/to/syncDir.sh /source/dir/ /backup/dir/. It is recommended to set up mySync.sh, second script below, to execute syncDir.sh and supply the arguments for source directory and target backup directory.

mySync.sh
Customize the paths to the syncDir.sh script, your source directory and the target backup directory.

~/scripts/syncDir.sh /Volumes/sourceDir/ /Volumes/backupDir

Execute the script in a terminal window (drag and drop and hit enter, or enter the path to the script and hit enter), or set up a cron job to execute this script on a schedule.


Important note!
Seems that sometimes we need to run rsync as superuser, to allow the “–delete” flag to do it’s work deleting or moving files that you have deleted or moved on your source directory. So you can run syncDir.sh as superuser by typing:

sudo ~/scripts/syncDir.sh /Volumes/sourceDir/ /Volumes/backupDir

I recommend testing these scripts thoroughly, with temporary directories and files, before using them on valuable data!

vi text editor introduction

vi, text editing program for the Unix/Mac terminal.

To open vi, open a file, or create a file type: vi file_name in a terminal window. Or more specifically vi path/to/filename.ext. For example: vi ~/Desktop/hello.sh will open, or create, a file on the Desktop called hello.sh.

To start adding text you need to be in insert mode! hit the i key to get into insert mode.

To delete text, in insert mode, press x for single characters. Press dw for entire words. Press dd to delete entire lines.

To save a file, first hit the ESC key, to enter “command mode”, then type: :w, then hit ENTER (colon+w ENTER)

To exit vi , first hit the ESC key, then type : :q, then hit ENTER (colon+q ENTER)

You can save AND exit by typing :wq, while in command mode.

To move to the beginning of a line, in command mode hit 0 (zero), the end hit $.

Vi cheat sheet

more info here
and here

Shell Scripting Notes – Apple Primer

Shell scripting primer from Apple:

http://developer.apple.com/mac/library/documentation/opensource/conceptual/shellscripting/Introduction/Introduction.html

poem.txt
foo.txt

Samples:

You can use regular expressions to search for strings in a file or a block of text by using the grep command. For example, to look for the word “bar” in the file foo.txt, you might do this:

grep "bar" foo.txt # or cat foo.txt | grep "bar"

Positional anchors allow you to specify the position within a line of text where an expression is allowed to match. There are two positional anchors that are regularly used: caret (^) and dollar ($). When placed at the beginning or end of an expression, these match the beginning and end of a line of text, respectively.

For example:

# Expression: /^Mary/ grep "^Mary" < poem.txt

This matches the word “Mary”, but only when it appears at the beginning of a line.

How to tar and compress files from the Terminal

Tar stands for “tape archive”, and it is a standard archiving method used with Unix systems. Instructions below are for archiving (optionally compressed) and unarchiving tar files from a UNIX shell or the Mac Terminal.

tar video on YouTube

Tar Rant

Lets say you want to back up all of your Users
tar -cvf myTarFile.tar Users
that assumes you are at /
so the User folder will be part of the structure
or you can:
cd /Users
tar -cvf myTarFile.tar *
and that would back up all the users too
or:
tar -cvf myTarFile.tar wolf
would just back up a wolf folder
good so far?
the c flag is for “create” (the tar file)
v is to view all of the files being added
(that is optional)
f is for the name of the tar file being created
you can use a path there, so as to put it where you want it.
if you want to compress the tar file on the fly, add the z flag, and name the tar file so you know that it is compressed
such as:
tar -czvf myTarFile.tar.gz wolf
or if you prefer bzip2:
tar -cjvf myTarFile.tar.bz2 wolf
use j for bzip
the order of the flags does sometimes matter, you will notice that we are creating the tar file first (with the c flag) then compressing it with the z or j flag

Next – we have to learn how to extract a tar file
actually – lets just look at whats inside a tar file first
tar -tvf myTarFile.tar
that will print the contents to the screen
tv is easy to remember – think of that you want to see the contents on your ‘tv”
to view a compressed file:
tar -ztvf myTarFile.tar.gz
the z comes first, because it has to uncompress it before it can print the contents on your “tv”
same but for bzip2:
tar -zjtvf myTarFile.tar.bz2
we just use a j instead of the z
note that if you want to search for a specific file you can use grep such as:
tar -ztvf myTarFile.tar.gz | grep -i someFileName
the -i for grep tells it to ignore the case (capital/lower)
to extract the whole archive, it is the same as viewing, except we just add one flag to extract it
tar -zxvf myTarFile.tar.gz
and instead of the t
that will print all of the files being extracted to the screen, if you want it to not print the files, omit the v flag:
tar -xf myTarFile.tar.gz
oops except that is compressed, so we need the z
tar -zxf myTarFile.tar.gz
if you want to extract an specific file, you just have to specify the file (and the path if any)
so to extract a file called porn.m4v from Documents you woud:
tar -zxvf myTarFile.tar.gz Documents/porn.m4v
and that is pretty much all there is to it!

“Tar Rant” by Jeshua Lacock from OpenOSX

Rename multiple files with Mac shell script

Simple renaming and renumbering script for multiple files

num=0 for j in *.jpg do mv "$j" `printf "myFILE.%05d.jpg" $num` num=`echo "$num + 1" | bc` done

Usage: In any text editor, modify the script so that the extension in the second line (here .jpg) exactly matches the extension of the files you want to modify. Also, change the file name in line four (here myFILE) to whatever name you want to use. In the Terminal, cd to the directory with your files. Paste the script to the terminal, hit enter.

Note, the way this is set up, the script will rename every file of the specified .extension in the current directory.

Replace file name suffix:

Another script. This script changes the suffix of each specified file in a directory. In this example from “myName_000_01.JPG” to “myName_000.jpg”

for i in *.JPG do mv $i `basename -s _01.JPG $i`.jpg done

Edit the the first line of the script so that it works on the appropriate files. In this example we have several files that have the extension of .JPG in common. Each of these files will be renamed.

The “mv” unix command is used to rename the files. Using the “basename” utility and the “-s” flag we can designate what part original name we want to keep, rather the suffix we want to remove. In this example the suffix we want to remove is “_01.JPG”.

Specify the new suffix that you want to add (here it’s “.jpg”).

Next, open a terminal window. Navigate to the directory where your files reside. Enter “sh” in the window, hit enter. Paste the script, and hit enter. That’s it.

scripts written with the assistance of Jeshua Lacock of OpenOSX.com