This is the place where Access Space participants (Spacers) create wikified web content.

 

Cool Stuff To Do With The Shell

Shell?

Introduction

The shell can be accessed in several ways, depending on your version of GNU/Linux. Both GNOME and KDE have their own terminal emulators, but the most ubiquitous is the venerable xterm.

To open xterm, hit Alt-F2 and type xterm, then press return. Pressing Alt-F2 will open a Run command… dialogue on both KDE and GNOME, and saves having to spend ages looking through menus.

Your shell should display a prompt, which will say something like theo@raindog:~$.

theo is your username and raindog is the name of the PC you are logged into (as displayed on the login screen.)

~ is your current working directory (~ means the same as /home/username), and $ is your prompt. On some rare occasions, the prompt may be a # or a % instead (as mine is.)

OK, now for some commands! Commands should be typed in or pasted. Ctrl-V won't paste in xterm, so you need to select the text, and then middle click in the place you want it pasted.

A command is simple a sentence, which begins with the name of a program or a shell keyword. It may also contain options - or switches - for the program, and possibly the names of files. When you've typed a command, press return to run it.

Here are some common commands for the both new and expert users. Commands are shown like this

  • whoami

Display your username.

  • ls

Displays the contents of your home folder.

  • cdparanoia -B && oggenc *.wav && rm *.wav

Rip a CD, and encode the tracks to a smaller, portable format. This is similar to mp3, and playable on any PC at Access Space, or on a Windows PC using the free VLC Media Player.

  • uptime

See how long your computer has been on for.

  • cat FILE

Display the contents of a file called FILE (if it is a plain text document.)

Download the entire contents of a website for offline viewing.

  • convert -resize 640 big_photo.jpg small_photo.jpg

Make a copy of a large photo called big_photo.jpg which is small enough for attaching to an email or putting online. The old photo is kept, and the new photo is called small_photo.jpg.

  • cal

Display a calendar for the current year.

Some commands require additional preparation, such as installing software, so they will not all work until the appropriate tools are installed (such as cdparanoia.)

Yeah, but why do I want a shell?!

This guide is designed for newcomers to the shell - and possibly to GNU/Linux, who want to know why they should learn how to use the shell, and what kind of things it can be used for.

If you read this and want to learn more, then there are many good guides out there that can give you a decent grounding in how to start writing your own commands or scripts to do what you actually want (like batch image processing.)

This guide is not designed to teach you about every command in the shell, or even about very many of the tools that you will probably end up using. There are other fine guides which do this, and they may be found by simply doing a web search.

Searching for help, however, requires some knowledge of what is actually possible, which is what this short introduction attempts to imbue you with.

When I mention a word, phrase of the name of a program which may not be self explanatory, such as Runlevel 5, I try and include a link to more information (usually to the Wikipedia article).

The Point of the Shell

Most users of the operating system work with some kind of Graphical User Interface, or GUI. This is the software programs which create the windows, buttons, menus etc. This makes some tasks a lot easier, however there are some tasks which, I would contend, are better suited to the shell.

The shell is a Command Line Interface, or CLI, which allows you to enter sets of commands in the form of text.

A lot of the functionality is no longer strictly required in the days of the GUI, however there is still a place for the command line in many every day tasks (including those you wouldn't normally think of, such as image and sound manipulation).

Why is it called the "Shell"?

On possibility is the idea of a protective shell, which insulates the inside functionality of the operating system. Another is the idea of a nut, since the core of the system is called the Kernel (such as the Linux Kernel.)

Accessing the Shell

Earlier, I covered the quickest and easiest way of accessing a shell on a computer which already has a GUI desktop running. This is by using a terminal emulator. The other way is through a Virtual Console.

When your system boots up, it will probably enter what is called Runlevel 2 (different versions of GNU/Linux do things a bit differently.) This will load all the background services (or daemons), and then start the GUI login screen.

The GUI runs on tty7. However there are 6 other consoles open which run only a shell. These are called ttys. If you want to open a tty, you can do this by pressing Ctrl-Alt-F2.

This will throw you into a text only login prompt. Log in with your username and password and you are in the shell. If you want to return to your GUI, press Ctrl-Alt-F7.

Basic Usage

When you open the shell, in whichever way you choose, you will almost certainly be in your home directory. To check where you are, you may run the pwd command. And, as mentioned earlier, ls will list your files. If you run ls -lh, then it will list your files in a more informative fashion, showing the permissions, modification time, and human-readable size.

It is relevant to know which directory in when you start running command which expect files to act on, however some commands are generic, and may be run from anywhere. If you want to change your working directory, you can type cd followed by a space and the name of the directory you wish to enter.

Commands are typed in lowercase, the shell is case sensitive so you can't get away with using mixed or uppercase like you can in DOS or Windows.

You are probably using the Bash shell, though some systems use other shells, such as Zsh and Ksh. Most shells commonly used are pretty much the same as eachother for basic usage, so following this guide, it shouldn't matter what your shell is.

Some commands, such as pwd, cd and exit are simply builtin shell commands. Most other commands will run other programs, such as ls, convert and vim. These may not be an obvious distinction, however GUI applications may also be run from the shell, at which point it all begins to come together.

If you run a command such as firefox, it will load Firefox just the same as if you had launched it from the menu. Running firefox http://access-space.org shows how it is just the same procedure to run graphical applications with switches and parameters as it is command line applications.

If your command is invalid, either because you typed it wrong or because the program in question is not installed, you will receive a command not found message. It is work noting that graphical programs will not run from a tty.

How About Other Commands?

The aim of this guide is not to tell you how to use every single command but to highlight some of the most useful things and give you an idea of how to learn more.

If you want to learn how to use a command or program (such as vi, the text editor), the type man vi in the Shell, this will bring up a manual.

Wikipedia

http://www.gnu.org/software/bash/manual/bashref.html

http://d4maths.lowtech.org/rgs.htm

You can also find out more by searching for bash shell commands or linux shell guide or something like that on Google.

And now…

Cool Stuff To Do With The Shell

Shell Scripts

With the introduction out of the way, let's talk about what the shell can do for you. Take image manipulation, surely the domain of graphical programs such as The Gimp or Inkscape.

But say you take digital photos, as many people do, and you want to put them online. If you have a camera full of photos (and chances are that with modern memory card sizes that will be more than 100), they probably have weird names like P1003501.JPG or DSC1012.JPG. It makes sense to your camera but is not very informative to you or anyone looking at your photos.

You could go through and rename them all park_tuesday1.jpg, france_21st.jpg etc. but that would be a waste of time when you could do it so much more easily with just a few lines of text.

When you enter a command for the shell to process, you have to type it in. This makes sense if it is a one off and you only want to do something to one file, but if you want to quickly and easily do batch processing on several (or several hundred) files, you'll want something quicker and easier.

This is where shell scripts come into their own. A shell script is nothing a but a plain text file containing one or more shell commands, however they can be very powerful. For instance, since the shell features programmatic control structures, such as if, for and while, it is possible to write a shell script with the same kind of dynamic logic that goes into a proper computer program.

Also, shells feature powerful globbing, meaning that it is possible to easily operate on a set of files at once (such as all JPEG images.)

Below is an example of a very mundane, if rather useless shell script.

#!/bin/bash
 
f=$(ls | wc -l)
 
echo "$(pwd) has $f files in it."
 
if [ $f -gt 50 ]; then
	echo "That's quite a few, perhaps you should put them into subdirectories, so that they are easier to look at."
fi

It might sound complex but it all makes sense when you know how the shell works. Open a text editor, such as Kwrite or Gedit, and paste the text in, and then save the file in your home folder as countfiles.sh

NOTE! The file must be plain text. This means not an OpenOffice document.

If you saved the file in your home folder, you can now open up a terminal and run chmod +x countfiles.sh. If you get an error, you should double check that the file is present in your home folder.

If the previous command was successful (meaning, in this case, that it printed no errors, and dropped you back at your shell prompt), you can now run ./countfiles.sh

The ./ bit tells the system to run the file in the directory, and is necessary to distinguish from an installed program or an internal command. It is possible to remove the requirement, however it is a more advanced subject.

If you got no errors, and the script printed out the number of files into your shell window, then it worked - you have just run your first shell script.

Back To The Camera Problem!

I have a camera which takes photos which is names as something like P1000169.jpg. This means something to the camera, but not to the user. When you I insert my camera memory card into a PC's card reader, or connect the camera with a USB cable, the photos currently on the camera appear in a folder somewhere like this: /media/usb_a/DCIM/104_PANA.

The location of the memory card will vary, depending on the make of camera as well as the current PC setup. I have written a shell script which copies photos off my camera, renames them to something useful, and moves them into my photos folder.

The script is called photo_dcim_get.sh, and can be downloaded from my website. You will need to open the script up in your text editor, and edit the locations to match where you want your photos to be. Only you can do that.

Finding Things With The Shell

Different environments, such as KDE and GNOME, have different search tools at hand, however the most reliable way of finding things is with a couple of programs called Find and Grep.

Searching By Name

If you've saved a file off somewhere, and you can't remember where you put it, or you just want to, for instance, find all the JPEG images on your machine, nothing beats find.

You can run find from the shell, like this find -iname “lost.txt”

This is cool if you know your file was called lost.txt. If you just want to find all the files with lost anywhere in their name, you can run find -name “*lost*”

If you can't even remember the name, but you saved the file within the last day, you could try the following command find -mtime -1

If you wanted to find all your MS Word or OpenOffice documents, then you could run find -name “*.doc”; find -name “*.odt”

The semicolon simply allows you to run two commands in succession, without waiting for the next prompt. While it is possible to make find look for multiple patterns at once, this is just as simple.

Searching By Contents

If you want to know which files contain certain words or sentences, it could be like looking for a needle in a haystack, were it not for grep. grep is very flexible, so it is probably best to look at some documentation (either on your machine or online), to find out how to get the most out of it.

While is can be very tricky to do some pattern matching, the simplest uses of grep are very easy indeed. To search all files in the current directory for the word string, for instance, you can just run grep “string” *

If you want to start using grep in earnest, you should read up some pages. Just do a web search for grep guide to find lots of useful information.

Making Animations

While most animations found online these days are Flash based, there is still a niche for the Animated GIF.

Animating GIFs can (AFAIK) be done with The GIMP, however it is extremely simple to do this with a shell script, also, and can be a lot more efficient if you have many frames.

If you wish to turn a directory full of JPEG images into a small Animated GIF, you can run convert -resize 320 -delay 20 -loop 0 *.jpg animation.gif

NOTE! Depending on the number and size of the images, this command can be quite slow to run, and will tie your PC while it operates.

Padding With Zeros

You may have noticed that when you name files by number, 1.jpg, 2.jpg, 3.jpg etc., they are often displayed in the wrong order by many programs, which simply look at the name, fail to sort them numerically. This can be quite annoying, since most of the time you want 1 through 9 to come before 10.

The answer is to pad the names with zeros. The following example works on JPEG images, but it can be anything. Since it strips off any prefix or suffix which may have been present in the filename, you can supply an argument to the script if you wish the names to be of the format blah001.jpg

#!/bin/bash

for j in *.jpg; do

	v=$(basename $j .jpg)

	n=$(printf "%03d" $v)

	mv -v "$i" "${1}${n}.jpg"

done

Reference

Redirection

With the shell, you can redirect (Pipe) the output of shell commands to files with the > (greater than) character like this: ls > listing.txt. This will put the output of the directory listing which you would normally see in the terminal into a file called listing.txt

You can also (as in the first script) use the output of one command as the input of another command with the | (vertical bar, or pipe character).

That is why ls | wc -l will show how many files are in a directory. Because ls will show the files (one per line), and wc -l will count how many lines whatever text it receives contains (which in this case is the output of ls).

Awk is a text processing utility which allows you to format lines of text by writing short programs which work on the files.

Shell Aliases

When you type a command into the shell, it usually needs to be an internal command or the name of a program/script. There is a third category, however: Aliases

These are where a word, such as browser is substituted for the actually name of the (browser) program, such as firefox or konqueror. Try this, run alias browser=“firefox”

Then run browser, and Firefox should open. Then close Firefox, and run alias browser=“konqueror”

Then run browser again, and this time Konqueror should open.

These aliases are rather trivial, however aliases can also contain arguments for a program. This is very useful on occasions where you want to always run a program with specific arguments. For instance, I have aliased ls ls -b –color=auto –group-directories-first -H –quoting-style=literal. This gives me a nicer listing by default, without me having to type more than the usual ls.

It is annoying to have to add the aliases to the shell each time, however they can be put is .bashrc so that they get re-bound each time you start a shell. Just add the lines, like alias la=ls -a, to the bottom of .bashrc, then start a new shell as type la to list all files, including hidden files.

Another example is if you are used to using DOS or cmd.exe, where del deletes files, cls clears the screen and move and copy are equivalent to mv and cp, the you can type:

alias cls=“clear”

alias move=“mv”

alias copy=“cp”

alias del=“rm”

Miscellaneous Tips and Tricks

Dictionary

One of the hidden gems of GNU/Linux is the dictionary file, which comes with most distributions. This file is most likely /usr/share/dict/words, though this may vary (find /usr -name words). It can be used to find words when you only have some letters, by grepping it.

For instance, if you are doing a crossword and have ”[ ][e][ ][a][c][e]”, you can run this command grep -i -o -w ”.e.ace” /usr/share/dict/words which will return:

belace
deface
delace
hexace
menace
reface
relace
repace
tenace

Text to Speech

If you system includes a program called ESpeak, then you can use the shell for Text To Speech. To try this, type espeak “Hello, world”

You can pipe the output of commands, or the contents of files, to Espeak to make it speak them aloud. This provides a very simple speaking clock date ”+The time is %I %M” | espeak

Fixing a Frozen System

While GNU/Linux is normally a very stable system, there may be cases where an application will freeze. This could potentially cause the whole system to stop responding. This may not always work, but in most cases you should be able to open a tty with Ctrl+Alt+F1.

If the application which was frozen was Firefox, then you could log in to your tty and run the command pkill firefox-bin. Then switch back to the GUI, and it should be gone.

As a last resort, if the system is completely non responsive, you may still be able to issue commands directly to the kernel, since it inhabits its' own space in the memory, different to the user programs.

You can talk to the kernel directly by using the SysRq key on your keyboard (although since it shares a key with PrntScrn, you must use Alt+SysRq.

To safely reboot the system, press the following sequence of keys, leaving a few seconds between presses: Alt+SqsRq+R (give control to the keyboard) Alt+SqsRq+E (send processes the tErminate signal) Alt+SqsRq+I (send processes the kIll signal, just incase they didn't get the message the first time) Alt+SqsRq+S (sync data to disks, this is very important) Alt+SqsRq+U (Unmount file systems) Alt+SqsRq+B (reBoot the system…)

A mnemonic suggested for this is “Raising Elephants Is So Utterly Boring”. It is also Busier backwards.

Hardware Information

A useful command to run in order to find out all the hardware on the system is lshw. It is best to run this as root, provided you know the password.

The $PATH

Your Path is a list of directories with files the system can run (either programs or shell scripts), such as /bin where all the system programs are stored and /usr/bin where all the user programs are stored.

There should be a file called .bashrc (DOT bashrc) in your home folder. You probably can't see it because files starting with a dot are hidden. This file tells the shell some things to do when it is opened in order to adjust the environment for you.

This file may include Environment Variables, Aliases and lots more. This file is actually a shell script which is automatically run when you start the shell.

It's best not to touch the contents of the file much at all, since there is important stuff here, but one useful thing you can do is to add directories to the path. Create a directory (either in the GUI or in the Shell) in your home folder called scripts. mkdir scripts

Without changing the file, add the following line at the bottom: PATH=“$HOME/scripts:${PATH}” and save it.

For changes to take effect, you must restart the shell (logout and login in a vc or reopen the window in a terminal emulator).

Then, move your script(s) to the new scripts folder (and remove the sh extension). Then, just type in countfiles and your script should run, from anywhere like any other program in the operating system…

WARNING Make sure your script doesn't share a name with any other program on the operating system, or it may really mess you up… It is best to run which countfiles (or whatever your new script is called). If this lists any programs other than your own then you'd better rename it…

About The Author

Theo Parmakis is a hacker, photographer and essay writer, based in Sheffield. Some of the stuff he makes can be found on his website.

The website includes many scripts and a few tips, which may come in useful when you start getting into using the shell.

 
shellstuff/shellstuff.txt · Last modified: 2010/05/01 16:52 by theo
[unknown button type]
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki