Sei sulla pagina 1di 5

13 08 2008 file:///floydb/programming/50webs_tux/pdf.

html #1
Tutorial
An Introduction to Linux CLI
By Craciun Dan <floydian.embryo@yahoo.com>
Aug 10, 2008

1. Basic Commands and Concepts


1.1. The shell
A shell is a command interpreter which allows the user to interact with the computer. The way things work is simple: you type in
commands, the shell interprets them and performs the tasks you asked it to do, and finally sends the results to the standard
output (usually the screen). For example, the output of the ls command may be something like:

$ ls
bin cdrom etc floydb initrd lib lost+found mnt proc sbin srv tmp var
boot dev floyda home initrd.img lib64 media opt root selinux sys usr vmlinuz
This is the list of files inside the root directory. The root directory is the first location in the filesystem hierarchy, and it is
represented by a slash sign: /.

Some of the most popular shells are:

bash Bourne6Again Shell, this is the default shell on most Linux systems
sh Bourne Shell, this one is not so widely used anymore and it has been replaced by Bash
csh C Shell, has a syntax which resembles the C syntax
tcsh an improved version of the C shell
ksh Korn Shell
dash Debian Almquist Shell
The default shell in Debian is Bash, which is a modern implementation of the older Bourne Shell (sh). Bash was developed by the
GNU Project, which is also responsible for most of the basic commands widely used on a Linux system. On a Debian system, the
default shell used by the system is specified in the /etc/passwd file. A default Debian installation provides Bash and tcsh as
available shells.

1.2. Basic commands


Commands in UNIX are executable files which can be run and perform tasks, like listing the files in a directory. Examples of
frequently used commands are ls, pwd, date , cat and so on. The general form of UNIX commands is:

command option(s) filename(s)


A command may or may not have arguments. An argument can be an option or a filename.

Here is a list of basic commands:

ls list directory contents


pwd print name of current/working directory
date print or set the system date and time
mkdir make directories
rm remove files or directories
mv move (rename) files
who show who is logged on
whoami print effective userid
cat concatenate files and print on the standard output
more file perusal filter for crt viewing
less opposite of more
Use the cat command to read small text files. For larger files which are longer than one page, use the less or more commands, or
a text editor. For example, to read the /etc/motd (Message of the Day) file, type:

$ less /etc/motd
13 08 2008 file:///floydb/programming/50webs_tux/pdf.html #2
Omit the dollar ($) sign, which represents the prompt. A file with some text message in it should be now displayed on the screen.
To navigate in a text file using less, use the following shortcuts:

^N to go forward one line (same effect as J)


^P to go backward one line (same effect as K)
^V to go forward one page (same effect as SPACE)
ALT+V to go backward one page
To quit and return to the prompt, type q. In the examples above, the ^ stands for the CONTROL character (labelled Ctrl), so ^N
means 'press Ctrl+N'. The above shortcuts are widely used on a Linux OS (Operating System) by programs like Emacs (an
advanced text editor), Nano (a simple and user6friendly text editor), the man command and many other.

The man command


Use man <command> to see detailed help about a command. You can use the keyboard shortcuts mentioned above to navigate
through a manual page.

Type q to quit the manual page and return to the shell prompt. The q character is usually used to quit in most Linux programs. q
will quit in more , less, man and many others. If you type h while reading a manual page, a help about the less command will be
provided. Those are the commands that apply in the manual page.

The cd and pwd commands


The command cd, which is used to change the current directory, and pwd, used to print the current working directory, are both
shell built6ins. This means they are included in the Bash program rather than a stand6alone command (file), like ls or date . Some
examples of shell built6ins are cd, echo , read, exit, logout, fg. A complete list is available under the 'Shell built6ins' chapter.

You can see what cd does by changing the current working directory to a new one and see the new path using the pwd command:

$ pwd
/home/christmas
$ cd /usr/share
$ pwd
/usr/share
As a note, cd without any arguments changes the current directory to the home directory of the current user:

$ pwd
/usr/share
$ cd
$ pwd
/home/christmas

Control characters

The shell uses certain key combinations to perform certain tasks. These key combinations are called control characters. Examples
of control characters:

^A go to the start of line


^E go to the end of line
^H erase one character to the left (same effect as BACKSPACE)
^D erase one character to the right; it also logs you out if there is nothing to delete
^U erase entire line
^K erase everything to the right until the end of line
^P bring the previous command in history
^N bring the next command in history
^C interrupt character; this usually terminates programs
For example, if you type ls, then date and pwd, if you type now ^P the 'pwd' command will be brought back and you only have to
type ENTER to issue it again. If you type ^P again, the output of date is brought and so on.

As a side note, the arrow keys can also be used for navigating through past commands, but most of the people recommend the
Emacs6like (above list) keyboard shortcuts due to the fact that once you get used to them they'll prove faster than the arrow keys
(you won't need to move your fingers and whole hand from the typing position to reach the arrows).

The ls command
The 'ls' command is used to list directory contents. With no arguments, it returns a list of files and directories in the current
directory. It also offers many arguments, so you can list more information about the existing files:
ls
l use a long listing format
ls
X sort alphabetically by extension
13 08 2008 file:///floydb/programming/50webs_tux/pdf.html #3
ls
h display sizes in human readable format (transforms bytes in KB, MB or GB when necessary); use this with '6l' to see the effect
ls
a list all files; this includes hidden files (files starting with a dot character)
You can nest all the arguments to obtain the desired result:

$ ls lhX
total 108K
drwxrxrx 2 root root 4.0K 20080213 07:27 bin
drwxrxrx 3 root root 4.0K 20080213 07:28 boot
lrwxrwxrwx 1 root root 11 20071223 13:52 cdrom > media/cdrom
drwxrxrx 13 root root 3.7K 20080316 13:08 dev
drwxrxrx 106 root root 12K 20080316 12:32 etc
...
See 'man ls' for a complete list of arguments.

The . and .. files

When using 'ls 6a', you will notice two special files, '.' and '..'. These are virtual files. '.' means the current working directory, and '..'
represents the parent directory of the current working directory. For example if your current directory is /home/chris and you want
to change it to its parent /home, use 'cd ..'. Or if you want to copy, say, file /etc/motd in the current directory use:

$ cp /etc/motd .
The rm, mkdir and touch commands
To remove a file, use:

$ rm file
To remove a directory, use:

$ rm r directory
The 'mkdir' command is used to create directories:

$ mkdir mydir
'touch' is an utility used to change file timestamps. That means it updates the date a file was last accessed and/or modified.
Example:

$ ls lhX print.pdf
rwrr 1 christmas christmas 82K 20080319 01:11 print.pdf
This file was last modified on March 19, 2008, at 01:11. To update its modification time, type:

$ touch print.pdf
$ ls lhX print.pdf
rwrr 1 christmas christmas 82K 20080320 12:48 print.pdf
Now, the date the file was last modified was changed to March 20, 2008, 12:48. If the file specified as argument for 'touch' does
not exist, an empty file is created:

$ ls myfile
ls: myfile: No such file or directory
$ touch myfile
$ ls myfile
myfile
Getting help

One of the most powerful way to get help is to read the manual pages, usually available for any GNU utility and program via the
'man' command. These programs also have two standard arguments, '66version' (or '6v') and '66help' (or '6h'). They usually return the
version of the program and a brief help on how to use the command. You can also use 'info <command>'.

On a Debian system, the manual pages are located in the /usr/share/man directory, under directories with names like man1,
man2, man3 and so on.

$ bash version
GNU bash, version 3.1.17(1)release (i486pclinuxgnu)
Copyright (C) 2005 Free Software Foundation, Inc.
13 08 2008 file:///floydb/programming/50webs_tux/pdf.html #4
1.3. Shell built
ins
Shell built6ins are commands included in the Bash program. Here is the complete list of shell built6ins for Bash 3.1.17:

. compgen exit let set ulimit


: complete export local shift umask
[ continue fc logout shopt unalias
alias declare fg popd source unset
bg dirs getopts printf suspend wait
bind disown hash pushd test
break echo help pwd times
builtin enable history read trap
cd eval jobs readonly type
command exec kill return typeset
To see if a command is a stand6alone program or a shell built6in, use the 'type' command:

$ type cd
cd is a shell builtin
$ type bash
bash is hashed (/bin/bash)
If the specified command is an alias, the output will be something like:

$ type ls
ls is aliased to `ls color=auto'
You can use the 'help' built6in to see a list of Bash commands, and 'help <command>' to see detailed help about each command.

The following describes several shell built6ins:

cd change current/working directory


help display helpful information about built6in commands
echo output the arguments given to it
pwd print the current working directory
bg place each job specified as argument in the background
fg place the job specified as argument in the foreground and make it the current task
1.4. Linux directory structure
Usually, the standard Linux directory structure is as follows:

/bin contains the essential programs for booting and maintaining the system; some examples would be: bash, chmod, chown, cp,
grep, kill, ps, rm, tar
/boot contains the Linux kernel image used to boot the system
/dev special files pointing to system devices
/etc configuration files for various programs
/home contains directories for all users the system has; examples: /home/chris, /home/corrina, /home/pink
/lib contains shared libraries available for all programs throughout the system
/media mount points for devices like hard disks, CD6ROMs/CD6RWs, DVD6ROMs/DVD6RWs
/mnt used for mount points like external filesystems for example
/opt additional programs go here
/proc contains virtual files which provide information about the system; try 'cat /proc/cpu' for example
/root home for the super user (root)
/tmp temporary files are stored here
/sbin programs for system administration, generally used by root (super user)
/usr contains almost all programs which are not located in /bin or /sbin, documentation, manual pages
/var files like logs, mails
1.5. More Linux commands
The command 'who' is used to show who is logged on, the number of the tty (teletype terminal) or display they are using, and the
date and time they logged on:

$ who
christmas tty1 20080320 14:47
christmas :0 20080316 13:08
13 08 2008 file:///floydb/programming/50webs_tux/pdf.html #5

Resources
Finally, I'd like to point out two great sites specially intended for beginners, which document the use of command line in Linux very
well and detailed:

TuxFiles.org 6 Tutorials for Linux newbies


LinuxCommand.org 6 An introduction to Linux command line and basic scripting for beginners

Updated: Aug 12, 2008 (Created: Aug 10, 2008, v0.2)

Copyright (C) 2008 Craciun Dan under the terms of the Creative Commons Attribution6ShareAlike 3.0 License.

Potrebbero piacerti anche