Tuesday, December 23, 2014

Linux Filesystem

Navigating Linux Filesystem


The Linux filesystem is a tree-like hierarchy of files and directories. At the base of this filesystem, is the "/" directory, known as "root"(Don't confuse with root user). Unlike DOS and Windows, where there is separate "/" for every disk drive, Linux filesystem mounts all disks somewhere under / filesystem. For example: /dev/sda , /dev/sdb etc. Everything in Linux is a file i.e a device attached to your Linux machine is shown as file in filesystem.


Directory Layout

 / : The nameless base of filesystem. All other files, directories, drives, and devices are attached to this root.
/bin : Essential command binaries stored here.(ls, cd, tar etc)
/boot : Boot loader files.(Files needed for booting of system)
/dev : Device files. In Linux, all hardware devices are accessed like other files and are kept under this dorectory
/etc : Machine specific configuration files. (/etc/hosts.conf)
/home : Users' personal home directory.
/lib : Important shared libraries and kernel modules.
/proc : Process information.
/root : root(superuser) home direcotory.
/sbin : Essential system binaries.(fdisk, fsck, init etc.)
/tmp : Temporary directory. Each user has rights to put their files to this location.
/usr : Base directory for most shareable and read-only data. For example program files of an application.
/var : Variable data-- Mail and printer spool data, log files, lock files etc.


Commands for Navigating Linux Filesystem

The first thing you do while learning about the Linux filesystem is to look around!! What is all around? 

Next few commands will tell: 1. Where you are? 2. Take you to somewhere else. 3. Whats there at current place?

pwd : print working directory.[In Linux prompt, it does not show the complete/absolute path of your current location but it shows the name of current directory. Hence pwd command helps in knowing absolute path of your current location ].


cd: For changing your current working directory to some other directory. If used only 'cd', without argument, it will take you to your home directory.


cd .. will take you one level up i.e. to parent directory.

cd - will take you to your previous location, before changing your location.

ls will List all your files/directory in current directory.(more than one file per line)

ls -l will display one filename/directory per line including various details.(refer man page).


ls -a will display all files/directories including hidden files.

ls -ld /dir will display detailed info. about /dir directory rather contents of /dir directory. Try and compare output of ls -l /usr/bin & ls -ld /usr/bin.


Piping and Redirection

Before we start learning more commands, lets have a look in to Piping and Redirection. In Linux, user has to combine different small commands and make more useful command sequences using those commands/programs.

Piping Commands

The Pipe character, "|" is used to combine two or more commands. The output of first command is "piped" in to another command and works as input to that command. If there is another 2nd pipe then output is sent to 3rd command.

For Example: ls -al | more 

Here we are piping the output of ls -al in to more command. As output of ls -al is very long list hence we used more to display one screen at a time using more command.

Redirecting Output to Files

At times we have to save the output of a command rather displaying it on screen.

For example: we want to create a list of text(.txt) files in current directory. So we can do this using ">" redirection character.

">" operator will overwrite the file if exist and create newfile if given file does not exist.

">>" operator can be used if you want to append the output i.e you do not wish to overwrite file.

We shall have look in to more filesystem navigation commands in later topics. Till then keep learning LinuxWithEase. Smile :)

Saturday, December 20, 2014

Unix Bash Shell and Executing Commands

Shell is a program that interprets commands. Shell is not an operating System(OS) but a way to interface with operating system to execute commands. It allows user to execute commands manually from terminal.

 Fig 3.1 Bash Shell Prompt

What is Bourne Again SHell(BASH)?

Bash is free replacement shell to Bourne shell which was originally written by Steve Bourne. Since it is free shell hence it has been adopted as default shell on most of the Unix/Linux systems.

How Bash is Different From DOS Command Prompt?


  • Case Sensitive:  In Unix/Linux, commands and filenames are case sensitive i.e. 'EXIT' and 'exit' are different unlike DOS where either of the two will work as command but in Linux 'EXIT' will throw an error on execution.
  • "\" and "/":  In Unix/Linux '/' is directory separator which is '\' in DOS.
  • Filename: DOS follows "EIGHT DOT THREE" filename convention that means 8 character long filename, then dot(.) followed by 3 charaters' file extension e.g. FILEMAME.TXT. In Unix/Linux there is no file extension. Periods can be placed as a part of filename.

Special Characters



Before learning commands it is important to know about some special characters that shell interprets in different ways.

'\' : If you want to reference special character you need to "ESCAPE" it using back-slash('\'). Example: Filename\* (For it to be interpreted by shell as Filename*).

'/': Used to separate directory hierarchy. /home/user1/data

'.':  Dot. Indicates current directory. Can also hide a file if it is the first character in a filename.

'..': Dot Dot. Indicates parent directory.

'~': Tilde. User's home directory.

* : Represents 0 or more characters in the filename. file* can represent file, file1, filexyz, file100 etc.

? : Represents exact single character. File?.txt can represent File1.txt, Filex.txt but not File.txt or File22.txt.

[ ]: Can be used to represent a range of values, e.g 0-9, A-Z, etc. Example: hello[0-2].txt represents hello1.txt, hello1.txt and hello2.txt

| : Pipe. Redirects output of one command to another. ls|cat

>: Redirects output of a command to a file. If a file already exist then overwrite it.
Example: ls > file1.txt

>>: Redirects output of a command to end of file.

; : Semi colon. Command separator.

&& : Command separator but second command will run only if first command runs successfully.
Example: cd /var/logs && cat log1.txt

&: Execute command in background. Output does not flash on screen. Example: ls &

Executing Commands 

Most of commands that we run, are located in Shell's "PATH". You just type the name of program rather giving full path of the program. "PATH" variable in your shell helps to run command without giving full path. This variable includes most common program locations. Location are separated 
by :(colon).

Value of PATH variable can be checked by executing following command.

$PATH

Fig 3.2
If you want to run a command/program that is not included in PATH variable you will have to give full path to the program to execute it.

Command Syntax


Commands can be run by giving command name only or arguments can be passed along command.

Example: command [-argument1] [-argument2][-argument3argument4] [--argument5] [Filename]

$ls
$ls -l -r -t
$ls -lrt
$ls -lrt --color
$cat -n filename

Note: '$' is prompt in above examples.

Getting Help

When you are struck or need help about any Linux command, you are just some keystrokes away!!

built-in help

Many commands have built in help screens that can be invoked using -h or --help flags along with command.

Fig 3.3

Online Manual Pages

Many commands come with manual pages with them. You can explore using 'man' command followed by command name.

Fig 3.4 Man Page
Sometimes you might not remember the name of the command you want to search about, then you can use -k option of man command. It searches the supplied "word" in argument in DESCRIPTION part of man page.

For example, man -k permission will display man pages having "permission" word in its DESCRIPTION field.

Fig 3.5


In our next topic we shall cover basic commands and will explore Linux Filesystem. Till then keep LearnLinuxWithEase and have nice time. Smile :)

Your comments and suggestion are most welcome.

Monday, December 15, 2014

What is Unix Shell

What is Unix Shell?

Unix shell is a program which works as intermediary between user input command and operating system. Shell interprets the user given command and direct the operating system what to do?

There are various type of shells. For a normal user they offer same kind of functionality but they have different syntax and capabilities.

Most shells are either of type Bourne Shell (Launched with Unix Version 7) or C shell (Launched with BSD). Most Unix machines have Bourne shell(sh) and C Shell (csh) installed.

Bourne Shell (Default prompt '$') can be further categorized as follows.
1. Bourne Shell(sh)
2. Korn Shell(ksh)
3. Bourne Again Shell(bash)
4. POSIX(sh)

The Different type of C shell as follows.
1. C Shell (csh)
2. TENEX/TOPS C Shell(tcsh)


If you want to know which shells are available for you in your Unix machine then you cat /etc/shells file. These shells are available for default login shell for any user.
$ cat /etc/shells

Fig 2.1


 For Unix, Shell is like another program. Hence any program that is part of /etc/shells can be designated as a login shell of a user.

It was basic about shell. We shall delve more in to it in coming posts.

Till then keep exploring and Smile :)


Sunday, December 14, 2014

Unix History

Linux Operating System history starts from Bell labs in 1957. They were needed an operating system to run batch jobs. They created BESYS operating system to deal with it. This was replaced by Multics(Multiplexed Information and Computing Services).

Later in 1969, Dennis Ritchie and Ken Thompson had rewrite an operating system, that led to UNICS (UNiplexed Inforamtion and Computing Services) ----- an 'emasculated Multics'.

In summer of 1969 Unix was developed.

First edition of Unix was released in 1971. It includes 60 commands like cat, chdir, chmod, chown, mv, ls, cp etc. But it was lacking piping.

In this duration, Dennis Ritchie rewrote B and  created new language C.

And Unix was written in C.

In October 1973, Unix released publicly in a conference and installed at 16 sites.

In next two decades various distribution of Unix launched like BSD, SCO, IRIX, System V, HP-UX, IBM-AIX, Sun Solaris etc.

In 1991, Linux was introduced by Linus Torwalds, as a student in Finland.

Later various flavors of Linux came in market i.e RedHat, Fedora, Ubuntu etc.


In my next post I will summarize the various shell version and their incorporation with Unix.

till then have a nice time and learn LinuxWithEase. smile :)