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:


 

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