Friday, February 12, 2010

FW: Linux tips and tricks..

1. List file names with the length

1. Full length

ls | awk '$(NF+1)=length'

2. Length without extension

ls | awk -F'.' '{ print($0, NF>1?length-length($NF)-1:length) }'

2. Protect Files with password

1. Zip with password

zip -e [zipfile [file1 file2 file3 ...]]

This command will prompt for password as well as verify password. use unzip zipfile to extract that file

2. Tar.gz with passphrase

tar -czf [newname.tar.gz file]
gpg -c newname.tar.gz

This command will prompt for passphrase as well as verify passphrase then create passphrase protected file called newname.tar.gz.gpg. use

gpg newname.tar.gz.gpg
tar -xzf newname.tar.gz

to extract that file. This protection is better that zip protection

3. Unzip many files at once

Generally  unizip *.zip command will not work as expect. In this instance, bellow commands will help

unzip '*.zip'

or

unzip \*.zip

4. Delete zero byte files

find $source -name $name -size 0 -exec rm -f {} \;

5. Date Format Matching in a File

Instead of this command for matching YYYY-MM-DD format

grep '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]' file_name

use this

grep '[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}' file_name

6. Get Argument Value in Different way

${var:FFSET:LENGTJ}

For instance, ${2:3:7} will give the result if the second argument is xportalwiki, rtalwik.

7. Remotely Access for Particular Files

Generally  scp command can be used to copy files/directory from/to a remote machine. But the bellow ways tell how to access particular files from a remote machine.

ssh user@remotemachine 'find $dir -name $name -cmin -15 -print0'

This will return files from the remote machine which were created in last 15 minutes.

8. Cron - A Powerful Schedule Task Manager

By default cron jobs sends a email to the user account executing the cronjob. If this is not needed put the following command At the end of the cron job line .

>/dev/null 2>&1

1. Crontab syntax

A crontab file has five fields for specifying day , date and time followed by the command to be run at that interval.

*  *  *  *  *  command to be executed
-  -  -  -  -
|  |  |  |  |
|  |  |  |  +------ day of week (0 - 6) (Sunday=0)
|  |  |  +--------- month (1 - 12)
|  |  +------------ day of month (1 - 31)
|  +--------------- hour (0 - 23)
+------------------ min (0 - 59)

2. Examples

Every Fifteenth Minute of every hours

15 * * * *

Every Fifteen Minute

*/15 * * * *

Every Fifteen Minute (Alternative Syntax)

0,15,30,45 * * * *

Every Day Mid Night

0 0 * * *

First of Every Months, Every hours

0 * 1 * *

9. Truncate a File

This is an easiest way to truncate a file. While looking a log file too, this kind of truncate is usefull

cat /dev/null > file_name

10. Shell Script first line

You may noticed that a shell script first line would be something like

#!/bin/bash

This is the interpreter for that script. For instance, if you write a php script under Unix environment your first line should be like this

#!/usr/local/bin/php

Whatever the script you have to put its interpreter in the first line leading with #!

In order to file the interpreter path, you can simply type this (for php)

which php

result would be /usr/local/bin/php

11. Packet Sniffing - tcpdump

tcpdump is a powerful command line packet sniffer. Make sure that tcpdump is used with root privileges to have sufficient privileges on a network device or a socket.

The file where sniffed packets by tcpdump are saved can be analyzed by a GUI tool Wireshark.

#tcpdump
This command is standard output of tcpdump packet sniffer. Command shell shows all the packets when this command is executed

#tcpdump -D
This command shows network interfaces available for capture. For instance

# tcpdump -D
1.bond0
2.eth0
3.eth1
4.any (Pseudo-device that captures on all interfaces)
5.lo

One of these interface will be used with latter command.

#tcpdump -c 20
This command will get only 20 packets and exit after that.

#tcpdump -i eth1 or #tcpdump -i 3
This command capture packets form particular interface. Interface name is used in the first command and interface number which is got from
 #tcpdump -D is used in second command.

#tcpdump -w /tmp/file_name.pcap
This command is to save packets to a output file. Here all packets are saved to
 /tmp/file_name.pcap file.

#tcpdump -s 1500
By default sniff size of packets is 96 bytes. It can be overloaded by this -s option.

#tcpdump -w /tmp/file_name.pcap host ss7 or #tcpdump -w /tmp/file_name.pcap host 172.16.11.17
Here host option is to define the host name or IP address of it so that packet sniffing is done only for that host.

#tcpdump -w /tmp/file_name.pcap -C 100 port 80
As it self explains, packets are captured for particular port given.

#tcpdump -C 100 -w /tmp/file_name.pcap
the flag C restricts file size of file_name.pcap so that if it's size exceeded it recreate that file.

Good Practice on TCPdump

  1. Get IP address and port to capture the packets.
  2. Make sure you first execute the command  #tcpdump -D to get interface.
  3. Always try to create output file with .pcap or .cap extension and in the /tmp directory.

For instance,

#tcpdump -s 1500 -w /tmp/file_name.pcap -C 100 host 172.16.11.17 and port 8080 -i 4

Notice that 'and' is used as host and port.

 

No comments:

Post a Comment