Use tr to transform a text string

April 20th, 2004

Problem:
In a shell script to back up a directory, you want to save the directory as a tar file whose name includes the full original path (just in case you have multiple directories, in separate locations, with the same name). So you want to save a directory with the path /dev/fs/D/mydir to a tar file called BACKUP_.dev.fs.D.mydir.

Solution:
Use tr to transform the path string. For example, the path to the directory you are backing up (/dev/fs/D/mydir) is stored in the variable $DIRECTORY. Use the following commands to create a variable called $NAME containing BACKUP_.dev.fs.D.mydir.

NAME = `echo $DIRECTORY | tr '/' '.'`
NAME = BACKUP_`echo $NAME | tr '\' '.'`

Note: don't mix up the backticks and the single quotes.

Leave a comment