Sunday, June 17, 2007

A couple of linux tips for newbies



Being a GNU/Linux newbie, having only switched completely to Ubuntu GNU/Linux from Windows about a year and a half ago, I am still trying to figure out what works and what doesn't for me, and how I can fix it. Ubuntu is my distro of choice, not necessarily because it's an easier distro, but because I like the Debian way of doing things, I like bleeding edge, and I like being backed up by the huge Ubuntu community. Having said that, most of these tips are from the point of view of a Debian/Ubuntu user, however, they can be applied to other distros:

1. Backup Script. It's usually good to backup at least your /home directory every once in a while. However, backing up manually can be a pain, and most of the scripts I found out there where too complex for what I needed, so I wrote my own:





#!/bin/bash
backup_path="/full/path/to/backup"
compress(){ #compress backed up files
tar cvvf $backup_path/backup$(date %d%m%G).tar $HOME
exit 0
}

if [ -d $backup_path/backup ]
then
compress
else
mkdir $backup_path/backup
compress
fi
else

exit 0


What I'm doing with this script is backing up my home directory and saving it as a compressed file with the name "backup[daymonthyear].tar"
I suggest you not save the backup files in the same hard drive as the original; if the hard drive fails you might be in trouble. You should mount to an external hard drive or a file server. If you want to get more detailed and run backups from only on a list of files, you can substitute the compress function for:

compress(){
tar cvvf $backup_path/backup$(date %d%m%G).tar -l file.list
exit 0
}


where the list of files is in a text file called "file.list" (remember: one file path per line and be careful with white spaces).

2. One thing that I miss when I use the command line is not being able to undo mistaken deletions. In Windows, or Using the Desktop manager ( I use GNOME), when I delete something, it goes to the "trash" folder, where I can go to retrieve it if i deleted it by mistake. When I use the rm command in the command line, whatever is gone, is gone for good. That's why I use a nice little trick: I use a "trash" function in my .bashrc file instead of "rm" when what to get rid of a file, but I am not sure if I am going to need it later. The trash directory (.Trash) is hidden in your home directory, so you just need a command that will move unwanted files and directories to the .Trash directory:


trash() { mv "$@" /home/$USER/.Trash;}


I call my function "trash" so when I call it I just type "trash [filename]" on the command line. Notice that I actually use the move command instead of the remove command, to move my files to the .Trash directory. Notice also that I use the $USER system variable instead of the actual home folder name - it's slightly more elegant since I can run this function under any username and the system will recognize what home directory to look for. Also, I'm lazy and my username is longer than the variable name. This function also allows me to attach move options as well as pointing to the file that I want to "trash". Copy this function to your .bashrc (in your /home directory). and you have yourself a new "delete" command!!

1 comment:

Benjamin said...

I'm a OS X UNIX geek (used to use Linux exclusively, but got annoyed trying to use my Treo with it). I have something very similar to your trash function, but mine is a bit more portable, as not all "Unices" keep their user folders in "/home" (for example, OS X keeps them in "/Users"). So, a slightly less brittle version might be:

function trash () {
mv "$*" "$HOME/.Trash/"
}

This will work on Solaris, Mac OS X, Linux, etc, etc.

Also, I noticed a while ago that I always "cd" and then "ls". If you do the same, you might consider the following function that I found on the web:

# From http://www.oreillynet.com/onlamp/blog/2007/01/whats_in_your_bash_history.html
# (slightly modified)
function cl () {
if [ $# = 0 ]; then
cd && ls
else
cd "$*" && ls
fi
}

Who Needs Internet Explorer?!!