Linux Tips and Tricks

Linux Tips and Tricks

Find Palindrome using Grep command

October31

All of you know that, grep is used to find out the text pattern in the file or from the output of any commands.

Now, we see how it will be used to find out the palidrome words.

We have to use the regular expression to find out the palindrome

Example :

1
2
3
4
5
6
7
8
9
10
11
$ echo "madam" | grep -w  '^\(.\)\(.\).\2\1'
madam
 
$ echo "radar" | grep -w  '^\(.\)\(.\).\2\1'
radar
 
$ echo "test" | grep -w  '^\(.\)\(.\).\2\1'
$
 
$ echo "words" | grep -w  '^\(.\)\(.\).\2\1'
$

 

Explanation :

The grep command searches for the first any three letters  by using \(.\)\(.\).  after that we are searching the same 2nd character and 1st character is occuring or not.

The above grep command will find out only 5 letters palindrome words.

Guglielmo Bondioni proposed a single RE that finds all palindromes up to 19 characters long using 9 subexpressions and 9 back-references:

1
grep -E -e '^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?).?\9\8\7\6\5\4\3\2\1' file

Notethis is done by using gnu ERE extensions; it might not be portable to other implementations ofgrep.

posted under Uncategorized | No Comments »

Create a File and make it as unalterable

October26

The command chattr is used to change the file as unalterable.

It cannot be changed or deleted by even root

example:

$ touch testing_file

creating a empty file using touch command

$ ls -lrt testing_file
-rw-r–r– 1 kamaraj kamaraj 0 2010-10-26 20:02 testing_file

checking the availabiltiy of the file

$ chattr +i testing_file
chattr: Operation not permitted while setting flags on testing_file

The command chattr is not allowing me to change the settings. so i am gonna try as root

$ sudo chattr +i testing_file
[sudo] password for kamaraj:
$

Yes, i applied the settings.
i – immuatable. (refer the manual page : man chattr)

$ echo “test” > testing_file
bash: testing_file: Permission denied

Trying to add a word “test” in the testing_file. But it is not allowing me to add the text. Let me try as root

$ sudo echo “test” > testing_file
bash: testing_file: Permission denied

Even in the root also it is not allowing me to add the text “test”. Ok, then remove the immuatable settings

$ sudo chattr -i testing_file

removed the immuatable settings. ( you have to remove as root )

$ echo “test” > testing_file

$ cat testing_file
test

Note : This work only on ext2/ext3 filesystems.

posted under Uncategorized | No Comments »

Delete the Files Permanently – shred command

October26

we can use the command called shred

shred – overwrite a file to hide its contents, and optionally delete it

This is a tricky thing to do, so I would STRONGLY advise reading the man page on this command. Because this is very dangerous command and tricky too

info shred
man shred

Deleting a file or formatting a disk will not erase your data. We can easily recover the files using the recoverable softwares. So if you want to delete your sensitive data, then you can use shred command

example

shred file1 file2

the above command securely destroy the file1 and file2.

shred /dev/hda5

the above command securely destroy your 5th harddisk partition

Note : This very dangerous command, so before using this command, i recommend you to read the manual pages for the shred


posted under Uncategorized | No Comments »

Split large file into several smaller files

October21

To split large file into several smaller files, you can use split command in linux

$ split –bytes=1m large-file-name output-file-name

you can easily change the output file size by changing the -bytes value. you can use b (bytes), k (kilobytes), m (megabytes)

if you want to split the file by number of lines, then you can use the below command

split -l 100 filename

The above command will split the file with 100 lines……

posted under Uncategorized | No Comments »

Find the large file/directory

October21

The following commands are used to find out the largest file and directory

Find out the largest file in the directory

$ ls -lS | head -5
total 180416
-rw-r–r– 1 kamaraj kamaraj 58911671 2010-07-15 01:55 Shakira_Waka_Waka.mp4
-rwxrwxrwx 1 kamaraj kamaraj 47224409 2010-07-20 02:20 qt-creator-linux-x86-opensource-2.0.0.bin
-rw-r–r– 1 kamaraj kamaraj 20165034 2010-10-08 00:12 skype-ubuntu-intrepid_2.1.0.81-1_i386.deb
-rw-r–r– 1 kamaraj kamaraj 8833362 2009-08-23 23:37 RealPlayer11GOLD.deb

Find out the largest file and its size in KB

$ ls -lS | head -5 | awk ‘{print $5/1024 “KB” , $8}’
0KB
57530.9KB Shakira_Waka_Waka.mp4
46117.6KB qt-creator-linux-x86-opensource-2.0.0.bin
19692.4KB skype-ubuntu-intrepid_2.1.0.81-1_i386.deb
8626.33KB RealPlayer11GOLD.deb

find out the file which has the size between 100k and 150k

$ find /etc -size +100k -size -150k
/etc/X11/xkb/base.xml
/etc/ssh/moduli

Find out the file which has more than 50 MB

$ find . -type f -size +50000k -exec ls -lh {} \; | awk ‘{ print $8 “: ” $5 }’
./Shakira_Waka_Waka.mp4: 57M

Find out the largest Directory

The below command will top 5 directories that occupies more space in MB (if you want in GB then, replace [0-9]M to [0-9]G)

$ du -h | grep [0-9]M | sort -n -r  | head -5
508M    ./Maathi Yosi (2010)
238M    ./jdk1.6.0_10
108M    ./jdk1.6.0_10/jre
107M    ./jdk1.6.0_10/jre/lib
65M    ./Perl-Report

posted under Uncategorized | No Comments »

List out the currently loaded kernel modules

October20

lsmod command is used to tell the kernel modules which are currently loaded in your system

example:

Type lsmod and press enter in your shell prompt

posted under Uncategorized | No Comments »

Find the location of a executable file

October20
In linux, we have a command called which

This which command is used to tell the location of the executables

how to use ?

example :

kamaraj@kamaraj-laptop:~/Desktop/testing$ which ls
/bin/ls
kamaraj@kamaraj-laptop:~/Desktop/testing$ which ping
/bin/ping
kamaraj@kamaraj-laptop:~/Desktop/testing$ which cat
/bin/cat

posted under Uncategorized | No Comments »

Find the PID from the Process Name

October20

How to find the process id from the process name ?

In linux, you have a command called pidof

This pidof command is used to find out the process id of the given process name

Example:

kamaraj@kamaraj-laptop:~/Desktop/testing$ ps -ef | grep fire
kamaraj 6380 1 26 12:20 ? 00:30:32 /usr/lib/firefox-3.0.19/firefox
kamaraj 13107 12535 0 14:17 pts/0 00:00:00 grep fire

kamaraj@kamaraj-laptop:~/Desktop/testing$ pidof firefox
6380

posted under Uncategorized | No Comments »

How to capture the output of the Top command

October20
Top command is used to display linux tasks

when you do top command it will show the highly cpu consumable process in the top list.

if you want to capture and process the output of the top command then, you need to add some parameters to the top command.

-b –> batch mode operation
-n –> how many iterations

simply execute the command top -b -n1

The above command will give the output of the top command, Try it out and let me know if you have any doubts..

posted under Uncategorized | No Comments »

What is happening when you are execuing commands ?

October20

Well, when you are exeuting any commands or scripts, the shell will internally do the system calls and do the job very quickly.

you want to trace the sytem calls and signals ?

lets try….

Example:

use strace command to trace the system calls

just do strace -c ls

you will get the list of system calls that made by the command ls

The best way to understand

do man strace

posted under Uncategorized | No Comments »
« Older EntriesNewer Entries »

Recent Comments

    Categories