Home arrow Site Administration arrow Page 4 - Personalizing the User Environment in BSD

Hack 8: Create a Trash Directory - Administration

In this second part of a three-part article, you'll learn a few more ways to personalize the user environment in BSD, such as adding some fun trivia, setting up a trash directory, and locking down your screen. It is excerpted from chapter one of the book BSD Hacks, written by Dru Lavigne (Copyright © 2005 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media; ISBN: 0596006799).

TABLE OF CONTENTS:
  1. Personalizing the User Environment in BSD
  2. Hack 6: Get Your Daily Dose of Trivia
  3. Hack 7: Lock the Screen
  4. Hack 8: Create a Trash Directory
By: O'Reilly Media
Rating: starstarstarstarstar / 5
December 14, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Save ďdeletedĒ files until youíre really ready to send them to the bit bucket.

One of the first things Unix users learn is that deleted files are really, really gone. This is especially true at the command line where there isnít any Windows-style recycling bin to rummage through should you have a change of heart regarding the fate of a removed file. Itís off to the backups! (You do have backups, donít you?)

Fortunately, it is very simple to hack a small script that will send removed files to a custom trash directory. If youíve never written a script before, this is an excellent exercise in how easy and useful scripting can be.

Shell Scripting for the Impatient

Since a script is an executable file, you should place your scripts in a directory that is in your path. Remember, your path is just a list of directories where the shell will look for commands if you donít give them full path-names. To see your path:

  % echo $PATH    
  PATH=
/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/ usr/local/sbin:/usr/ 
  local/bin:/usr/X11R6/bin:/home/dru/bin

In this output, the shell will look for executables in the bin subdirectory ofdruís home directory. However, it wonít look for executables placed directly in my home directory, or /home/dru. Since bin isnít created by default, I should do that first:

  % cd
  %
mkdir bin

As I create scripts, Iíll store them in /home/dru/bin, since I donít have permission to store them anywhere else. Fortunately, no one else has permission to store them in my bin directory, so itís a good match.

The scripts themselves contain at least three lines:

  #!/bin/sh
  # a comment explaining what the script does
  the command to be executed

The first line indicates the type of script by specifying the program to use to execute the script. Iíve chosen to use a Bourne script because that shell is available on all Unix systems.

Your script should also have comments, which start with the #character. Itís surprising how forgetful you can be six months down the road, especially if you create a lot of scripts. For this reason, you should also give the script a name that reminds you of what it does.

The third and subsequent lines contain the meat of the script: the actual command(s) to execute. This can range from a simple one-liner to a more complex set of commands, variables, and conditions. Fortunately, we can make a trash script in a simple one-liner.

The Code

Letís start with this variant, which I found as the result of a Google search:

  % more ~/bin/trash
  #!/bin/sh
  # script to send removed files to trash directory
  mv $1 ~/.trash/

You should recognize the path to the Bourne shell, the comment, and themvcommand. Letís take a look at that$1. This is known as a positional parameter and specifically refers to the first parameter of thetrashcommand. Since themvcommands takes filenames as parameters, the command:

  mv $1 ~/.trash/

is really saying,mvthe first filename, whatever it happens to be, to a directory called .trash in the userís home directory (represented by the shell shortcut of~). This move operation is our custom ďrecycle.Ē

Before this script can do anything, it must be set as executable:

  % chmod +x ~/bin/trash

And I must create that trash directory for it to use:

  % mkdir ~/.trash

Note that Iíve chosen to create a hidden trash directory; any file or directory that begins with the . character is hidden from normal listings. This really only reduces clutter, though, as you can see these files by passing the -a switch to ls . If you also include theFswitch, directory names will end with a /:

  % ls -aF ~
  .cshrc     .history   .trash/
  bin/       images/    myfile

Replacing rm with ~/bin/trash

Now comes the neat part of the hack. I want this script to kick in every time I userm. Since it is the shell that executes commands, I simply need to make my shell use thetrashcommand instead. I do that by adding this line to ~/.cshrc:

  alias rm    trash

That line basically says: when I typerm, executetrashinstead. It doesnít matter which directory I am in. As long as I stay in my shell, it willmvany files I try tormto my hidden trash directory.

Running the Code Safely

Whenever you create a script, always test it first. Iíll start by telling my shell to reread its configuration file:

  % source ~/.cshrc

Then, Iíll make some test files to remove:

  % cd
 
% mkdir test
  % cd test
  % touch test1
 
%
rm test1
  % ls ~/.trash
  test1 

Looks like the script is working. However, it has a flaw. Have you spotted it yet? If not, try this:

  % touch a aa aaa aaaa
 
%
rm a*
 
% ls ~/.trash
  test1          a
 
% ls test
 
aa         aaa         aaaa

What happened here? I passed the shell more than one parameter. The a* was expanded to a, aa,aaa, andaaaabefore trashcould execute. Those four parameters were then passed on to themvcommand in my script. However,trashpasses only the first parameter to themvcommand, ignoring the remaining parameters. Fortunately, they werenít removed, but the script still didnít achieve what I wanted.

You can actually have up to nine parameters, named$1to$9. However, our goal is to catch all parameters, regardless of the amount. To do that, we use$@:

  mv $@ ~/.trash/

Make that change to your script, then test it by removing multiple files. You should now have a script that works every time.

Taking Out the Trash

You should occasionally go through your trash directory and really remove the files you no longer want. If youíre really on your toes you may be thinking, ďBut how do I empty the trash directory?Ē If you do this:

% rm ~/.trash/*

your trash directory wonít lose any files! This time you really do want to use rm, not trash. To tell your shell to use the real rm command, simply put a\in front of it like so:

  % \rm /trash/*

Voila, empty recycling bin.

Hacking the Hack

One obvious extension is to keep versioned backups. Use thedatecommand to find the time of deletion and append that to the name of the file in thetrashcommand. You could get infinitely more complicated by storing a limited number of versions or deleting all versions older than a week or a month. Of course, you could also keep your important files under version control and leave the complexity to someone else!

Please check back next week for the conclusion of this article.



 
 
>>> More Site Administration Articles          >>> More By O'Reilly Media
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

SITE ADMINISTRATION ARTICLES

- Coding: Not Just for Developers
- To Support or Not Support IE?
- Administration: Networking OSX and Win 7
- DotNetNuke Gets Social
- Integrating MailChimp with Joomla: Creating ...
- Integrating MailChimp with Joomla: List Mana...
- Integrating MailChimp with Joomla: Building ...
- Integrating MailChimp with Joomla
- More Top WordPress Plugins for Social Media
- Optimizing Security: SSH Public Key Authenti...
- Patches and Rejects in Software Configuratio...
- Configuring a CVS Server
- Managing Code and Teams for Cross-Platform S...
- Software Configuration Management
- Back Up a Joomla Site with Akeeba Backup

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: