Posted in linux, work

Some cool linux commands and utilities :)

1. Command to generate a pseudo-random number between 1 and 9:
head -c10 /dev/urandom | md5sum | tr -d [a-z] | tr -d 0 | cut -c1

2. To generate alphanumeric with special characters:
cat /dev/urandom | tr -dc “a-zA-Z0-9-_\$\?\!\@\#\%\^\&\*\(\)\+\.” | fold -w 10 | head -10

Here: fold –w 10 is for the size of the string and head -10 gives you 10 values.

3. It is always better to avoid unnecessary commands, particularly in a pipe:
cat “$file” | grep “$word”
grep “$word” “$file”

4. A descriptive listing of directory, though it doesn’t replaces the conventional ls command and its options:
(printf “PERMISSIONS LINKS OWNER GROUP SIZE MONTH DAY HH:MM PROG-NAME\n” ; ls -l “Directory_Name” | sed 1d) | column –t

Here provide the directory name in “Directory_Name

5. To check the difference between two files, two directories.
>diff oldfile.txt newfile.txt
To check the difference between two directories, it is very useful when restoring a backup in an upgrade.
>diff –r |less

6. To create a hierarchy of directories.
>mkdir –p /one/two/three/four/five

7. To convert a pdf file to text and html, or to get the information about the pdf file.

To get information about the PDF file:
>pdfinfo

To convert the pdf file to test or html:
>pdftotext
>pdftohtml

8. DSTAT is a versatile replacement for vmstat, iostat, netstat and ifstat. Dstat is very handy for monitoring systems during performance tuning tests, benchmarks or troubleshooting.

Usage: As dstat is not present with linux by default, for dstat usage, one must install the dstat utility: dstat-0.7.2-1.el6.rfx.noarch.rpm, note that: The rpm will differ based on the operating system you are using.

Eg:  Following command will provide system stat with following data stats:

Cpu, Net, Disk, System, Load, proc, top_cpu and memory usage in single command.

>dstat –time –cpu –net –disk –sys –load –proc –top-cpu -m –bw

dstat_all

To get info about the free space on the system:

>dstat –freespace –bw

dstat_freespace

Consider checking the manual page for dstat, to explore more on checking the system stats and health.

The output of the dstat can be directly written as well as displayed in the screen using –output option. Command is: dstat -t -a –output file.csv, open the csv file with a spreadsheet application such as Microsoft Excel, you can select the columns you want plotted into a graph.

9. Following are some not so useful commands, but fun to play around linux:

To reverse a string or any input, just type >rev (enter), give the input (enter), you will get the reversed output, other way to use the command is : >rev <file_name> <path of the file>.

If you face hard time finding the factor for a number, a simple way to calculate the factor of a number is to just type: >factor [number to decompose]

10.  :(){ : |: & };:     The given set of characters is very dangerous bash function, it is called as Fork Bomb, on executing the given line, it defines a shell function that creates new copies of itself. The process continually replicates itself, and copies continually replicate themselves, quickly taking up all your CPU time and memory, this can even cause your computer to freeze. It is basically a denial-of-service attack.

11. To compare a remote file with a local file, to check if there are difference between local and remote file:

>ssh user@host cat /path/to/remotefile | diff /path/to/localfile –

12. To query Wikipedia via console over DNS for a txt record. The txt record will also include a short URL to the complete corresponding Wikipedia entry:

>dig +short txt <keyword>.wp.dg.cx

13. A simple way to copy the ssh keys to enable password-less ssh logins:

>ssh-copy-id user@host

Make sure the private and public RSA key pair is generated, if not use the following command: >ssh-keygen and provide enter as input for each request.

Once ssh keys copy is done, check login with >ssh ‘user@host’ , user should be able to login without password authentication.

14. In addition to above command, if you want to edit a file on a remote host:

>vim ssh://username@host//path/to/somefile

15. If your mysql server gets clogged up, a better way to monitor the processlist to check which queries are causing the server to clog up is with watch.

>watch –n 1 mysqldadmin –user=<user> –password=<password> processlist

16. To generate the pdf file for the manual pages available in server:

>man –t <command> | ps2pdf – > command_file.pdf

17. To append a file to existing tar file, use the –r or –append option, the append option is not allowed when the file is being compressed

>tar –rvf tarfile.tar file.txt

 

18. TCP dump to check if there is any incoming http (GET/POST) traffic on any port:

/usr/sbin/tcpdump -nX -s0 -i any port 9201 or 9200 or 9203 or 9202 | grep GET/POST

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s