An Introduction to Linux CLI

Posted by Chris7mas on Aug 10, 2008 6:45 PM EDT
TuxArea; By Craciun Dan
Mail this story
Print this story

A tutorial intended for beginners explaining how Linux CLI works and the basic concepts and commands of Bash.

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.

Some of the most popular shells are:

bash Bourne-Again 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

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

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 user-friendly text editor), the man command and many other.

The man command

Use man 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 built-ins. This means they are included in the Bash program rather than a stand-alone command (file), like ls or date. Some examples of shell built-ins are cd, echo, read, exit, logout, fg. A complete list is available under the 'Shell built-ins' 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.

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
ls -h display sizes in human readable format (transforms bytes in KB, MB or GB when necessary); use this
with '-l' 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
drwxr-xr-x 2 root root 4.0K 2008-02-13 07:27 bin
drwxr-xr-x 3 root root 4.0K 2008-02-13 07:28 boot
lrwxrwxrwx 1 root root 11 2007-12-23 13:52 cdrom -> media/cdrom
drwxr-xr-x 13 root root 3.7K 2008-03-16 13:08 dev
drwxr-xr-x 106 root root 12K 2008-03-16 12:32 etc
...

See 'man ls' for a complete list of arguments.

The . and .. files

When using 'ls -a', 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
-rw-r--r-- 1 christmas christmas 82K 2008-03-19 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
-rw-r--r-- 1 christmas christmas 82K 2008-03-20 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, '--version' (or '-v') and '--help' (or '-h'). They usually return the version of the program and a brief help on how to use the command. You can also use 'info '.

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 (i486-pc-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.

1.3. Shell built-ins

Shell built-ins are commands included in the Bash program. Here is the complete list of shell built-ins 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
built-in enable history read trap
cd eval jobs readonly type
command exec kill return typeset

To see if a command is a stand-alone program or a shell built-in, use the 'type' command:

$ type cd
cd is a shell built-in
$ 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' built-in to see a list of Bash commands, and 'help ' to see detailed help about each command.

The following describes several shell built-ins:

cd change current/working directory
help display helpful information about built-in 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 used for mount points
/mnt mount points for devices like hard disks, CD-ROMs/CD-RWs, DVD-ROMs/DVD-RWs
/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 2008-03-20 14:47

Full Story

  Nav
» Read more about: Story Type: Tutorial; Groups: Linux

« Return to the newswire homepage

Subject Topic Starter Replies Views Last Post
very excellent tuxchick 4 1,368 Aug 11, 2008 9:49 PM

You cannot post until you login.