Linux Tips and Tricks

Linux Tips and Tricks

Find Prime Number using Factor Command

October31

Hi All,

Here is the one line command which i written to find out the number is prime or not.

1
2
3
4
5
6
7
8
$read number;result=`factor $number | wc -w`; [ $result = "2" ] && echo "$number is prime Number" || echo "$number is not prime number" ; echo "Factors of" `factor $number`
78
78 is not prime number
Factors of 78: 2 3 13
$read number;result=`factor $number | wc -w`; [ $result = "2" ] && echo "$number is prime Number" || echo "$number is not prime number" ; echo "Factors of" `factor $number`
13
13 is prime Number
Factors of 13: 13

If you like to check for all the numbers then use the below command

1
2
3
4
5
6
7
8
9
10
11
12
$ for i in `seq 1 10`; do result=`factor $i | wc -w`; [ $result = "2" ] && echo "$i is prime Number" || echo "$i is not prime number" ; done
 
1 is not prime number
2 is prime Number
3 is prime Number
4 is not prime number
5 is prime Number
6 is not prime number
7 is prime Number
8 is not prime number
9 is not prime number
10 is not prime number

To display only the prime numbers

1
2
3
4
5
$ for i in `seq 1 10`; do result=`factor $i | wc -w`; [ $result = "2" ] && echo "$i is prime Number"; done
2 is prime Number
3 is prime Number
5 is prime Number
7 is prime Number

Note : Change the seq command input for more output

posted under Uncategorized | No Comments »

Kill the User and his Processes

October31

Here is the one line command which kill the specific user and his all running processes.

kill -9 `ps aux | awk -v var=”kamaraj” ‘$1==var { print $2 }’`

The above command will kill all the processes of the user kamaraj

Note : some processes needs root access to kill

posted under Uncategorized | No Comments »

Creat Directories and Sub Directories in one command

October31

We use mkdir command to create directories. In the mkdir command we have a option called -p, which is used to create directory and sub directories in one shot.

kamaraj@kamaraj-laptop:~/Desktop/testing/test$ man mkdir | grep -A2 “\-p”
-p, –parents
no error if existing, make parent directories as needed

Example :

I am going to create directory in the below manner.

a directory contains the directory b
b directory contains the directory c
c directory contains the directory d

kamaraj@kamaraj-laptop:~/Desktop/testing$ cd test/
$ tree
.

0 directories, 0 files
$ pwd
/home/kamaraj/Desktop/testing/test
$ ls -lrt
total 0
$ mkdir -p a/b/c/d
$ ls -lrt
total 4
drwxr-xr-x 3 kamaraj kamaraj 4096 2010-10-31 12:05 a

$ tree
.
`-- a
       `-- b
             `-- c
                   `-- d
4 directories, 0 files

$

tree command is used to display the directory structure in the tree format ( parents and its childs )

posted under Uncategorized | No Comments »

Find and Replace with SED

October31

Normally we use SED command to replace one word to another.

The following example will replace the word old with the word new in the test.txt file

$ echo “old is old” > test.txt

$ cat test.txt
old is old

$ sed -i ‘s/old/new/’ test.txt

$ cat test.txt
new is old

Note : The above sed command only replaced the first occurance. If you want to replace all the occurance in the line. you have use ‘g’

$ sed -i ‘s/old/new/g’ test.txt

Now, we see how to replace in all the files from the current directory.

$ find . -type f -exec sed -i ‘s/old/new/g’ {} \;

Here is the sample script to take backup of the original file before you do the changes with SED command.

#!/bin/bash
for i in *; do
cp $i $i.bak
sed -i ‘s/old/new/g’ $i
done

SED tutorial: http://www.grymoire.com/Unix/Sed.html

posted under Uncategorized | No Comments »

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 »
« Older Entries

Recent Comments

    Categories