Tuesday, September 6, 2011

Helpful Linux Commands/Linux tips-tricks



Home | Profile 


-- To list file with line number
nl <filename>     

-- Saves man pages to text
man cat | col -b > less.txt  

-- Mounting as a loop device
mount -o loop -t iso9660 /FC2-i386-DVD.iso  /iso 

-- To split a large file into multiple files
split --bytes 1m bigfile 1_  

-- List of open files opened by raj under /dev/hda1
lsof -u raj -a /dev/hda1  

-- Find who owns the port number
fuser -v -n tcp 992345   

-- List of users accessing the /mnt file, -k option to kill all using the FS
fuser -v /mnt/*    

-- File/File System status
stat afile

-- Trace System calls
strace -aef cat afile

-- Run Top in batch file
top -b -d 5 -n 3 >> top.out  
b=batch, d=delay in seconds , n=number of iterations

-- To read headers of binary files (ELF)
readelf --file-header /bin/ls    

-- Provides file system statistics
stat <filename>

-- List all network services running
netstat -tanup    

-- List listening interfaces
netstat -l     

-- Shread and remove a file
shred -n 50 -zuv afile
n=shred the file "n" times, -z will zero out the file in the last pass (extra protection), u=truncate it every time, v=verbose.

-- Create a function Calculator - calc 3+45/10
function calc { echo "{$1}" | bc -l ; } 

-- Command line calculator
echo "10/2*10" | bc -l   

-- Search man pages for string
man -k string    

-- Merge pdf files into one
gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=Merged.pdf -dBATCH img*.pdf

-- Create pdf file from man page
 man -t cat | ps2pdf - > cat.pdf
  
-- Colors the string while grepping
grep --color string filename   

-- Copy files between Servers using netcat (Alternate option to consider if ftp is disabled )

On Server (in my case host graphics) run
   tar -cvjf - dummy.dat | netcat -l -p 3333
On the Client ( in my case it was silicon) run
   netcat graphics 3333 | pv -b | tar -xjvf -
On the server listening port 3333 is created and dummy.dat is tar'd. When netcat is ran with no default options, it basically connects to the server/port specified.

-- Rsync files between servers
silicon~> rsync -avrz . --include "init*.ora" --rsh='ssh' graphics:/usr/oracle/admin
Above syncs all init.ora files from silicon to graphics a=archive,v=verbose,r=recursive,z=compress



-- List all network services running on the system
netstat -tanup


-- Test FTP Speed. Use dd to create a large file, but instead of sending it to the disk send it to null device. Here 
/dev/zero is the input and output of the dd is /dev/null. 
put "|dd if=/dev/zero bs=1024 count=1000000" /dev/null


-- Find all files with group owner is "oracle" and change it to "dba" group.
find . -group oracle -exec chgrp dba '{}' \ ;


--Show files greater than a specific size 
find ./ -size +100M -type f -print0 | xargs -0 ls -Ssh1 --color


-- Send e-mail attachments with mutt
mutt -a file.txt -s "A Text File" raj.anju@gmail.com < /tmp/junk


-- Kill multiple processes in one shot
ps -ef|grep dbconsole | awk ' { print $2} ' | xargs kill -9 


-- Copy directory & files under remotely.
scp -r RMANREPO/ oracle@eccas1004:/usr/oracle/admin/RMANREPO/


-- Improve ftp speed with jumbo frames.
Note: Read more on the Jumbo frames, as its applicable only on certain situations. On a 10/100 data the max mtu can only be upto 1500, but for a GigaBit Ethernet connection it can go upto 9000. The best throughput I got while doing an XO (Cross-Over) connection between two systems is when the mtu was 6000.
ifconfig eth0 mtu 6000
For a permanent change you have to edit the below file to include MTU 6000
/etc/sysconfig/network-script/ifcfg-eth0



14 comments: