HOW TO USE LINUX TERMINAL

by Donald Daniel, 2010, revised Dec 2017

up one level

If the lines of text are too long you can fix the problem with these instructions.

GETTING STARTED

This article teaches linux for beginners. This is based on Debian linux, but will mostly apply to any version of linux. It assumes that the linux installation included "gnome desktop" and "standard system". If you have not yet added linux to your windows computer instructions are provided here.

This introduction to linux is designed to get you started quickly. If you need a much more lengthy and detailed article see http://tldp.org/LDP/intro-linux/html/.

These instructions emphasize features of linux that are different from Windows. You must first login then you will see the gnome desktop screen. Make sure that you log in as an ordinary user, not as superuser. In the top left corner of the gnome desktop screen you will see "activities". If you click on that, below it will be some icons to select some of the more popular software. If you click on these items they will work in a way that will be familiar to windows users, though not identical to windows. In the upper right corner of the screen will be an array of symbols. If you click on it, a black window will open below it. At the bottom of the black window will be three symbols. If you click the symbol on the right, a new window will appear that will give you the choice to "power off" that you will use to shut down the computer.

Click on "activities". One of the icons, a three by three array of dots, will let you see "applications" when you click on it. To the right of the array of application icons will be a vertical column of four small circle icons. Click on each circle icon to see a different set of application icons. Search through the applications until you find the terminal icon, a rectangle. Click on the terminal icon to open a terminal window. If you right click on the terminal icon you can click on "add to favorites" to make the icon appear on the desktop so it is easier to get to. The real power of linux will be accessed from a terminal window, and that will be the focus of this course. You can open more than one terminal window at a time. To do this, in the first terminal issue the command "gnome-terminal" and another terminal will open. You could also right click in the terminal window to open another terminal window.

Click the mouse anywhere in a terminal window to activate the terminal. Anytime you are instructed to type a command, hit the "enter" key afterward, since no command takes effect in linux until you hit "enter". Then enter "pwd", which means "print working directory". Directory in linux means the same as folder in windows. You will see something like "/home/yourname" where "yourname" is whatever user name you picked when you installed linux. Next, enter "cd /", then enter "ls". "ls means "list". You will next see the following list of directories and files: bin boot cdrom dev etc home initrd.img lib lost+found media mnt opt proc root sbin selinux srv sys tmp usr var vmlinuz.

You are now at the root directory of the file system, "/". This is like being at the bottom of a ship below all the machinery. Most of the stuff you see is the machinery of the operating system. The directory "home" is like the passenger decks on the ship. Now enter "cd", then enter "pwd" and you will see that you are back in your home directory.

In your home directory enter "mkdir wkspc". "mkdir" means "make directory". Then enter "ls". You will see the new directory "wkspc" that you have just created with the command "mkdir". Now "cd wkspc". Now you are in the directory "wkspc". Now enter "mkdir back ltr photo scratch". Now enter "ls" and you will see the directories you have created to hold your backup files which will be saved to a USB stick to save valuable personal data in case of a hard drive failure, letters or emails that you have written, photographs you have loaded onto your computer, and a scratch directory where you can do messy work that you only want to retain temporarily.

Enter "cd scratch" to get into your scratch directory. Next you should learn the vi editor, and in your scratch directory create some little unimportant file of nonsense called "xyz" that you can experiment with.

Now enter "cp xyz abc". "cp" means "copy". Then "ls". You will see that you now have two files, "xyz" and "abc". Look at them with the vi editor and see that they are the same. Now enter "rm abc", then "ls" and you will see that you have removed "abc".

There is an easy way to refer to certain directories. Your home directory is "~". The directory where you are at any given time is ".". The parent directory of where you are is "..". If you are in your scratch directory, enter "pwd" then "ls", then enter "cp xyz ~". You have now made a copy of xyz in your home directory. Enter "cd .." then "pwd" and "ls". You are now in your wkspc directory. Again "cd ..", "pwd" and "ls". You are now in your home directory and can see the copy of the file xyz that you just made. Now "cd wkspc/scratch", "pwd" and "ls", and you see that you are back in your scratch directory. Now in your scratch directory and enter "rm ~/xyz" and you have removed the copy of xyz in your home directory.

Now in your scratch directory enter "cp xyz t1", "cp xyz t2", "cp xyz t3", then "ls". You now have xyz, t1, t2, and t3 in your scratch directory. Now we use the wildcard character *. Enter "rm t*" then "ls", and you will see only xyz remains, the others have been removed. By saying "t*" we referred to everything starting with "t". If we had said simply "*" we would have removed everything. "rm" just removes files. "rm -rf" removes both files and directories. You are doing this as an ordinary user, not a superuser, or root, as superuser is also called. As an ordinary user you cannot remove any of the stuff you saw in the directory "/". But as superuser you could. This is VERY DANGEROUS!!! Be very careful what you do as superuser. If at "/" as superuser you did "rm -rf *" you would remove your whole linux system!

If you want to move a file from one place to another, you could "cp" to create the new version then "rm" to delete the old version, but it is simpler to just "mv" which means "move". "mv" can also be used to rename a file without moving it. "mv xyz uvw" changes the name from "xyz" to "uvw". To copy a whole directory and not just a file, use "cp -r" and the name of the directory. "ls" will list directories and files, but you may not know which is a directory and which is a file. There are also hidden files with names having "." as the first character. To list all directories and files, including the hidden ones, use "ls -aF". In your home directory you will see some hidden files. Directories names will end in "/" to distinguish them from files. To see the size of files and other information about the files do "ls -l".

To see how much of your filesystem is used enter "df". The entry "/dev/hda2" or similar which is mounted on "/" is the important one. To see how much space is used by each directory enter "du -bhs *".

BECOMING SUPERUSER

As previously noted, becoming superuser gives you a dangerous amount of power. Only become superuser to carefully do a task that requires you to be superuser, and cease to be superuser as soon as the task is completed. To become superuser, in a terminal window enter "su", then you will be prompted for your superuser password. When you are not superuser, the prompt character at the left of the terminal window will be "$". When you become superuser the prompt character will change to "#". When you are ready to cease to be superuser, in the terminal window enter "exit". As superuser you can shut the system down with "shutdown -h now". You can move executable scripts or user created sofware to /usr/local/bin. Various other special tasks may require you to be superuser.

FINDING INSTRUCTIONS

There are several sources of information contained in your linux system, and if these fail, you can google your question and probably find and answer.

To find out how to move things we would enter "apropos move". A list of commands related to moving will be shown. If the list ran off the screen we could contain it with "apropos move | more". The "more" command limits the size of lists to one screen at a time, with the spacebar being used to see the next screen full of a long list. If we want to find out more about a command in the list, such as "mv", we would enter "man mv". "man" means "manual" or "handbook". Most commands have a "man page". An exception is "cd". This is because cd is not really a stand alone command, but is part of bash, so "man bash" will contain an explanation of cd. Bash is a program that starts running automatically when you open a terminal window. Its purpose is to create a user friendly environment.

There other places containing instructions, most notably in /usr/share/doc. If you do "ls" the list will scroll off the screen, so to "ls | more", then hit the spacebar to see each screen of the list. These are all directories. Most of the files in these directories end in ".gz". The vi editor will not read these. Instead, to read "file.gz" enter "zless file.gz". This will unzip the file into the vi editor.

ADDING SOFTWARE

If you cannot find software on your computer to do what you want, you can probably add the software free of charge. If you already know the name of the package you want to add, go directly to the synaptic package manager on your computer. If you do not already know the name of the package you want go to http://packages.debian.org /stable and look for what you want. Make a list of the packages you want. Then enter "su" and your superuser password, then enter "synaptic", and then click on "search" and enter the name of the package you want. When you click on search a "find" window will pop up. In the find window is a "look in" bar with different options. Sometimes one option will find what you want and another option will not. You will see an alphabetical list containing the package you want. Scroll down the list until you see the package you want. Click on it to see a description. If it is really what you want click on the tiny square to the left of the package name to select it for installation. The package you selected may depend on additional packages, which will automatically be selected also. When you have found and selected all the packages you want, click on "apply". When the process is all finished, exit the synaptic program and be sure to enter "exit" to quit being superuser and go back to being an ordinary user.

If you are not sure whether you have a certain piece of software on your computer, use the "whereis" command. Thus "whereis vi" will yield "vi: /usr/bin/vi /usr/share/man/man1/vi.1.gz" which shows where the various parts of vi are stored. But "whereis dog" will simply show "dog:" meaning that you have no such program. Sometimes the "locate" command will find things that the "whereis" command will not.

To see a list of the software packages that are already installed, enter "dpkg --get-selections > temp" then use the vi editor to look at the list in the file temp. This procedure will only list debian packages. Software obtained from other sources will not be included.

If you add non-debian software to your computer, it will have to be invoked from within a terminal window, not from an icon. Thus, if you install the google chrome browser, to invoke it open a terminal window and enter "google-chrome".

From time to time, linux will automatically add updates to your existing software. If you would like to force an update to make sure you are up to date, here is how. Open a terminal. Become superuser. Enter "apt-get update". When stuff stops scrolling by, "exit". Click on "activities" in the upper left corner of the screen. Click on the array of dots in the lower left corner of the screen to see more applications. Click on the third dot down of the vertical column of four dots on the right side of the screen. Click on the "package updater" icon. You will see a list of package updates that you need. When you click to install the updates, you will have to enter the superuser password again.

BACKUP PERSONAL FILES

About once a month you should backup your personal files, saving them on two USB sticks, so that if your hard drive fails you will not lose your work. You do not need to be superuser to do this. First, copy all the files you must save to your backup directory, wkspc/back. Then enter "mount". Notice the last line of the listing. Insert a USB stick into a USB port on your computer. A large rectangular window may pop up, get rid of it, we will not use it. Enter "mount" again. Notice a new line added to the end of the listing, something like "/dev/sdf1 on /media/2365-21A3 type vfat ...". Minor details may be different on your computer. In fact the number may be different on your computer each time you insert a USB stick. This tells us the the USB stick is /media/2365-21A3. Now "cd /media/23*". Here the "*" is a wildcard so you do not have to type the whole thing. Then "ls" to see what is on the USB stick. Do "rm -rf *" to erase the USB stick. Then "cp -r ~/wkspc/back .", where the final "." means put the copy in the directory where you presently are, the USB stick. If there is a lot of stuff, this may take a while before the cursor re-appears. Do not be deceived, the copy is not finished at this point. Enter "sync" and wait until the cursor re-appears. Now the copy is finished. To make sure the copy is a good one, enter "diff -r back ~/wkspc/back". If no errors appear the copy is a good one. First "cd" to your home directory, then you can remove the USB stick. It is best to make backup copies on two USB sticks just in case one of them should fail for some reason, and keep them stored in separate places.

EXECUTING PROGRAMS

If the program is located in one of the "bin" directories just typing the name of the program will execute it no matter what directory you are in. The main bin directory is "/bin". You can see it listed if you type "ls /". When you use the synaptic package manager to install programs they will be put in that bin directory or a bin directory at a lower level. You should never tamper with these directories yourself, let synaptic do it. If you want to install a program by hand yourself, put it in /usr/local/bin, which is for use by all users. You must be superuser to do this.

If the program "xyz" is not in one of the bin directories, but is in the directory where you are, typing "xyz" will do nothing. Instead, type "./xyz" which will execute it. Notice the period before the slash.

If you start a program running, and it keeps running longer than you want, you can ususally stop it by holding down the "ctrl" key and at the same time hitting the "c" key. This is ususally described as "control-c" or "ctrl-c".

STANDARD OUTPUT

Some programs put their output to "standard output", which is the screen. Thus the command "ls" will list the files in the current directory on the screen. But suppose you want the list in a file. "ls > temp" will put the list in the file "temp". The ">" redirects standard output to a file. If you wanted to print the file you could enter "lpr temp". But you could use a pipe, "|", instead. Thus "ls | lpr" will pipe the output of the "ls" command to the "lpr" command and the list will be printed.

The command lpr probably originally stood for "line printer". The original computer printers were either mechanical line printers or mechanical accounting machines that could also print. Originally they took data from punched cards, later directly from the computer from commands such as "lpr". In 1963 I used an IBM 407 accounting machine that read cards. It was about 51 inches high, 73 inches wide, and 31 inches deep from front to back. It weighed 2620 pounds. It was mounted on springs and rocked back and forth as it printed a line of text at a time on a long stream of fanfold paper.

USING THE BROWSER

The way the firefox browser is initially set up you cannot view html documents that are on your own computer, not on the internet. To fix this, click on the icon with three horizontal bars, "customize", right click on "open files", click on "add to toolbar". Now you have an icon that will let you see local files.

The browser operation is not as obvious as it should be. The default way to print is with ridiculously small margins. To set the margins, click on the icon with three horizontal bars, then "print", "page setup". In "manage custom sizes", change the margins to one inch, "close", "apply".

LINUX TOOLS

You could spend a lifetime mastering all the tools in linux. Here is a sample to familiarize yourself with the sorts of tools that are available.

You will sometimes want to re-format a USB stick. One time I wrote a file on a USB stick and took it to be read by another computer. The person used commercial application software to read the file off the stick. But the application software corrupted the stick so I could no longer write to it. So it needed to be re-formatted. Here is how. Do not put the stick in the computer yet. Open a terminal window. Enter "mount". Write down the first few words of the last line. Put the USB stick in the computer. Enter "mount" again. Notice that a new line has been added after the last line. It might be something like "/dev/sdb1 on /media/yourname/23...". If you enter "cd /media/yourname/23*" you will be in the USB stick. "ls" will show what is on the stick. But you must not be on the stick to re-format it, so "cd" to get back to your home directory. Enter "su" and the superuser password so that the first character on the line changes from "$" to "#". Enter "umount /dev/sdb1" so that the stick is no longer mounted but it is still in the computer. Enter "mkfs.vfat /dev/sdb1" to re-format the stick. Enter "exit" so you are no longer superuser. Now "mount" will not show the stick. Remove the stick and re-insert it into the computer. Now "mount" will show the stick. "cd" to the stick at "/media/..." and "ls" to see that it is now a blank re-formatted stick.

Suppose you want to find all occurences of the word "glue" in every file in every directory at and below where you are. "grep -r glue *" will do this. "grep -c glue *" will count the number of times the word "glue" appears in every file in the directory where you are. If you want a file named "temp" that contains all the lines in "myfile" containing the phrase "I want this line" then "grep 'I want this line' myfile > temp" will do it. Then the command "wc -l temp" will tell the number of lines in the file temp. Grep is a powerful tool for analyzing the use of your website that is recorded in a logfile of hits. You can find how often one part of your website is found by linking from another part of your website.

Suppose you have downloaded a suspicious file and you want to find out what kind of file it is. The command "file *" will list every file in the directory where you are and tell what kind of file it is.

You will probably want to install the "zip" program so you can use zip files. If you want to create fancy pdf documents you will want "texlive-latex-base". If you want to convert an html document to pdf format, "htmldoc" works well, but skips graphics in the html document.

For hearing some types of audio files you will need the "vorbis-tools" package. For some video work you will need "cdrskin".

For drawing things, you could use the openoffice draw program which comes with your debian installation, which I have never tried. I use the older program "xfig".

To see graphics files or pdf files click on "applications" "system tools", "file browser", then click to find the directory where the file is. But if you are in a terminal window where the file is, it would be quicker to use a command to display what you want to see. If the imagemagick package is installed, "display xyz" will display the graphics file xyz for most formats. If "gv" is installed, "gv xyz" will display pdf files.

For photography, there is "dcraw", which converts raw still camera formats to ppm format, which can then be converted to jpeg with "pnmtojpeg". If there is a gray card in the scene, or a separate gray card shot taken at the same time, perfect color balance can be achieved. "Gimp" performs most of the functions of Photoshop. "Kino" does video editing on video from standard definition digital video, but not yet high definition video.

Suppose you have a list of old movies in the form title, year, comments in a file named "list" like this:

show people, 1928, m
the battle of the sexes, 1928, dw griffith
the patsy, 1928, documentary following movie

You can sort this list by year, and within each year alphabetically with this command script:

sort -t , -b -k 2,2 -k 1,1 list > temp
mv temp list

When you first use the vi editor to create this script in a file arbitrarily called "srt", "ls -l srt" will show that it is not executable. Make it executable with the command "chmod +x srt". Now "ls -l srt" will produce different results showing that it is executable. Then to sort your list enter "./srt". Each field title, year, comment is separated by a comma. In the script we have chosen the comma as a separator. So if a title had a comma in the title, the comma would have to be left out for the sort to work properly.

There are many linux commands that change a file from one format to another format. This includes text files, graphics files, audio files, etc. Suppose you want to change a large number of files in one format with one suffix to another format with another suffix. File suffixes matter to the windows operating system, but they do not matter to the linux operating system. Suffixes do matter to some programs in linux. Take a trivial example of files like "t1.txt" that are lower case with "txt" suffix. You want to make them upper case with "caps" suffix. The command "tr 'a-z' 'A-Z' < t1.txt > t1.caps" would do this. But it would be a lot of work to do this for t2.txt, t3.txt, ...t100.txt. The following script is a program written in bash that will do this all at once:

#  make caps files from txt files
namechange ()
{
oldname=$1
newname=${oldname%.txt}.caps
echo "$newname"
tr 'a-z' 'A-Z' < $oldname > $newname
}
for filename in "$@" ; do
namechange "$filename"
done

If we arbitrarily choose to call the script "proc", then "chmod +x proc" will make it executable. "./proc t1.txt" will perform the operation for t1.txt. But "./proc *.txt" will perform the operation for all of the files in the directory with "txt" suffix. For some other format changing command and other suffixes, you could change the suffixes in this script and change the command line. If your line is too long, end it with "\" and continue on the next line, but you must be careful that the "\" is the last character on the line, and there is no invisible space character " " following it. The line starting with "#" is just a comment, and has no effect.

The previous example is instructive but not very useful. Now for a more useful version of the kind of script in the previous example. Suppose you want to create "png" files representing only the ink on the stained and yellowed pages of an old manuscript or book. You use a digital SLR camera and incandescent light to photograph the pages. You set the camera to save the images in raw DNG format. Transfer the DNG files to your computer. Use the dcraw command first to get the numbers needed to make the yellowed pages appear white. See the "man" page for dcraw to understand what you are doing. An example might be:

dcraw -v -A 1500 1000 500 500 page1.DNG
Write down the numbers that are needed to whiten the pages. Then use the numbers to convert all the DNG files of yellowed pages to ppm files of white pages:
dcraw -v -r 1.0 1.0327 2.293 1.037 *DNG
Then use gimp to see the left, top, right and bottom coordinates in the image that are beyond the page of the book and that you want to cut out of the image. Next use the following script to change all of the ppm files to png files:
# convert ppm to png 
namechange ()
{
oldname=$1
newname=${oldname%.ppm}.png
echo "$newname"
ppmtopgm "$oldname" \
| pnmcut -left 792 -top 400 -right 3220 -bottom 2450 \
| pnmflip -ccw \
| pnmnorm -bvalue 150 -wvalue 151 \
| pnmscale 0.7 \
| pnmtopng > "$newname" 
}
for filename in "$@" ; do
namechange "$filename"
done

You will need to consult the "man" pages for each of the commands in this script to understand how it works.

The command "mv" for moving and renaming files comes with linux. But you might want to install the more powerful command "mmv". Suppose you have a list of files like "access_110606.log", "access_110613.log", etc. representing log files for the month of may. The command [mmv "*1106*.log" "may-#2"] would change the names to the simpler form "may-06", "may-13", etc. Here the brackets [] are not part of the command, but are used because the double quotes " are part of the command.

My favorite linux tool is oo2c, which I use to program the computer when I want to accomplish something for which there is no built in function. It is not available from debian, and must be installed without the help of synaptic. It automatically installs in /usr/local/bin.

If you want to save an example of a detailed procedure so that you can email it to someone or remember how you did it, the "script" command is useful. Before you start your procedure type "script". Then after completing your procedure hold down the "ctrl" key and type the letter "d". This will write everything that has just happened into a file called "typescript". In the vi editor you can see that the file will be in windows format with carriage returns at the end of each line. You can remove these if you wish as explained in the article on how to use the vi editor.

There are many more ways in linux to make hard jobs easy. All of the tools mentioned here are free software. Finally, you should get yourself a bound book of lined notebook paper and title it "howto". Every time you figure out how to do something in linux, enter it in your "howto" book. This will save you the frustration of having to figure out the same thing twice.

DISASTER RECOVERY

A minor disaster you may want to recover from is if you start a long printout, and realize something is wrong and want to stop the printout. Enter "lpq" to see the job number of the printout, which might be 87. Then enter "cancel 87". Then enter "lpq" again and the print job should be removed from the list. This will stop sending any more pages to the printer. But the printer memory may already be full of pages that it has not yet printed. You may have to temprarily remove the power plug from the printer to erase the printer memory. One time the printing quit working because of an unknown software glitch. "Restart" would not fix it. I had to shut down completely and turn the computer back on.

Another disaster is when the screen freezes and nothing you do has any effect. Hold down the "ctrl" and "alt" keys, and tap the "F2" key. This should switch you to a black screen. Enter the user name "root", then the root password, then the command "shutdown -h now". This will shutdown the computer. Alternatively, you could enter "shutdown -r now", and the computer will shutdown and reboot.

Hard drives do not last forever. If bootup fails with a black screen requiring you to run "fsck", or if your personal files mysteriously become "read only" so you cannot write to them, your hard drive may be starting to wear out. Of course, a new hard drive will be blank, requiring new software installation. You need to back up personal files first. It is easy to replace a hard drive on a desktop machine with a full sized tower, not so easy on smaller machines. On a full sized tower you typically remove the side panel of the cabinet, unplug two connectors from the back of the hard drive, unscrew two screws holding in the hard drive, slide out the old hard drive and slide in the new one. Hard drives are a very small fraction of the cost of a new computer. Before you remove the old hard drive you may want to zero it out to protect private files. In the directory /dev see if sda or hda is listed. If so that is the name of your hard drive device. As superuser the command "dd if=/dev/zero of=/dev/sda" will zero your hard drive after a few minutes.

up one level

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.