Shell Programming Blog Shell Programming Blog

Sunday 30 September 2012

Linux terminal freezing problem [resolved] :-)

Unknown | 03:48 | Be the first to comment!



Bash Linux Unix shell scripting



I often wonder why my Linux terminal freezes when I press certain key combinations unknowingly. Finally when I found the key combination, I found that I pressed Ctrl+s key. Which is used for enabling the scroll lock. Hence the screen was shocked. To release the lock, we have to press the Ctrl+q. This key combination will unlock the scroll lock.

Please share any such instances with us that you encountered in your Linux career.

How to display the text in Bold and Underline in Linux

Unknown | 03:42 | 3 Comments so far


 


 To display the test in "BOLD" and "UNDERLINE", the following ANSI Escape Sequences can be used.

To display the text in BOLD
$ echo -e "\033[1mThis is a BOLD line\033[0m"
This is a BOLD line

#Using tput
$ tput bold
$ echo "This"     #BOLD
$ tput sgr0     #Reset text attributes to normal without clear.
$ echo "This"     #NORMAL

To display the text in UNDERLINE
$ echo -e "\033[4mThis is an underlined line.\033[0m"
This is an underlined line.

Saturday 29 September 2012

How to sort the files and directories based on their size

Unknown | 23:25 | | | Be the first to comment!




Let us see how to sort the files and directories. In my current directory, I have the following files and sub-directories:

$ file *
config: directory
val.dat: ASCII text
sample.txt: ASCII text
log: directory

i.e., 2 files and 2 directories

Now to list the files and directories (in the current directory) in the descending order of file size.

$ du -sk * | sort -nr
4730 log
2779 config
1100 val.dat
968 sample.txt

To list only the directories (in the current directory) in descending order of size:

$ du --max-depth=1 . | sort -nr
9308 .
4730 ./log
2779 ./config


Since the sub-directories also contain files, let's list all the files (not directories) in the descending order of file sizes

$ find . -type f -exec du -sk {} \; | sort -nr
2196 ./log/main1.dat
2064 ./config/pi.dat
1100 ./log/huo.txt
1100 ./val.dat
968 ./sample.txt
964 ./log/as.txt
916 ./config/lis.txt


If you would like to restrict find to the current directory only and not the sub-directories, use need to use maxdepth=1. i.e.

$ find . -maxdepth 1 -type f -exec du -sk {} \; | sort -nr
1100 ./val.dat
968 ./sample.txt

How to list the empty directories in Linux

Unknown | 22:37 | Be the first to comment!


 

The find command in Linux has an option called 'empty'. This option can be used to list empty regular files or empty directories.

e.g.

To list all the empty directories

$ find . -type d -empty

Output:

./dir1/praset
./salo/new/data5
./premoc/test1

How to find the past and future dates using date command

Unknown | 11:53 | Be the first to comment!


 
 
The "date" command in Linux is used to display the current date and time. But, we can also make the date command to display past and future dates based on the inputs/parameters that we pass to it. Below are some of the ways to find the future and past dates.
 
 
 
1) Normal dates:
Today
$ date +%Y-%m-%d
2012-09-29
 
After 2 days
$ date +%Y-%m-%d -d "2 day"
2012-10-01
 
Before 2 days
$ date +%Y-%m-%d -d "-2 day"
2012-09-27
 
Yesterday
$ date +%Y-%m-%d -d "yesterday"
2012-09-28
 
Tomorrow
$ date +%Y-%m-%d -d "next day"
2012-09-30
 
After 3 weeks
$ date +%Y-%m-%d -d "3 weeks"
2012-10-20
 
Before 3 weeks
$ date +%Y-%m-%d -d "-3 weeks"
2012-09-08
 
One Year 3 days after
$ date -d "1 year 3 days"
Wed Oct  2 02:24:53 CDT 2013
 
Last Sunday what was the date ?
$ date -d 'last sun'
Sun Sep 23 00:00:00 CDT 2012
 
How about coming Sunday?
$ date -d 'sun'
Sun Sep 30 00:00:00 CDT 2012
 
This year, what day does "Independence Day" fall on?
$ date --date='15 Aug' +%A
Wednesday
 
2) GMT Time?
$ date -u
Sat Sep 29 07:27:33 UTC 2012
 
3) Epoch time?
Number of seconds from 1970-01-01 UTC (epoch time,it is the number of seconds elapsed since midnight UTC of January 1, 1970, not counting leap seconds)
$ date +%s
1348903671
 
The opposite way conversion, from epoch time to date is shown below.
$ date --date '1970-01-01 UTC 1199968580 seconds'
Sat Sep 29 02:27:51 CDT 2012
 
To make the above command as a function which can be used always, you need to do the following steps.
Make the below function in ~/.bash_profile

epochtime () {
date --date '1970-01-01 UTC '$1' seconds'
}
 
So that you can use it as below in the command line or in a script.
$ epochtime 1199968580
Sat Sep 29 02:27:51 CDT 2012

Friday 28 September 2012

How to calculate the size of a directory in Linux

Unknown | 12:09 | Be the first to comment!


 

Let us see how to calculate the size of a directory including its sub directory.

To calculate the total folder size of folder "bin"

$ du -sh bin/
280K bin/

-s, --summarize (display only a total for each argument)
-h, --human-readable ( print sizes in human readable format (e.g., 1K 234M 2G))

How to find largest files in a directory and its sub directories

Unknown | 11:15 | Be the first to comment!




A situation may occur in your office that you might have to clear the space in the server by deleting the largest files in the log directory. If you want to find and print the top 10 largest files' names (not directories) in a particular directory and its sub directories you can use the following commands.

$ find . -printf '%s %p\n'|sort -nr|head

To restrict the search to the present directory, you can use the parameter "-maxdepth 1" with find command.

$ find . -maxdepth 1 -printf '%s %p\n'|sort -nr|head

And to print the top 10 largest "files and directories":

$ du -a . | sort -nr | head

you can vary the number of files to be printed on the screen by using "head" command as "head -n X" instead of using only "head" in all the above commands to print the top X largest files

How to remove duplicate entries from a file without sorting

Unknown | 10:58 | Be the first to comment!


 


Usually whenever we want to remove duplicate entries or lines from a file, we need to sort the entries and then eliminate the duplicates using "uniq" command.

But if we want to remove the duplicates and preserve the entries in the same order or sequence, here is the way:



 Sample file:


Using the "uniq" command without sorting the file will not remove all duplicates.


The "sort" command has an option (-u) to sort and uniq, but we will lose the original sequence of the entries


Using sort and then uniq commands will remove the duplicates, but sequence?? (Same as above)


Finally, here is the solution using AWK:


How to exclude or skip a directory while searching for a file using FIND command

Unknown | 10:33 | Be the first to comment!




Find command is used to search the files in the Linux environment. This find command will search all the directories and the sub directories in the path which we give in the find command. Often we would like to exclude some directories from our FIND command. Now we will see how to do it.


The below command is used to search all the *.sh files starting from current directory.



Suppose if you want to skip or exclude the directory './dir22' and all files and directories under it, you need to use -prune option with bash find command.



Wednesday 26 September 2012

Sorting the file leaving the first line and writing in a file

Unknown | 10:43 | 1 Comment so far



Now we can see how to sort a file by leaving the first line as it is. For example, we take a file sample.txt which contains the below data.

chrom:index:forward:reverse
chr01:13:1:2
chr13:23432:4:7
chr01:3445:1:6
chr02:2311:3:1
chr01:212:5:2
chr03:12:1:4
chr02:345:12:6
chr01:45:45:0

If we sort the above data by leaving the first line, the output would be

chrom:index:forward:reverse
chr01:13:1:2
chr01:45:45:0
chr01:212:5:2
chr01:3445:1:6
chr02:345:12:6
chr02:2311:3:1
chr03:12:1:4
chr13:23432:4:7

Now we are going to see the command which sorts the content of the file by ignoring the header line (i.e. the first line). This command sorts the first field in ascending order(String) and the second field in the ascending order(numeric)

head -1 sample.txt  

tail -n +2 sample.txt | sort -t : -k1,1 -k2,2n

1) The first command prints the first line of the file on to the screen.
2) The second command will print all the lines except the first line and then sorts the data using the first field (string sort) and then with the second field(numeric sort).

If you want the output to be written in a file, redirect the output using the re-directional operators.

head -1 sample.txt > output.txt

tail -n +2 sample.txt | sort -t : -k1,1 -k2,2n >> output.txt

The symbol > is for writing into a file. This will clear the output.txt if exists and then writes the data. If the file does not exist, it will create the file and then write the output to it.

The symbol >> will append the data to the existing data in the output.txt file.

Tuesday 25 September 2012

How to use Arrays in Shell scripts

Unknown | 10:28 | 2 Comments so far


We know what an array is. An array is a variable which can store multiple values in the same name. These values can be accessed by using the subscripts. The whole of the array can be accessed by just mentioning the array name without any subscripts. Let us see some of the important operations using array in BASH with the help of the below script.




The output of the above script would be

$ ./array.sh
The array contains 4 members. They are:
0: ram
1: rick
2: sittik
3: rahim
Adding bill to the end of the array
Listing all the elements in the array
ram rick sittik rahim bill
Listing all the elements in the array
ram rick sittik rahim bill
Deleting "rick"
Now the array is
ram sittik rahim bill
length of 3rd element in the array
6
Deleting the whole array



Monday 24 September 2012

How to print the lines to the screen with line numbers

Unknown | 10:45 | Be the first to comment!


As you know, the CAT command prints the lines of the file to the screen in the ASCII format. This is one of the command that is used to view the content of the file without opening the file in an editor. Suppose if you want the lines of the files to be displayed on the screen with line numbers, you can use the below options.



1) CAT command:
From man page of CAT(1):
       -n, --number
              number all output lines

So,
$ cat -n file.txt | less

will print the line number in-front of each line.

2) LESS command: 
The CAT command displays all the content in a single go. But the LESS command displays the content of the file page by page. You can use the below options to use view the content of the file using LESS command.

Press the [space-bar] if you want to see another page, and type [q] if you want to quit reading.

From man page of LESS(1):
       -N or --LINE-NUMBERS
              Causes a line number to be displayed at the beginning of each line in the display.

So,
$ less -N file.txt

will display the lines along with the line numbers.

3) you can set the less command to print the lines with line numbers by default using the below command.

$ export LESS='-RS#3NM~g'

look command to print lines in Linux

Unknown | 10:32 | 1 Comment so far


 


Let me introduce a simple UNIX/Linux command which is very useful but not that popular. The name of the command is 'look'. As the name suggests, this command displays lines beginning with a given string.


Usage:
look string <file>

The look utility displays any lines in 'file' which contain 'string' as a prefix. This can be a simple alternative of using

$ grep "^string" <file>

If file is not specified while using the look command, the file /usr/share/dict/words will be used, only alphanumeric characters are compared and by default the case of alphabetic characters is ignored.

This utility can be very useful to look up the dictionary words from 'usr/share/dict/words'

e.g.
$ look Das
Dasheen
Dashers
Dashier
Dashiki
Dashing
Dashpot
Dassies
Dastard
Dasyure



In order to ignore the case of alphabetic characters you can use its -f option. (similar to grep -i)

Exit Status:
The exit status of the look utility will be 0 if one or more lines found and displayed.
The exit status of the look utility will be 1 if no lines found
The exit status will be >1 if there is an error.

Sunday 23 September 2012

How to find the length of a string in Linux/Unix Shell scripting

Unknown | 20:16 | 2 Comments so far



While writing a shell script you might want to find the length of a string. While reading GNU expr command man page I found an interesting option:

expr length STRING

For example display the length of "abusittik" word:
expr length "abusittik"

Output:
9

expr and POSIX

However expr command is not concerned with POSIX (open system standards based on Unix). You can try old good KSH/Bash command (also following command should work with other UNIX like OSes such as FreeBSD / Solaris ):

myVar="abusittik"
echo ${#myVar}


Output:
9

Using AWK :

% echo abusittik | awk ‘ { print length } ‘

Output : 9

Using Perl:
 % echo abusittik | perl -nle ‘ print length ‘

Output : 9

A shell script to normalize the month field in the given date

Unknown | 08:25 | Be the first to comment!


 


 This script will normalize the month field in the given date to three letters and the first letter would be capitalized.





Saturday 22 September 2012

How to recover the files from pen drive or removable disk which are affected due virus attack?

Unknown | 22:28 | Be the first to comment!

Nowadays USB pen drives are used by most of the people to transfer and save the data. A very important problem in the USB pen drives is virus infection. Since the pen drives are being  used in different systems to transfer files, in spite of the antivirus softwares used, the viruses are affecting the files stored in the pen drive easily.

If your files are affected by the virus, your files will go hidden and it will not be visible when you open the pen drive. It will display an empty folder without files. But when you check the "properties" of the pen drive, you can see that your pen drive contains the files by the size of the memory used. If you do not have any important documents in the pen drive, you can format it and use it again. But, if you have any important files in the pen drive, you would want to recover the files at any cost. 

You don't need to install any software to recover the data from your pen drive. You can easily recover the files by following the below steps without using any recovery software.

  1. Insert the pen drive in your computer 
  2. Go to Start => Run => type "cmd" without quotes and press enter. 
  3. Now check which drive is your pen drive using my computer(If you open My computer, you can see the label of the pen drive)
  4. Suppose if the pen drive is E: drive, then you type "E:" in the command prompt and press Enter.  
  5. type "attrib -h -s -r /s /d *.*" in the prompt with spaces in the appropriate places. 
  6. Check once whether you have typed correctly and then press Enter
Wait for few seconds and now check your pen drive. You will be able to see the files again in your pen drive. Happy Recovery!!!

வைரஸ் தாக்கிய ‘பென்ட்ரைவ்’ இலிருந்து பைல்களை மீட்க சிம்பிள் வழி!

Unknown | 21:49 | Be the first to comment!

தற்பொழுது தகவல்களை சேமிக்க பெரும்பாலானவர்களால் பயன்படுத்தப்படுவது USB பென்டிரைவ்கள். இதில் முக்கியமான பிரச்சினை வைரஸ் பிரச்சினை. வெவ்வேறான கணனிகளில் உபயோகிப்பதால் வைரஸ்கள் சுலபமாக பென்டிரைவில் புகுந்து உள்ளே இருக்கும்பைல்களை பாதிக்கிறது.

இப்படி பாதிக்கும் பொழுது உங்கள் பென்ட்ரைவில் உள்ள பைல்கள் மறைக்கப்பட்டுவிடும் கணனியில் பென்டிரைவை ஓப்பன் செய்தால் எந்த பைல்களும் இருக்காது. வெற்றிடமாக இருக்கும். ஆனால் properties சென்று பார்த்தால் பைல்கள் இருப்பது போன்றே அளவு காட்டும். காரணம் நம் தகவல்களை வைரஸ்கள் மறைத்து வைத்துவிட்டது. பென்டிரைவில் முக்கியமான தவல்கள் ஏதும் இல்லை எனில் Format செய்து பென்டிரைவை திரும்ப பெறலாம். ஆனால் ஏதேனும் முக்கிய மான தகவல்கள் இருந்தால் எப்படி அந்த பைல்களை பத்திரமாக மீண்டும் கொண்டு வருவது என பார்ப்போம்.
இதற்க்கு நீங்கள் எந்த மென்பொருளையும் உங்கள் கணினியில் Instal செய்து உபயோகிக்க வேண்டியதில்லை.உங்கள் கணனியிலேயே சுலபமாக செய்து விடலாம். கீழே உள்ள வழிமுறையின் படி கவனமாக செய்து அந்த பைல்களை மீட்டு எடுங்கள்.

1) முதலில் பென்டிரைவை உங்கள் கணினியில் சொருகி கொள்ளுங்கள்.
2) Start ==> Run ==> CMD==> Enter கொடுக்கவும்.
 3) இப்பொழுது பென்ட்ரைவ் எந்த ட்ரைவில் உள்ளது என பாருங்கள். My Computer செல்வதன் மூலம் கண்டறியலாம்.
4) உதாரணமாக E: டிரைவில் பென்ட்ரைவ் இருக்கிறது எனவைத்து கொள்வோம் அதற்கு நீங்கள் E: என கொடுத்து Enter அழுத்தவும்.

 
 5) attrib -h -s -r /s /d *.*என டைப் செய்யுங்கள் ஒவ்வொருபகுதிக்கும் Space சரியாககொடுக்கவும்.

◦நீங்கள் சரியாக கொடுத்துஉள்ளீர்கள் என உறுதி செய்து கொண்டு Enter அழுத்துங்கள்.

◦சில வினாடிகள் பொறுத்திருங்கள். இப்பொழுது உங்கள் பென்ட்ரைவ் சோதித்து பாருங்கள் உங்களுடைய பைல்கள் அனைத்தும் திரும்பவும் வந்திருக்கும்

How to make web links open in Firefox 15 by default?

Unknown | 11:44 | Be the first to comment!

If you have more than one web browser, Windows needs to know which browser to use by default. This article will show you how to make Firefox your default browser.
  1. At the top of the Firefox window, click on the Firefox button (Tools menu in Windows XP) and then click Options
  2. Select the Advanced panel, then click the General tab, and then click Check Now.Default - Win
  3. Select Yes to set Firefox as your default browser.
Note: MSN Messenger and other applications may open Internet Explorer regardless of which browser is the default. Also, Internet service providers like PeoplePC Online, Juno and NetZero may provide connection software that automatically launches Internet Explorer.

To set another browser as the default, use the settings in the browser you wish to be your default.

how to find out exit status of command or shell script?

Unknown | 11:16 | Be the first to comment!



By default in Linux if particular command/shell script is executed, it return two type of values which is used to see whether command or shell script executed is successful or not.

(1) If return value is zero (0), command is successful.
(2) If return value is nonzero, command is not successful or some sort of error executing command/shell script.


This value is know as Exit Status.

But how to find out exit status of command or shell script?
Simple, to determine this exit Status you can use $? special variable of shell. For e.g. (This example assumes that unkfile doest not exist on your hard drive)


$ rm unkfile

It will show error as follows
rm: cannot remove `unkowm1file': No such file or directory and after that if you give command


$ echo $?

It will print nonzero value to indicate error. Now give command


$ ls
$ echo $?

It will print 0 to indicate command is successful.


Exercise
Try the following commands and not down the exit status:
$ expr 1 + 3
$ echo $?


$ echo Welcome
$ echo $?

$ wildwest canwork?
$ echo $?

$ date
$ echo $?

$ echon $?
$ echo $?

Shell Built in Variables

Shell Built in VariablesMeaning
$#Number of command line arguments. Useful to test no. of command line args in shell script.
$*All arguments to shell
$@Same as above
$-Option supplied to shell
$$PID of shell
$!PID of last started background process (started with &)

 

 

Shell Programming Copyright © 2012 Shell Programming theme is Designed by Abusittik, Shell Programming