PawEng

Software, hardware, and the internet

Skip to: Content | Sidebar | Footer


 Subscribe to feed    

I left my shell in the sun and now it is all GUI

5 August, 2008 (10:25 pm) | Shell

It seems that everything is GUI these days. Everything has to have a graphical user interface. Sure, it looks great, but is it really user-friendly? Do you really get more done with a GUI than with a command-line interface? The saying goes that when you sell software you show the managers the graphical version, but you give the engineers the command-line version.

A GUI is great if you don’t know a program. It shows you right there what you can do. Unfortunately, it normally requires a lot of mouse clicks; even the simplest things now require use of the mouse. And it is difficult to make different GUI programs talk to each other.

A good shell and some command-line tools, on the other hand, may have a steeper learning curve, but once you master them, you can do very powerful things very quickly.

Consider this situation: You have a number of .jpg files in multiple directories. You want to copy them all into a single directory. How would you do it in Windows? You would probably have to look in each directory and copy them separately. If you have hundreds of directories, this becomes tedious. Below I will show you how to do this with a single command in a shell.

I work a lot on Linux and Unix systems, and here I use the tcsh shell. If you have a Windows PC, you can download Cygwin. It is a Linux-like environment for Windows, and it gives you a bash shell and a set of Unix command-line tools.

Completion

So, what can you do with the command-line? One of the best things is completion: When you are typing in a command or a file or directory name, you can press the TAB key and the shell automatically completes as much as it can. This makes typing much faster and much less error prone. For example, try typing the following command:

more some-very-long-directory-name/some-very-long-file-name.txt

I bet it was slow and that you made at least one mistake. With completion you only have to type:

more s[TAB]s[TAB]

The first TAB key expands “s” to “some-very-long-directory-name/”, and the second time I hit TAB I get “some-very-long-file-name.txt”. In both cases I assume there is only one directory or file with a name starting with “s”. If “s” is not unique, TAB will simply expand as much as it can. And if I press TAB an extra time, the shell will show me which options are available.

Gluing Commands Together

On the command-line you can directly use the output of one command as the input to another command by simply placing a ‘|’ (pipe) character between the commands. For example:

cat foo.txt | grep "something interesting" | head

Here, “cat foo.txt” will show the content of the file foo.txt, but because of the pipe, the output will not go to the screen but rather directly to the next command — in this case grep. The grep command will look for lines containing “something interesting”. Those lines are the input to the “head” command, which will display the first 10 lines of its input.

Another way of gluing together commands is using the back-tick operator. The shell executes what is in back-ticks, and replaces it with its output.

grep "something interesting" `cat filelist.txt`

Here the shell first executes “cat filelist.txt”. Assuming filelist.txt contains a list of files A.txt, B.txt, and C.txt, the shell replaces `cat filelist.txt` with the files:

grep "something interesting" A.txt B.txt C.txt

The grep command now looks for “something interesting” in those three files.

Command-line Tools

The “find” command is very powerful. It allows you to find files and optionally process them. For example, you can easily find all .jpg files in the current directory and its sub-directories, and copy them the directory /tmp:

find . -name '*.jpg' -exec cp \{\} /tmp \;

The find command will find all files matching ‘*.jpg’, and execute (-exec) the copy (cp) command on each of them. The \{\} expands to each file name in turn, and the \; indicates that -exec option is done.

The above find command calls cp for each file. If you have thousands of files, then there is a more efficient way of doing it: Use the xargs command.

find . -name '*.jpg' -print0 | xargs -0 -I cp \{\} /tmp

First the find command finds all files matching ‘*.jpg’. Then it passes the list of files to the xargs command. Xargs breaks the list up into pieces that will fit on the command line, and calls cp. Again, \{\} is replaced with file names, but this time each cp command will copy multiple files at once. The -print0 option to find, and the -0 option to xargs are needed to prevent problems with file names containing white spaces. The -I option to xargs tells it to insert the file names at the location of \{\} rather than at the end of the line.

Final Words

If this is the first time you see Unix command lines, then it probably looks like gobbledygook. It is not easy to read until you learn how to decipher the code. But once you learn it, you have a powerful set of tools at your disposal.

University of Cambridge has a leaflet describing Thirty Useful Unix Commands. There are many more commands out there, but these ones are good to know.

Comments

Pingback from PawEng » Ubiquity: Supercharge Your Firefox with a Command Line
Time August 27, 2008 at 7:43 pm

[...] add-on is Ubiquify, and it gives you an instant command line interface to the web. I have earlier shared my love for command lines so I was very excited about Ubiquity. After installing it, I was not disappointed. Even though [...]

Pingback from PawEng » Grep and Awk to Extract Text
Time September 10, 2008 at 10:06 pm

[...] you start using the command line, it is amazing how many things you can do that were difficult in a GUI system. A common task is to [...]

Pingback from Recent Links Tagged With “xargs” – JabberTags
Time October 1, 2008 at 5:34 am

[...] public links >> xargs Epicness Saved by asbjorn on Tue 30-9-2008 I left my shell in the sun and now it is all GUI Saved by Belldandy on Mon 29-9-2008 Musings of an Anonymous Geek » Blog Archive » Show Me Your [...]

Write a comment