Run Several Commands Sequentially


;

What if you have several commands you need to run consecutively, but some of them are going to take a long time, and you don't feel like babysitting your computer? For instance, what if you have a huge number of John Coltrane MP3s in a zipped archive file, and you want to unzip them, place them in a new subdirectory, and then delete the archive file? Normally you'd have to run those commands one at a time, like this:

Note

In order to save space, I removed the owner and group from the long listing.


$ ls -l /home/scott/music -rw-r--r-- 1437931 2005-11-07 17:19 JohnColtrane.zip $ unzip /home/scott/music/JohnColtrane.zip $ mkdir -p /home/scott/music/coltrane $ mv /home/scott/music/JohnColtrane*.mp3 /home/scott/music/coltrane/ $ rm /home/scott/music/JohnColtrane.zip 


JohnColtrane.zip is a 1.4GB file, and even on a fast machine, unzipping that monster is going to take some time, and you probably have better things to do than sit there and wait. Command stacking to the rescue!

Command stacking puts all the commands you want to run on one line in your shell, with each specific command separated by a semicolon (;). Each command is then executed in sequential order and each must terminatesuccessfully or unsuccessfullybefore the next one runs. It's easy to do, and it can really save you some time.

With command stacking, the previous series of commands now looks like this:

[View full width]

$ ls -l /home/scott/music -rw-r--r-- 1437931 2005-11-07 17:19 JohnColtrane.zip $ unzip /home/scott/music/JohnColtrane.zip ; mkdir -p /home/scott/music/coltrane ; mv /home/scott/music/JohnColtrane*.mp3 /home/scott/music/coltrane/ ; rm /home/scott/music /JohnColtrane.zip


Of course, you can also use this method to introduce short delays as commands run. If you want to take a screenshot of everything you see in your monitor, just run the following command (this assumes you have the ImageMagick package installed, which virtually all Linux distributions do):

$ sleep 3 ; import -frame window.tif 


The sleep command in this case waits three seconds, and then the screenshot is taken using import. The delay gives you time to minimize your terminal application and bring to the foreground any windows you want to appear in the screenshot. The ; makes it easy to separate the commands logically so you get maximum use out of them.

Caution

Be very careful when command stacking, especially when deleting or moving files! Make sure what you typed is what you want because the commands will run, one right after another, and you might end up with unexpected surprises.




Linux Phrasebook
Linux Phrasebook
ISBN: 0672328380
EAN: 2147483647
Year: 2007
Pages: 288

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net