Quantcast
Channel: Webminal - Latest topics
Viewing all articles
Browse latest Browse all 484

Linux Command-line Journey Day-1

$
0
0

@laks wrote:

This tutorial originally written for Online Linux Terminal 'www.webminal.org' - So better open
your terminal and read on!

  • Know your location
  • Where Am I ?
  • What's a command
  • How to create a new directory?
  • Analyzing ls command output
  • Move into a directory
  • What's . and .. ?
  • Inode numbers
  • Absoulte and Relative path names

Know your location:

Assume, your user name is "efg".
When you first login to a linux system, it will display "Last login: " Time and
date and content of the file /etc/motd (this file is owned by the root user) and
then it will greet you with a bash prompt, typically you will get something like -


[efg@fedori ~]$

"fedori" here refers the hostname. "$" denotes the bash shell is ready to accept your commands.

Where Am I ?

Let's understand how you landed in this location. By default, when you login, you
will be redirected to a directory specified in the /etc/passwd file. This entry is
created when the system-admin created an account for you and all /etc/ are
configuration files. The /etc/passwd file has an entry for you, like -


efg:x:500:500:EFGname:/home/efg:/bin/bash

Let's split them to understand it-


efg - login name
x - Password (place holder in modern system, user password will be stored in another
encrypted file /etc/shadow)
500 - unique user id
500 - groub id
EFGname - real name
/home/efg - home directory
/bin/bash - shell type

The summary of this entry would be - after verifying password for user "efg" in the /etc/shadow file,
allow user "efg" to login and place him under "/home/efg" with bash as the shell type.

You can get your current (working) directory using the command "pwd" (pRINT wORKING dIRECTORY).

What's a command

First of all, what is a command? It's a binary (also known as executable) file
kept under a specific location. Guess where? Near the washing machine? No ....
near the TV? ..no again.
Okay. Here is a hint, you can use another command called "whereis" to find the
location of the command. Lets try it first:


[efg@fedori ~]$ whereis pwd
pwd: /bin/pwd

pwd binary can be found under the /bin directory. Wait a second, before running the pwd command.
There is more than one location where the binary files are kept.
You can view then using:

[efg@fedori ~]$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/efg/bin

When you type something on bash prompt,

[efg@fedori ~]$something

Bash assumes you entered a command called "something" and check it first on the
current directory and then paths specified by the PATH environmental variable
-so first it goes to "/usr/local/bin" and finds there is no file called "something",
then it checks /usr/bin and then /bin, etc.If it didn't find any file name it'll
display the "command not found" error message as shown below-
`
[efg@fedori ~]$ something
bash: something: command not found

[efg@fedori ~]$ pwd
/home/efg

`
So you are in the right place, at your home directory as expected by your /etc/passwd entry.
How to "clear" above errors messages from the screen? ..there is command called ..
you figure this out yourself, the question itself has the "answer". :smile:

How to create a new directory?

You are at your home. It's nothing but a directory you can create your own
directory using the command,

[efg@fedori ~]$touch dir0

If no error message is displayed after executing this command,then your directory is created.

Then in order to check the presence of the directory, use the "ls" command.

[efg@fedori ~]$ls
dir0

It should display your newly created directory.

Another way to create a directory is by using "mkdir" (mAkE dirECTORY)

[efg@fedori ~]$mkdir dir1

If no message is displayed after executing this mkdir command {it means it went without error}.
Then in order to list directory contents, use the "ls" command.

[efg@fedori ~]$ls
dir0 dir1

displays your newly created directories.

Analyzing ls command output

ls - command will display directory contents. If you want to know about it's permission,use "ls -l"

[efg@fedori ~]$ls -l
-rw-rw-r-- 1 efg efg 0 2010-08-13 08:13 dir0
drwxrwxr-x 2 efg efg 4096 2010-08-13 08:15 dir1

Let's understand this output first, see the second line, can you see 4096, yes? yes? Okay,
then ignore it for now :smiley: on its right side, you will see "date and time and directory name".
-date/time here refers creation time for this entry.And on its left side, you have four entries separated
by white fedori(blank fedori).drwxrwxr-x 2 efg efg
efg efg says user named efg who belongs to the group called efg is owner of this directory and group
member can also use this directory.

So what's is a group? We will talk about this while dealing with root user commands. For now just think of it
as your friends.

Lets check the first entry -

drwxrwxr-x

It denotes the permissions for this directory. In GNU-Linux,
the first "d" letter mention its a directory.
In general,rwx stands for read, write and execute.
then we have rwx repeated twice.
First three letters, (rwx) always denotes owner and second three (rwx) always refers
to group or friends and the final three,(r-x) always refers to all others or unknown
people who uses this machine. "-" here denotes,others don't have write permission,
they can only view/search this directory.

Now let's examine the first line,

-rw-rw-r-- 1 efg efg 0 2010-08-13 08:13 dir0

Yes, I can hear you screaming, "It doesn't have 'd' letter at it's begining? What happened?"
Remember the command used to create this one, "touch" - I lied about it :)- touch
creates an empty file not a directory. So there is only one way to create directory,
that is to use "mkdir" command.

A touching note: -if the file doesn't exists, the touch command will create an empty file,
if file already exists, it updates its timestamp.

Okay,back to exploration-

Since "dir0" is regular file - it don't have a "d" at beginning, instead uses "-". Only special
files are denoted by first entry (a directory is also a special file).
rw-, says owner "efg" can read and write into this file and he can't execute it. Similarly next three
fields "rw-",users belong to group "efg" can read and write.Final three entries "r--" says other can read/view

this file content ,but they can't modify or execute it.

since touch creates an empty file 0 - denotes its size. and date and time of creation along with file name is
displayed on the rest of the line.

Now we have two entries on our home current directory and lets visualize them.-Close your eyes and think
about it! ..wait don't sleep :smile:

/home/efg/ (our working directory.)
|
|-------------dir0
|-------------dir1/

how to move "into" newly created directory? Can you guess the command for it? If you think "into" as the answe
r,
congrats!!!, you are dead wrong :smile:

Move into a directory

use "cd" command,which stands for "cHANGE dIRECTORY",to move into a specific directory.

Before you use cd command on directory, what will happen, if you use it with our file
dir0? Let's try that first -

[efg@fedori ~]$ cd dir0
bash: cd: dir0: Not a directory

"cd" is wise, it checks "dir0" and realizes it don't have "d" on it's beginning and says
this error message. Good.Now move on,

[efg@fedori ~]$cd dir1

We moved into a whole new directory. We can verify this by using "pwd" command and list the directories
content using ls and ls -l . Enjoy.

Oh oops ..there is nothing! Try it again but this time pass 'a' as an option for ls command
"ls -la" . a surprise awaits you!

[efg@fedori dir1]$ ls -al
total 8
drwxrwxr-x 2 efg efg 4096 2010-08-13 08:15 .
drwx------ 5 efg efg 4096 2010-08-13 08:15 ..

What's . and .. ?
================
Whats this "." dot and ".." dot dot refers here? Let's wear the hat of Sherlock Holmes and
investigate this case as "the Curious case of . and .. " Two striking things are both entries
has "d" in front and same time stamp.

Check the timestamp of "dir1" when it's created using mkdir. Got it? both are same,so these
two entries "." and ".." are created when mkdir command was used. What they actually refer to?
Here comes the ls command to the rescue again, now pass "i" option to it.

[efg@fedori dir1]$ ls -lia
total 8
1966463 drwxrwxr-x 2 efg efg 4096 2010-08-13 08:15 .
1966452 drwx------ 5 efg efg 4096 2010-08-13 08:15 ..

Inode numbers
=============
Numbers in front are called "Inode" numbers. These numbers are actually used by linux kernel.
Each file has a unique inode number.We use filename or directory names but kernel uses only these numbers.

Repeat the same procedure for the /home/efg directory, you can use ls command and pass
the directory name as argument:

[efg@fedori dir1]# ls -lia /home/efg
1966452 drwx------ 5 efg efg 4096 2010-08-13 21:22 .
172034 drwxr-xr-x 13 root root 4096 2010-08-13 08:56 ..
1966461 -rw------- 1 efg efg 109 2010-08-13 08:10 .bash_history
1966457 -rw-r--r-- 1 efg efg 18 2010-08-13 08:10 .bash_logout
1966455 -rw-r--r-- 1 efg efg 176 2010-08-13 08:10 .bash_profile
1966453 -rw-r--r-- 1 efg efg 124 2010-08-13 08:10 .bashrc
1966460 -rw-rw-r-- 1 efg efg 0 2010-08-13 08:13 dir0
1966463 drwxrwxr-x 2 efg efg 4096 2010-08-13 08:15 dir1

More Surprises ! Who created all those files that begin with a dot?. Our investigation takes a new turn,
looks like this case will take sometime to solve. -Don't worry! Its not a judical enqury, so we will have verd
ict soon :smile:

Let's focus on how "." and ".." files created under dir1 first and we can check others file, a little later.


1966463 drwxrwxr-x 2 efg efg 4096 2010-08-13 08:15 dir1

this entries inode number is same as "." file under "dir1". So the file /home/efg/dir1/. denotes the directory
itself.And Just look above under "ls -lia /home/efg" here we have inode number same as /home/efg/dir1/.. - so initia
l verdict is,all directories have two files named "." and ".." where "." refers to that directory itself and ".." refers to
its parent directory.

Absolute and Relative path names

These ('.' and '..') are used for relative path names, so far we have used absolute path names.
Absolute path names begin with / (root) example : /home/efg/dir1
Relative path names begin with "." or ".." or without "." or ".." or "/"
Check your current path - using pwd command it will tell its "/home/efg/dir1"
you can access dir0 file using relative path like,


[efg@fedori dir1]$ls -l ../dir0

You can access files under current directory using "."

since we don't have any files here,first create a file using touch

[efg@fedori dir1]$touch giis.txt

and access it using

[efg@fedori dir1]$ls -l ./giis.txt

here is the final relative path method,which won't use . or .. or /

simply call,

[efg@fedori dir1]$ls -l giis.txt

This marks end of Day-1. Now feel free to learn Day-2 today itself or go to bed or go back to windows :smile:

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 484

Trending Articles