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

Linux Command-line Journey Day-2

$
0
0

@laks wrote:

We will continue Day-2 of our Linux Command-line journey.

  • Understanding and manipulating file contents:
  • Understanding cat command
  • Input/Output Redirection
  • Head and tail of file

Understanding and manipulating file contents:

Okay, now lets understand the dot "." files under /home/efg namely ".bash_history" ,
".bash_logout" , ".bash_profile" , ".bashrc".

Go back to /home/efg using command

[efg@fedori dir1]$cd ..

Did you understand what above command really mean? ...what? NO? then re-read topic
"Move into a directory".Then Came back ..I'll count till 10000 and wait for you.
10000,9999,9998...3,2,1,0. okay.Now lets first understand ,.bash_history ,file is used
to keep track of shell commands. You can view the content of file using "cat" command.

[efg@fedori ~]$cat .bash_history

will display commands and everything you have typed in shell prompt. "history" command used
to read this file and display it on screen.

[efg@fedori ~]$history

It can used by root user to keep track of what you are doing.. I can hear you "aaawk!! how to remove
this feature? Can I delete this file?" yes you can delete this file using the "rm" command.

[efg@fedori ~]$rm .bash_history

This will prompt you for confirmation. Since any deleted files can't be recovered, unless you have
fail-safe tools or hoping that FS didn't remove its final references from the journal log.

But the sad news is, this bash_history will be created automatically whenever you type something into
bash prompt. But this file is useful, we will see that in next section.

".bash_profile" , ".bashrc" files have environmental variable specific to this user. Say for example,
you want to change bash prompt


[efg@fedori ~]$

to it look like root# - so that you can fool your friend, saying you got root access. So edit your
.bash_profile and add following line -


export PS1="root#"

How to append this entry to file .bash_profile? Read on...

Understanding cat command:

we have used "cat" command view the content of .bash_history earlier.Lets understand some basic terms of GNU-L
inux.

your kepboard (or anyother input device) is referred as STDIN - standard input device,
your screen (or anyother output device) is referred as STDOUT - standard output device,
finally normally screen is also referred as STDERR- standard error device.

by default cat reads from stdin and writes into stdout device. (ie) it reads from your keyboard and write into
screen.

To verify this simply type cat,


[efg@fedori ~]$cat

now type something as input (cat is reading from keyboard) and press "Enter" key,it will display whatever you
typed on the screen itself,which is your
default stdout.

Did you now realize what's stdio and stdout? so now type something like junk word m5do5(btw, to come out of "c
at" command press "ctrl" and "d" simulateously -which
tells the cat command - end of input,take me back to bash prompt) ...were where we? at some junk m5do5 :smile:


[efg@fedori ~]$ m5do5
bash: m5do5: command not found

Error message is displayed on default stderr ,which is your screen.

So when you said,"cat .bash_history" ,you are telling cat command ,"hey cat,this time ,treat given file as std
in".
and cat did the same,it read your file content and displayed on stdout.

Input/Output Redirection

you can also override stdout,and ask "cat,please treat file1 as my input device and redirect the output to fil
e2 instead of default stdout,which is screen."

">" is called redirection operator for output stream.


[efg@fedori ~]$cat .bash_history > history_file.txt

">" refers to append the content to existing file.

So to append the environment variable,

[efg@fedori ~]$echo "PS1=\"root#\"" >> .bash_profile

above command will set the enviromental files. echo command by default,displays output in stdout,we told echo
to redirect it to file.
\ is used here as an escape charater.we will see about escapte character,little later.

Check your file .bash_profile ,the last line would be PS1="root#" using cat command,but wait there is two othe
r common method to view content of file.

head & tail of file


[efg@fedori ~]$head .bash_history

head command will display ,first 10 lines of a file ,if you want to change number of line ,pass and option "-4
" .

[efg@fedori ~]$head -4 .bash_history

will display first four lines from the file.

tail,You guessed it correctly, is exact opposite of head.it will display last 10 lines and you can manipulate
it by passing the number,


[efg@fedori ~]$tail -1 .bash_history
PS1="root#"

Now logout and re-login again

[efg@fedori ~]$logout

Now provide your username & passwd. Eureca ! you will see this-
*
root#
*
".bash_logout" can used to have commands that ends to be executed when you log off. How about putting the comm
and to delete .bash_history in .bash_logout?

wait,.bash_history or history command is indeed useful one.

When you run history command ,it will display commands along with numbers on its right. If you want run one of
those commands again,you can simply using

[efg@fedori ~]$!1

will the run command show by:

[efg@fedori ~]$head 1 .bash_history

That's it for today. Its a short day. see you on Day-3

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 484

Trending Articles