Running programs - Linux Nitty Gritty: Working at the Ubuntu Command-Line Prompt (2011)

Linux Nitty Gritty: Working at the Ubuntu Command-Line Prompt (2011)

Running programs

What happens behind the scenes when a command is typed?

First, bash checks to make sure the command isn’t a builtin—a command that’s part of bash itself.

If the command isn’t a builtin, bash checks to see if the command is in one of the folders mentioned in the $PATH variable. This is a list of folders on the system that contain programs. You can view the $PATH on your system by typing echo $PATH at the command-line:

echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

bash searches through these in order, so looks first in /usr/local/sbin, and then in /usr/local/bin, and so on.

If the command isn’t in any of the folders in the $PATH then bash gives up, and you’ll see a “Command not found” error message.

NOTE Actually, Ubuntu is a little cleverer than most Linuxes in this situation. Rather than immediately report a “not found” error, it will check to see if the command you typed is available for download and installation from the software repositories. If it is, you’ll be told so, and even told how to install it!

What if you’ve manually downloaded a program to the folder you’re browsing, and want to run it? Simply typing its filename won’t work. bash will search the $PATH to look for the command.

Instead, a neat trick is used: the command is preceded by a dot and a slash. The following will run a program called browser that’s been downloaded to the folder the user is currently in:

./browser

In the same way that two dots refer to the parent folder, a single dot refers to the current folder. Some people interpret the dot to mean “right here” and that’s a handy way of thinking about it.

Another method of running the program is to type its absolute path. If browser is located in /home/keir, for example, it could be run by typing /home/keir/browser.

Useful everyday commands

Table 5-1 lists typical commands that are commonly used day-to-day in Ubuntu, along with popular command options. It’s only a brief list, and emphasis is placed on file manipulation commands.

Table 5-1. Useful day-to-day commands.

Command

Description

ls

List files and folders.

Typical example: ls -l

-l : Long listing (show permissions, ownerships etc.)
-a : Show all files, including hidden files
-h : Show KB, MB etc., rather than bytes

cd

Change folder. Type cd .. to change to parent folder.

Typical example: cd Documents

cp

Copy file or folder; first specify file (and path if necessary), then specify destination.

Typical example: cp myfile.doc Desktop/

-r : Copy folders too, including contents (otherwise folders will be ignored)

mv

Move file or folder; can also be used to rename files/folders if a new destination isn’t specified. Note that, unlike cp, it is not necessary to specify the -r option in order to move folders.

Typical example: (moving): mv myfile.doc Desktop/

Typical example: (renaming): mv old.doc new.doc

rm

Delete file(s) or folder(s); multiple files/folders can be specified.

Typical example: rm –rf myfolder

-r : Delete folder; must be used if a folder is to be deleted

-f : Force deletion; don’t prompt user for confirmation when deleting (useful when deleting lots of folders, but must be used with care)

ln

Create a link to a file (similar to a shortcut under Windows); first specify the file (including path if necessary), and then the location where the link should be created. A different filename may be specified for the new link.

Typical example: ln –s myfile.doc ~/Desktop/

-s : Create symbolic link, rather than hard link. In nearly all situations, a symbolic link is preferable, making this practically a prerequisite command option

less

Open specified plain text file in a viewer (use cursor keys to scroll; hit Q to quit). Useful for viewing configuration files.

Typical example: less myfile.txt

df

Show amount of free disk space on all attached filesystems.

Typical example: df –h

-h : Show KB, MB, GB etc. rather than bytes

free

Show amount of free memory.

Typical example: free –mt

-m : Show output in megabytes, rather than kilobytes
-g : Show output in gigabytes
-t : Show totals column

grep

Search through specified file for a word or phrase. First, specify the phrase, and then the file to be searched through.

Typical example: grep –i wireless myfile.txt

-i : Ignore upper/lowercase when searching

man

View the manual (man) page for specified command. A man page is built-in technical documentation—see Appendix B.

Typical example: man ls

nano

Simple text editor that’s ideal for creating, editing or viewing files (particularly configuration files); hit Ctrl+J to re-justify current line should you create a line-break during editing.

Typical example: sudo nano /etc/fstab

umount

Unmount attached storage device. Not a typo! The command is umount and not unmount. Needs root powers. Specify the mount point.

Typical example: sudo umount /media/cdrom

locate

Find specified file; relies on a background database that is periodically and automatically updated. The database can be manually updated by typing the sudo updatedb command.

Typical example: locate filename.doc

-i : Ignore upper/lowercase when searching