Wednesday, January 13, 2016

Linux interesting commands which might be helpful in creating shell script.


Restoring deleted /tmp folder

As my journey continues with Linux and Unix shell, I made a few mistakes. I accidentally deleted /tmp folder. To restore it all you have to do is:

mkdir /tmp
chmod 1777 /tmp
chown root:root /tmp
ls -ld /tmp

Password protecting file in vim text editor

Afraid that root user or someone may snoop into your personal text files? Try password protection to a file in vim, type:


vim +X filename

Or, before quitting in vim use :X vim command to encrypt your file and vim will prompt for a password.

Get a reminder you when you have to leave

If you need a reminder to leave your terminal, type the following command:

leave +hhmm

Want to go the directory you were just in? Run:
cd -

Need to quickly return to your home directory? Enter:
cd

The variable CDPATH defines the search path for the directory containing directories:

export CDPATH=/var/www:/nas10
Now, instead of typing cd /var/www/html/ I can simply type the following to cd into /var/www/html path:

cd html


See the output and write it to a file

Use the tee command as follows to see the output on screen and also write to a log file named my.log:

mycoolapp arg1 arg2 input.file | tee my.log
The tee command ensures that you will see mycoolapp output on on the screen and to a file same time.



History command will not show the time stamp.. to make it happen do below

# HISTTIMEFORMAT="%d/%m/%y %T "

If you want to permanently append this change, add the below line to ~/.bashrc.

#export HISTTIMEFORMAT="%d/%m/%y %T "

# source ~/.bashrc

Explanation of commands and switches.

history – GNU History Library
HISTIMEFORMAT – Environmental Variable
%d – Day
%m – Month
%y – Year
%T – Time Stamp
source – in short send the contents of file to shell
.bashrc – is a shell script that BASH runs whenever it is started interactively.

how to check disk write speed? Well one liner dd command script serves the purpose.

#dd if=/dev/zero of=/tmp/output.img bs=8k count=256k conv=fdatasync; rm -rf /tmp/output.img

Explanation of commands and switches.

dd – Convert and Copy a file
if=/dev/zero – Read the file and not stdin
of=/tmp/output.img – Write to file and not stdout
bs – Read and Write maximum upto M bytes, at one time
count – Copy N input block
conv – Convert the file as per comma separated symbol list.
rm – Removes files and folder
-rf – (-r) removes directories and contents recursively and (-f) Force the removal without prompt.

How will you check the top six files that are eating out your space?

# du -hsx * | sort -rh | head -6

Explanation of commands and switches.

du – Estimate file space usages
-hsx – (-h) Human Readable Format, (-s) Summaries Output, (-x) One File Format, skip directories on other file format.
sort – Sort text file lines
-rh – (-r) Reverse the result of comparison, (-h) for compare human readable format.
head – output first n lines of file.

The next step involves statistics in terminal of a file of every kind. We can output the statistics related to a file with the help of stat (output file/fileSystem status) command.

# stat filename_ext  (viz., stat abc.pdf)

Ex:

#stat filename.txt

Linux-command-line phobic and the below one liner will generate random man pages. The benefit is as a newbie you always get something to learn and never get bored.

# man $(ls /bin | shuf | head -1)

Explanation of commands and switches.

man – Linux Man pages
ls – Linux Listing Commands
/bin – System Binary file Location
shuf – Generate Random Permutation
head – Output first n line of file.

Command: url

Won’t it be an awesome feeling for you if you can update you twitter status from command line in front of your friend and they seems impressed. OK just replace username, password and your status message with your’s username, password and “your status message“.

# url -u YourUsername:YourPassword -d status="Your status message" http://twitter.com/statuses/update.xml

Command: aafire

How about fire in your terminal. Just type “aafire” in the terminal, without quotes and see the magic. Press any key to interrupt the program.

Install aafire

# apt-get install libaa-bin
Output

# aafire

Command: espeak
Just Turn the Knob of your multimedia speaker to full before pasting this command in your terminal and let us know how you felt listening the god’s voice.

Install espeak

# apt-get install espeak
# yum install espeak
Output

# espeak "Muthu loves sathya"

Command: oneko
OK so you believe that mouse pointer of Linux is the same silly black/white pointer where no animation lies then I fear you could be wrong. “oneko“ is a package that will attach a “Jerry“ with you mouse pointer and moves along with you pointer.

Install cmatrix

# apt-get install oneko
# yum install oneko

Output

# oneko

Command: cmatrix

You might have seen Hollywood movie ‘matrix‘ and would be fascinated with power, Neo was provided with, to see anything and everything in matrix or you might think of an animation that looks alike Hacker‘s desktop.

Install cmatrix

# apt-get install cmatrix
# yum install cmatrix
Output

# cmatrix

Command: toilet
what? Are u kidding, huhh no! Definitely not, but for sure this command name itself is too funny, and I don’t know from where this command gets it’s name.

Install toilet

# apt-get install toilet
# yum install toilet
Output

# toilet muthu

It even offers some kind of color and fonts style.

# toilet -f mono12 -F metal Muthu


Command: yes
It is funny but useful as well, specially in scripts and for System Administrators where an automated predefined response can be passed to terminal or generated.

# yes I Love Linux

I Love Linux
I Love Linux
I Love Linux
I Love Linux
I Love Linux
I Love Linux
I Love Linux
I Love Linux
I Love Linux
I Love Linux
I Love Linux
I Love Linux
Note: (Till you interrupt i.e ctrl+c).

Command: Cowsay
An ASCII cow in terminal that will say what ever you want.

Install Cowsay

# apt-get install cowsay (for Debian based OS)
# yum install cowsay (for Red Hat based OS)
Output

# cowsay I Love linux

 ____________
< I Love linux>
 ------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
How about pipelineing ‘fortune command‘, described above with cowsay?

# fortune | cowsay

 _________________________________________
/ Q: How many Oregonians does it take to  \
| screw in a light bulb? A: Three. One to |
| screw in the light bulb and two to fend |
| off all those                           |
|                                         |
| Californians trying to share the        |
\ experience.                             /
 -----------------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
Note: ‘|‘ is called pipeline instruction and it is used where the output of one command needs to be the input of another command. In the above example the output of ‘fortune‘ command acts as an input of ‘cowsay‘ command. This pipeline instruction is frequently used in scripting and programming.

xcowsay is a graphical program which response similar to cowsay but in a graphical manner, hence it is X of cowsay.

apt-get install xcowsay
yum install xcowsay
Output

# xcowsay I Love Linux
install xcowsay
xcowsay command
cowthink is another command just run “cowthink Linux is sooo funny” and see the difference in output of cowsay and cowthink.

apt-get insatll cowthink
yum install cowthink
Output

# cowthink ....Linux is sooo funny
 _________________________
( ....Linux is sooo funny )
 -------------------------
        o   ^__^
         o  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||


Command: rev (Reverse)

It reverse every string given to it, is not it funny.

# rev

123abc
cba321

xuniL eb ot nrob
born to be Linux

what about getting your random fortune, sometimes funny in terminal.

Install fortune

# apt-get install fortune (for aptitude based system)
# yum install fortune (for yum based system)
# fortune

You're not my type.  For that matter, you're not even my species!!!
Future looks spotty.  You will spill soup in late evening.
You worry too much about your job.  Stop it.  You are not paid enough to worry.
Your love life will be... interesting.

No comments:

Post a Comment