- THE GNU BOURNE AGAIN
SHELL (BASH): USEFUL COMMANDS AND KEYS
-
-
(C) 2004 Daniel Brodzik
-
This document is released under the terms of
the GNU Free Documentation License as published by the Free Software
Foundation, either version 1.2 or (at your option) any later
version, with no front-cover texts, no back-cover texts, and no
invariant sections.
-
-
-
INTRODUCTION
-
This is a guide to using the command line
under a shell called GNU BASH. The GNU Bourne Again SHell, or
'bash', is the most commonly-used shell under most Linux-based
operating systems. It has numerous features to help you out when
you're typing at the prompt. I'll first explain some keys that may
be useful to you. After that, I'll explain some useful commands. I
hope this is helpful to those who are completely new to GNU or
Linux.
Because of
graphical desktop environments for Linux like KDE and GNOME, a
working knowledge the command line is optional, but I highly
recommend knowing how to do at least basic tasks on the command line
under Linux because the command line is more flexible than a GUI
could ever be. It is also often faster. Want to erase all files that
end in '.wav'? Type 'rm *.wav'! There are more examples to this. If
you are afraid of DOS prompts because of their user-unfriendliness,
prepare to be amazed at how much easier the Linux command prompt is
to use than the DOS prompt. I liked the DOS prompt, but I love the
Bash prompt! Another reason to learn the command line is that many
useful graphical utilities under Linux are really just front-ends to
command-line programs. Learning the command line can give you better
control of your computer.
-
Note that this guide does not explain the BSD
C-shell (csh; included with many Linux distributions as well as
BSD), ash, the Bourne Shell (sh; found on most older and non-Linux
Unix systems), zsh, or other shells; it just explains Bash. However,
most shells use a similar syntax to most commands, provided the
commands in question are not built-ins.
-
-
This guide first explains some useful
keystrokes in Bash, followed by most useful commands.
-
-
KEYSTROKES:
-
-
Up and down
-
Browse the history by going back one command
(up) or ahead one command (down). Note that if you edit any
commands as you're going through the history, then the change(s)
WILL stay in effect until you press Enter. For example, say you
edit the third command going up, but instead of pressing Enter, you
continue moving through the history. In DOS, this would abandon
your changes, but in Bash, this will NOT abandon your changes.
(That is, if you 'turn around' and go the other way in the history,
then, when you get to the command you edited, your changes will
still be there UNTIL YOU PRESS ENTER!)
-
-
Home; End; Backspace; Delete; Left; Right
-
These keys behave as they would in most word
processors.
-
-
Alt-Backspace
-
This erases one 'word' to the left. An
alternate is Esc, Backspace (press Esc, release it, and press
Backspace).
-
-
Insert,Ctrl-[character]
-
Allows you to insert control characters.
First you press Insert, and then type your control character.
-
-
Tab
-
This is one of my favorites. It acts like an
auto-complete for filenames. If you have a long filename to type,
then type the first few characters of it and press Tab. One of two
things will happen. Either the rest of the filename will be typed
automatically, or it will beep and possibly type some of it. The
first case is easy to understand, but the second case needs a
little explanation. The second is caused by more than one filename
starting out with the same character(s). Bash will type the part
until the the part in which the difference occurs in the filename
and beep. If you press Tab once or twice again, then it will list
the filenames that could fit. This also works for typing the
commands themselves. If there is only one file in the directory
you're referencing, all you have to do is press Tab without typing
the name or any part of it.
-
-
-
Shift-Page Up; Shift+Page Down
-
These are two other favorite keystrokes of
mine. These scroll the screen backwards and forwards, allowing you
to see the output of commands that scrolled off-screen or verbose
output that was too, well, verbose to fit on-screen. This even
works after a 'clear' command has been typed. This does not
work after switching virtual terminals, however.
-
Alt-.
Did you just type a command and want to use
the last or other recently-used filename/pathname in the next
command? Simply type the new command up to the point at which you
wish to reuse the filename/pathname, and press Alt-. until you get to
the filename you want. Useful, isn't it?
Ctrl-D
Do you want to log out or exit the shell right
away? Push Ctrl-D to exit BASH. This types the 'logout' or 'exit'
command for you depending upon whether the current shell is a log-in
shell or not, respectively. The origin of this keystroke is that
ASCII code 4 (Ctrl-D) is the end-of-file character for text files in
Unix and Unix-like systems, including Linux. You only need to know
the origin of this if you're a programmer (or are planning to be a
programmer) or you wish to use the keyboard to type stdin 'file'
input for a command directly. (Each operating system type uses a
different end-of-file character for text files. For GNU, Linux, and
other Unix-like operating systems, it is the Ctrl-D character, ASCII
code 0x04. In MS-DOS, MS-Windows, and other DOS-based operating
systems, it is Ctrl-Z, 0x1a. I don't know for sure what the EOF
character is on a Macintosh. On all modern operating systems, the
end-of-file character is optional, but it is still used for using
direct keyboard input as standard input (stdin).)
-
Alt-P; Alt-N
-
These keystrokes are used to search the
history for a certain string. To use this searching functionality,
press Alt-P, type in what you're looking for, and press Enter. If
what shows up is not what you're looking for, press Alt-P and then
Enter to search again. If you accidentally went too far back, use
Alt-N to search the other way.
-
-
-
-
COMMANDS:
-
-
Here are some useful commands at the bash
prompt. These will be organized by 'normal users' and 'root only'.
To get help on a command, there are some things you can type,
substituting the command for command.
-
command --help
command
-h
man command
info command
-
The first two may cause part of the help to
scroll off-screen. To counter this, add '| more' or '| less' to the
end of the command. I prefer '| less' because it lets you go up and
down as you wish, while '| more' only lets you go forward. Less also
has a search function, in addition to many more features. NOTE THAT
ALL COMMANDS AND FILENAMES UNDER LINUX AND OTHER UNIX SYSTEMS
ARE CASE-SENSITIVE!
-
-
-
Normal Users:
-
-
uname
-
Tells you the Unix name. This includes the
kernel name (which is probably Linux), the kernel version number,
the kernel build time in your local time zone, and other stuff.
This is kind of like Micro$oft Windows' ver command, only more
verbose. Type 'uname -a' to see everything.
-
-
history
-
Displays your command history. 'history -c'
clears the history.
-
-
clear
-
Clears the console screen.
-
-
cd
-
Changes directories. If a directory is
specified, then it goes to that directory. If '..' is specified,
then it goes to the parent of the current directory. If nothing is
specified, then cd takes you to your home directory.
-
pwd
This built-in shell command will display your
present working
directory.
- mkdir
-
Makes a directory.
-
rmdir
Removes an empty directory.
-
ls
-
Used for the same purpose as DIR under DOS.
Lists the contents of the current directory or the one specified on
the command line. Here are some examples of useful command line
switches:
-
ls – Shows a list of only filenames and
directory names.
ls --color
– Forces 'ls' to color-code the file and directory names by
type.
ls -lh – Shows the long format
(-l) and gives sizes in 'human-readable' format (-h) instead of just
bytes. 'Human-readable' means sizes like 23k, 4M, and 1.2G instead
of long numbers.
-
-
dir
-
Same as 'ls' except that backslashes are
shown where you need to type them on the command line in filenames
that need them. Filenames with spaces or apostrophes are mainly the
ones that need these.
-
-
df
-
This nifty little command shows a
nicely-formatted table of all the mounted file systems, how much is
used on each file system, and how much space is free. Use the -h
option to tell it to use 'human-readable' sizes (like 45M, 256k,
and 2.3G) instead of long numbers.
-
-
mount
-
Mounts a disk or disk image. Some common
variants are:
-
-
Note that on some Linux distributions, these
commands should be:
-
-
-
umount
-
Unmounts a disk or disk image, providing the
device is NOT in use. Add a 'u' to the beginning of the command
examples in the 'mount' section above for some examples. Note
that this command is not 'unmount' but 'umount' without an
'n' after the first 'u'!
-
-
nano
-
Starts the GNU nano text editor. GNU nano is
a replacement for the non-free (as in freedom) popular text editor
Pico. (For some odd reason, Pico's license agreement allows
distribution in source code, but not binary, form.) Specify a
filename on the command line to open and edit the text file. If you
installed the 'ne' package for your distribution, then I'd
recommend that over nano.
-
emacs
Starts GNU Emacs, a fancy text editor, to say
the least. :) This editor is very popular in the Unix world, ranking
second only to vi. If you want to be like the prestigious Unix people
to the point of using a popular text editor, use either Emacs or vim
(Vi Improved), but stay away from nvi if you are a new user! In text
mode, the menus are accessed through F10. In the X Window System,
Emacs has a menu bar you can click. Emacs even has an easy-to-follow
tutorial and full documentation within itself! To top it all off,
Emacs has a LISP interpreter embedded in it, so you can write
programs for it! This is one cool editor!
vi
Vi is a very terse but very powerful editor.
It was one of the first screen-oriented editors available for Unix.
(TECO was available for other platforms for a while before vi came
out, and it was screen-oriented.) If you are a new user, please
install Vim (it comes with most non-live-CD Linux distributions),
which is Vi Improved. Vim has a
very good interactive tutorial. In fact, after one hour in the
tutorial, I already felt quite comfortable with it. If you have Vim
installed, you should be able to type "vi" to access it; if
not, use "vim" instead. Vim has a GUI, so you should be
able to use "gvim" under the X Window System to get a
graphical interface. I would consider writing a tutorial on using
Vim, but the tutorial that comes with Vim, which can be accessed by
typing "vimtutor" at the command prompt, is very good.
- less
-
This is a file viewer and text pager. You
can scroll through the output of a command by typing 'command
| less', or you can use it to view text files by typing 'less
filename', replacing command with the command you wish to
scroll through the output of or filename with the text file's name.
-
-
cp
-
Copies files. Multiple files can be
specified on the command line, like this:
-
cp zork gork bork /home/user
-
-
If you want to copy a whole directory
structure, use the '-r' switch, like this:
-
cp -r directory ~/foo
-
-
mv
-
Moves or renames files. Like Micro$oft
Wincrap, only the directory entries get moved and/or renamed if the
source and target are on the same file system. This command uses
the same syntax as 'cp', except that you don't need to use '-r' to
move a directory.
-
-
ln -s
-
This is one of my favorite Unix features.
This command creates a symbolic link. Coming from using Wincrap, I
am familiar with shortcuts. Think of this as going one step further
than a shortcut. Whereas a shortcut under Micro$oft Wincrap takes
you to the original under Windows Explorer, a symbolic link
actually functions as the original item when referred to in a path,
and any writes go to the original item in its original location.
For example, if you created a shortcut under Windows to
C:\WINDOWS\SYSTEM under C:\ and called it 'SYSTEM', then references
to C:\SYSTEM or even C:\SYSTEM.LNK would not refer to
C:\WINDOWS\SYSTEM; they'd refer to the shortcut file itself. Under
Linux, however, creating a link in ~/ to /usr/local called 'local'
would make all references to ~/local refer to the directory
/usr/local. In fact, if there was a file called 'foo' in
/usr/local, then ~/local/foo would be a valid path! (By the way, ~,
when used in a Unix pathname, refers to the current user's home
directory.) This applies to everything except deleting the link;
'rm'ming a symbolic link will delete the link and not the original
file/directory.
-
-
rm
-
Deletes a file, but not a directory (unless
used with -r).
-
-
rm -r
-
Deletes a directory and ALL its contents.
Same as DELTREE under DOS, except that rm does NOT prompt first! BE
CAREFUL!
touch
What 'touch' does depends on whether the
filename you give it exists or not. If the file exists, 'touch'
updates the time and date stamp to 'today, right now'. In other
words, this has the same effect as 'copy /b filename+,,' under
DOS. However, if the file does not exist, 'touch' creates a
zero-byte file with the name you gave 'touch'. This command is often
used by the operating system tools for 'stamp' files and lockfiles.
-
su
-
After typing this, you'll be asked for the
root password. If you get it right, then you'll become root (the
'superuser'). To exit su, type 'exit'. Note that if you type a user
name after 'su', then you will be prompted for that user's password
instead and, if the password is correct, you'll become that user.
exit
- Exits a shell. Also works as a
replacement for 'logout'.
-
-
logout
-
Logs you out. I usually type 'clear;logout'
to clear the screen when logging out.
-
-
clear
-
Clears the screen.
-
-
startx
-
Starts an X Window System (X11) GUI session.
You don't need this if you're using a display manager like kdm,
xdm, gdm, or wdm. In other words, if you see a graphical login
prompt when you start up, you don't need startx.
-
-
killall
-
This kills process(es) by name. Use '-s
KILL' to forcefully terminate the process(es). Note that unless you
are root, you can only kill your own processes.
-
-
kill
-
This ends a process. You must specify the
PID(s) of the process to be killed. Use '-s KILL' to forcefully
terminate the process(es). Note that unless you are root, you can
only kill your own processes.
-
-
top
-
This will show a table of
processes and some system statistics. It's pretty
interesting....
-
-
pidof
-
This reports the Process Identifier(s)
(PID(s)) of a process when given the name.
-
-
ps ax
-
This shows a list of all the running
processes.
-
-
command &
-
When you type a command and end the command
line with '&', the process will run in the background, and you
will be returned to the command prompt. You will be given a status
line, like this:
-
[1] 665
-
This means, in the example, that the job
number is 1, and the PID is 665. The job number will be useful to
you in the next two commands. Note that in some console-based
programs, you can also press [CTRL][Z] to put the job in the
background. If you do this, however, the program will be stopped.
The next two commands will make it go again.
-
-
fg
-
This brings a program that's in the
background to the foreground again. To use this command, enter 'fg'
followed by the job number you got when the program went into the
background.
-
-
bg
-
This pauses/resumes a program that's in the
background. To use this command, give the job number that you
received when the program went into the background.
-
-
whoami
-
Tells you who you're logged in as.
whereis
Would you like to know where a certain
program/command's binary and manual page are stored? Type 'whereis
command'.
- Commands most useful as the
superuser (root):
-
Note that some of these, like 'chmod', are
also useful as a normal user if you use them on your own files.
-
-
poweroff
-
As you may have guessed, this is used to
shut down the computer safely. You could also type 'init 0' to get
exactly the same effect. If you have APM or ACPI support installed,
then this may turn off your computer automagically. This is great
for virus scanners, etc.
halt
This command is equivalent to 'poweroff'.
- reboot
-
Reboots the computer. You can also use 'init
6' to get exactly the same effect.
-
-
fsck
-
This is Linux's equivalent to scandisk. This
works on FAT12, FAT16, FAT32, ext2, ext3, and more. Since ext3 is a
journaling file system, fsck is almost never needed to fix problems
on ext3 disks/partitions. Anyway, here are some command-line
examples:
-
fsck /dev/hda1 --- checks the first partition
of the first hard drive for errors if it's not marked clean
fsck -f
/dev/hda3 --- Forces a filesystem check on the third partition on
the first hard disk
fsck -fy /dev/fd0 --- Forces a
filesystem check on the first floppy drive and automatically fixes
any problems found without prompting you first.
- WARNING!!! RUNNING FSCK ON A
FILE SYSTEM THAT IS MOUNTED READ-WRITE MAY, AND PROBABLY WILL,
CAUSE SEVERE
FILE SYSTEM DAMAGE!! If you forget this and try to fsck a mounted
file system, fsck will give you a warning before it does anything.
To unmount a file system, use 'umount mount-point'. To
mount all your automatically-mounted file systems again, type
'mount -a'. To remount an already-mounted file system read-only,
type 'mount -o remount,rw mount-point'. To remount an
already-mounted file system read-write, type 'mount -o remount,rw
mount-point'. It's recommended (and usually easier) to
unmount the file system and mount it again later, but you will
need to remount the root filesystem as read-only to check that
because you cannot unmount the root...
-
-
It is likely that you will have to go into
single-user mode to change the access mode of the root file system
to read-only. To do this, type "init 1" as root. To get
back to normal, you can type "exit" at the command
prompt. Before returning to the normal runlevel, don't forget to
return your root file system to read-write mode and to run "mount
-a" to mount everything else again.
-
-
cfdisk
-
Runs Linux's easier-to-use disk partitioning
utility. USE WITH CAUTION!! This program can PERMANENTLY
erase data on your disk if you aren't careful!
-
-
fdformat
-
Formats a floppy disk. Unlike DOS and
Wincrap, you must also place a filesystem on the disk by using the
next command.
-
-
superformat
-
This is supposedly a better version of
fdformat. It has many more command-line options.
-
-
mkfs.type
-
Makes a filesystem in either a device
(/dev/x) or a file. Type 'mkfs' BUT DON'T PRESS ENTER YET. Then,
press Tab twice or thrice to see what filesystems you can make.
(Yes, thrice is a word; it means 'three times'.)
-
-
chmod
-
Changes file permissions. The syntax is:
-
-
chmod mode file
-
Mode can be an octal number. It can also be
in the form of the user (u for user, g for group, o for others, a
for all), a + or - sign, and then an action (r for read, w for
write, x for execute, X to make it so that execute is changed only
if the file is a directory OR if the execute permission is already
set). A common code for the octal number is 666 to allow ALL users
to read and write the file. File can be a wildcard. For more
information on file permissions, read my file system guide, which
is another one of my Linux guides.
-
-
Examples:
chmod 666 fred.txt --- Enables everyone to
read and write to 'fred.txt'
chmod
ug+rwx jim --- Enables the file owner and members of the group
that's associated with the file 'jim' to read, write, and execute
it.
chmod
u+w,go-w,a+x sheila.sh --- Enables the owner to write to
'sheila.sh', prevents others (except root) from writing to it, and
allows everybody to execute it. 'sh' is a common extension used for
shell scripts that aren't used frequently. (If the shell script has
an extension, you must type the extension every time you run the
script at the command line.)
chmod a-r,a+w,a-x barney.foo --- Sets
some weird permissions for 'barney.foo': nobody except root
can read the file, everybody can write to the file, and
nobody can execute the file. This set of permissions is not
very common except on some device nodes in /dev. 'foo' is a common
metasyntactic variable, and so are these names.
-
chown
-
Changes the owner of a file. Syntax is:
-
chown user:group file
Either the user or the group can be omitted.
(Sorry, you cannot omit both.) If the user is omitted, there must
be a colon before the group.
-
adduser
-
This one adds a user to a group.
-
-
useradd
-
groupadd
-
These add users and groups. I'm not sure how
to use them, so read the manual pages ('man useradd').
-
-
modprobe
-
Loads a kernel module. With -r, it removes a
module. You may want to use modconf instead. It's explained later
on in this article.
-
-
insmod
-
This is the 'simpler' version of modprobe.
Unlike modprobe, it does not automatically load any dependencies.
-
-
rmmod
-
Unloads a kernel module. Again, you may want
to look at the section for modconf later on in this article.
-
-
lsmod
-
Do you want to see what modules are loaded?
Type this command as root.
-
-
MAKEDEV
-
This command is to be entered in all
uppercase. It makes device files that you don't have. To use it,
first type 'cd /dev'. Then, type 'man MAKEDEV' to see some of the
different device files you can make. Note that the man page for
MAKEDEV was written in 1994, and, as such, is "woefully out of
date". You don't need this command if you use devfs. (See my
file system hierarchy guide to learn more about devfs and why I
highly recommend using it. In a nutshelll, Devfs is a way to
automatically manage your device files so that you don't have to
manually create and delete them; this job gets offloaded to the
kernel and a program called devfsd.)
-
-
-
Debian-specific commands:
-
These commands must be run as root in
order to be effective.
-
-
dselect
-
Starts Debian's dselect program, the easy
way to add and remove software that comes with Debian.
-
-
modconf
-
This utility is a menu-driven program for
configuring modules. Changes take effect immediately and will
remain in effect after rebooting. I'd recommend using this over
modprobe, insmod, and rmmod if you use Debian.
-
-
dpkg
-
The Debian packaging tool. I use it to
install packages that I downloaded from the Internet. If you have a
package file (they end in '.deb'), then you can type 'dpkg -i
<filename>' to install it. This may cause dependency
problems, so if you get any error messages about missing
dependencies, run dselect A.S.A.P.! Choose '[S]elect' from the
menu, press [Enter] to leave '[S]elect', and select '[I]nstall'.
Until you do that, you won't be able to use the software in the
package you installed.