Linux basic guide
From HvWiki
This page is intended to introduce Linux newbies to the command-line. The current trend is toward GUIs for all purposes. While this has its merits, such as being easy to learn, there are many things which can be done more quickly and easily through the command-line.
Contents |
The Shell
A shell is a program in which you type commands. The shell most commonly used in Linux is bash, the Bourne Again Shell. The commands below are meant for use in bash.
A dollar sign ($) at the end of a prompt is used to denote a non-root command prompt. A root (also known as superuser) prompt is denoted by a #. For example, a non-root prompt may look like this:
smith@compy ~ $
whereas a root prompt may be
root@compy ~ #
There are ways to customize this prompt which will be discussed later. In this guide, a simple $ will be used to denote a non-root shell and a # to denote a root prompt.
The shell always operates in a directory called the working directory. This directory can be changed by the cd command, for example:
$ cd mydir
The current directory can be printed with pwd:
$ pwd /home/smith/mydir
In bash, a tilde (~) is used to denote a home directory. In most cases this will be in /home, like /home/smith or /home/jane. The directories ./ and ../ are special directories. ./ refers to the current directory, while ../ refers to the directory above the current one.
Commands in bash are actually programs in the user's PATH variable. To see the path, type
echo $PATH
in the shell. This shows a list of directories separated by colons, such as
/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin
Each of these directories contains executables which can be used by simply typing the executable name. When a command is typed, bash searches through each directory starting with the first one in the path for an executable with that name. If no executable is found that matches <command>, bash returns
-bash: <command>: command not found
Executables outside PATH can be executed by typing either the relative path to the executable (e.g. ../misc/stuff/print ) or the full path (e.g. /home/smith/misc/stuff/print ).
About Commands
In Linux, a command usually follows the syntax
command <options>
where the options are given as a single-letter option preceeded by a -:
-f
or as a multiple-letter option preceeded by a --:
--foo
or multiple single-letter options as the letters together preceeded by a dash:
-xvjf
Some commands have a different syntax for options, but this is the form most commonly encountered.
Basic Commands
Listing Files
ls <options> <dir> ...
Lists the contents of <dir> etc. If <dir> is a file, it prints that filename if it exists. If no <dir> is given, it lists the contents of the working directory. By default it does not show hidden files (files whose names are preceded by a period, e.g. .file). To show hidden files, use the option -a. Use -l to give verbose output, which shows file access and modification times, permissions, ownership, and size. Note that multiple <dir> arguments can be given.
Copying Files
cp <options> <source> <dest>
Copies file or directory <source> to <dest>. If <dest> is nonexistent, it copies <source> to that name and location. Use -R (or --recursive) to copy directories. To preserve mode, ownerships, and file access/modification times, use -p.
Moving and Renaming Files
mv <options> <source> <dest>
Moves file or directory <source> to <dest>. If <dest> is nonexistent, it moves <source> to that name and location. Use -R to move directories, as with cp.
Note that this is also used for renaming files - there's no rename command, unlike DOS. To rename a file from foo to bar, use
mv foo bar
Deleting Files
rm <options> <file>
Removes (deletes) <file>. Use -i for interactive mode (prompts for each file), or -f to remove without prompting. By default it should prompt you for files that are marked read-only and other such things, though it varies from distro to distro. To remove a directory and its contents recursively, use -r. Be very careful when combining the -r and -f options...
Creating Directories
mkdir <options> <dir>
Makes the directory <dir>. If the directory has a nonexistent parent (e.g. you try to make the directory /home/smith/foo/bar/foobar but foo does not exist), use the option -p. To make the directory with a particular set of permissions, use the option -m <permissions> where <permissions> can be expressed in the format explained below for chmod.
Adjusting File Permissions
chown <options> <owner> <file> chown <options> <owner>.<group> <file>
Changes the ownership of <file> to <owner>, or changes the ownership and group of <file> to <owner> and <group>. Use -R to change ownership/group recursively.
chmod <options> <permissions> <file> ...
Changes the permissions of <file> to <permissions>. To change the permissions of an entire directory structure, use -R. To change the permissions of a file, you must be the owner of that file.
Permissions can be given in various forms. Octal permissions are generally the easiest to use, though they may take some getting used to. Each permission is assigned a value, and these values are added up to specify the entire permission set. Each digit reprents a "set" of users. User is the first digit. This would be the person who actually owns the file. Group is the second digit. This includes anyone that is in the "group" the file has listed. The third digit is world. This means everyone and anyone.
The values are as follows:
user read: 400
user write: 200
user execute: 100
group read: 40
group write: 20
group execute: 10
all read: 4
all write: 2
all execute: 1
For example, to allow everyone to read and write a file, use permission 666. Most shared executables (such as those in the /bin directory) have permissions 755, which allows the owner to read, write, and execute, but others can only read and execute it. Permissions for non-executable, non-shared files are usually 644. A directory must always allow execute permission if you want to cd into it.
The second way permissions are specified is in the <category>+/-<permission> form. The categories are u (user), g (group), o (users not in the group), and a (all, including the user and group of the file). The permissions are r (read), w (write), and x (execute). For example, to give all users execute permission to a file, use a+x for <permissions>. To remove write access to a file for all but the owner of the file, use o-w.
echo <options> <string>
Prints <string>. No options, really.
More Advanced bash Usage
bash is a very powerful shell with many features. For example, the wildcard *, if used by itself, denotes all the files in the current directory. The command
rm *
will remove all the files in the current directory. Note that rm never 'sees' the *; bash expands the * to all the files in the directory and then passes it on to rm. The wildcard *, when not by itself (e.g. foo*) denotes all the files having names containing anything in place of *. For example,
rm *.doc
will remove all those pesky Microsoft Word documents.
bash also allows the user to set variables by typing
<variable>=<value>
where <value> can be any string. This is where the echo command comes in handy; echo $<variable> will show the value of that variable. There are many reserved variables, which are used by bash to set aspects of the shell, such as the PATH variable described above. The shell interprets $<variable> as the value of the variable. For example, to add /usr/sbin to the path:
PATH=$PATH:/usr/sbin
This command sets PATH to the old value of PATH with :/usr/sbin added to the end.
The default bash prompt is set by the PS1 variable. Special symbols, usually a letter preceded by \, are used to denote certain aspects of the shell, the user, or the hostname. For example, \u denotes the username, \h denotes the hostname, and \w denotes the working directory. The PS1 value that would generate a prompt resembling
[smith@compy /usr/bin ]$
would be
[\u@\h \w ]\$
Note that if you try to set it with
PS1=[\u@\h \w ]\$
it would not work, because certain characters have a special meaning to the shell, such as $ and [. To get around this, put quotes around the PS1 string, like so:
PS1="[\u@\h \w ]\$ "
Alias is a powerful command. It allows you to set "defaults" to some commands.
alias <command>='<command with options>'
So for rm, most distributions default to
rm -i
which means interactive. Too many people have accidently deleted the root of the drive without this set. To set the interactive flag, you would have typed
alias rm='rm -i'

