Skip to content

mcp

2011 February 20

Image: Rochester & Genesee Valley Railroad Museum (August 27, 2005), rgvrrm.org

When I get a new loginid on a Linux (or Unix) system, there are a few things I set up right away. While I will expend surprisingly many words to tell you about this, the action only takes a minute or two when you know how. This article uses a very basic set of Linux (or Unix) commands. If you are a newbie who wants to get started quickly with Linux (or Unix), then learn the contents of this article. Harass your colleagues for help if you need it.

First, I set up my own ~/bin subdirectory to hold my glorious creations, including mcp.

$ mkdir ~/bin

Next, I create mcp, which I use all the time. mcp is an acronym that stands for something I don’t recall. mcp is a shell script that contains this:

#! /bin/sh
cp ${1} ${1}.`date +%Y%m%d-%H%M%S`

I use mcp to make a copy of a file before I change it. This gives me a ready fallback. If I find myself on an unproductive coding tangent, or if delete my working file by mistake, or if it was working before I fixed it and now it’s not, then I can get back to where I was and save the galaxy after all. My colleague may hose up the program she’s been debugging for the last three weeks, and try to recreate if from scratch, but not me. Nor do I have to beg the indulgence of an overworked and underpaid system administrator to restore the working version from backups. The timestamp works much better than a suffix like .o or .old, since these accommodate only one backup copy. The timestamp also works better than suffixes like .1, .2, .3, etc. because every timestamp is unique, and you don’t have to wonder whether the .2 copy was made recently or last year, and you don’t have to wonder whether it’s old enough to discard. When my colleagues ask, “How long should we keep multiple timestamped copies?”, I usually say “90 days or make an executive decision.” When it comes time to clean out the debris, a simple command used with care like

$ rm *.200906*

usually works nicely.

The copy I made with mcp has the unique and never to be repeated timestamp. The time stamp formats the date and time so that multiple mcp copies will sort in order.

I suppose one could create mcp using cat, or even emacs, but I like vi for this purpose. Also, I test mcp immediately to make sure it works. Here are my steps:

$ mkdir ~/bin
$ cd ~/bin
$ vi mcp
$ chmod +x mcp
$ touch bozo
$ mcp bozo
$ ls bozo*

This would display something like

bozo bozo.20110220-114919 mcp

Ta-da! It’s working.

I thank Phil Greiper at Bank of America and my colleagues at Mellon Capital Management for inspiring the hyphen between the datestamp and the timestamp for readability.

A variation we used at Mellon to help accountability was

#! /bin/sh
cp ${1} ${1}.`date +%Y%m%d-%H%M%S`.`id –un`

though I sympathize with those who feel it adds too much clutter to a filename already too long,  and it exacerbates the spelling challenge. I seldom use this variation because the value of simplicity usually trumps the value of accountability. We must consider all things with balance and in context.

A cousin to mcp is mvp:

#! /bin/sh
mv ${1} ${1}.`date +%Y%m%d-%H%M%S`

I use mvp to delete a file or directory. By renaming it with mv, the file no longer serves its original purpose, in nearly all cases, yet I can get it back tomorrow, without waiting for the sysadmin’s 72-hour restoration request fulfillment policy, if I have second thoughts about having deleted it. That virtue acknowledged, I’ve seldom used mvp in practice, so I don’t bother with creating it until a specific use arises.

But enough of this fascinating digression. A little more work will make mcp fully useful.

Let’s make sure mcp is on the path. The command set

$ cd ~
$ which mcp

will show us the mcp we will use when we invoke the command, or will show us the list of directories in which it didn’t find mcp.

(If the instructions that follow seem like gibberish, then don’t worry about it. Just give the commands, because the instructions will either work or they won‘t, and will do no harm if they don’t work, and it’s easy to try it, and so go for it. If they don’t work, then bribe your overworked and underpaid sysadmin with a cappuccino and ask her to help you “put ~/bin on my path”, which will probably take her about 90 seconds of direct effort, tops.)

If we get the list of directories and if we tested mcp with bozo as above, then most likely our new toolbox ~/bin subdirectory isn’t on the path. We can fix that in file ~/.bashrc . (If you aren’t using Linux or if you aren’t using bash, and if you aren’t using csh, then use the file ~/.profile instead of ~/.bashrc and also instead of ~/.bash_profile in the instructions that follow. If you are using ksh, then use file ~/.kshrc instead of ~/.bashrc ,  and use ~/.profile instead of ~/.bash_profile .)

Also, at no additional charge, we will here cover how to fix the annoying multicolor output of ls with a simple alias.

Here are the commands:

$ cd ~
$ ~/bin/mcp .bashrc
$ vi .bashrc

Scroll down to the bottom of .bashrc and append these lines:

alias ls='ls -AF'            # omit this line if you like multicolor ls output
PATH=${PATH}:~/bin
export PATH

Save the file, then source the profile and check:

$ . ~/.bash_profile
$ which mcp
$ touch bozo
$ mcp bozo
$ ls bozo*

That’s all until next time. Happy trails!


 

No comments yet

Leave a Reply

You must be logged in to post a comment.