Category Archives:

Chris and Matt: Unix Quick Tips

Posted on April 21, 2008 by Matt Williams

Preventing Accidental File Deletion in Unix

Every seasoned command-line user has at least one horror story involving an accidental file deletion.

Unfortunately, experience won't prevent this from happening again as fingers will still occasionally outrun (or outmaneuver) the preoccupied brain.

One way to defend against this is to use the -i flag with potentially destructive commands such as cp or rm. Even if it were possible, however, to train yourself into this habit 100% of the time, that's 3 extra characters to type with every file manipulation command - not exactly efficient.

The best line of defense is to modify the .bashrc (or .bash_profile) file in your home directory to alias these commands.

alias rm="rm -i"
alias cp="cp -i"

Great, but...

...deleting a directory containing many files can become a huge annoyance, since the -i flag will prompt you about deleting each file. This is what the less popular -I (uppercase i) flag is for. Unfortunately, this isn't available with every implementation of rm (looking at you OS X!), but if your *nix supports the flag (man rm and look for the flag if you're unsure), you'll get a single prompt when you try to delete a directory using the -r flag asking if you want to recursively delete. If you prefer this (I do) and if your *nix allows it (Debian does), just modify the alias that you just created.

| Comments (0) | Technorati Tags :

Posted on March 25, 2008 by Matt Williams

Unix Directory Creation

One of the most common inefficient habits of Unix users is iterative directory creation:

$ mkdir project
$ cd project
$ mkdir docs
$ cd docs
$ mkdir html

You may (or may not) already be aware of the -p flag, which allows you to create parent AND child directories in one command:

$ mkdir -p project/docs/html

Not many people are aware, however, that you can create arbitrarily complex directory trees using the -p flag and some curly braces:

$ mkdir -p proj/{bin,etc,docs/{html,txt,pdf},src}

If the example above represented a common project layout, you could add a quick alias to .bashrc in your home directory:

alias mkproj="mkdir -p proj/{bin,etc,docs/{html,txt,pdf},src}"

| Comments (1) | Technorati Tags :