Tut Cli
Tut Cli
html #1
Tutorial
An Introduction to Linux CLI
By Craciun Dan <[email protected]>
Aug 10, 2008
$ 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: /.
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.
$ 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:
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.
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:
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.
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:
$ 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.
/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:
Copyright (C) 2008 Craciun Dan under the terms of the Creative Commons Attribution6ShareAlike 3.0 License.