Linux Tips and Tricks

The Redis Labs Team -

Some Linux tips and tricks:

[Everyone is welcome to update]

 

make hostname -> uppercase

hostname | cut -d '.' -f1 | awk -F app '{print $1}' | tr [:lower:] [:upper:]

 

 

Extend Linux partition in lvm:

 

extend VG

vgextend sysvg00 /dev/sdb

Volume group "sysvg00" successfully extended

 

extend LV

lvextend -L +29G /dev/sysvg00/rootlv00 /dev/sdb

Extending logical volume rootlv00 to 101.06 GiB

Logical volume rootlv00 successfully resized

 

resize filesystem

resize2fs /dev/sysvg00/rootlv00

resize2fs 1.41.12 (17-May-2010)

Filesystem at /dev/sysvg00/rootlv00 is mounted on /; on-line resizing required

old desc_blocks = 5, new_desc_blocks = 7

Performing an on-line resize of /dev/sysvg00/rootlv00 to 26492928 (4k) blocks.

The filesystem on /dev/sysvg00/rootlv00 is now 26492928 blocks long.

 

Rescan scsi drives/disks

echo "- - -" > /sys/class/scsi_host/host0/scan

 

Serch and Replace with perl:

find . -type f | xargs perl -p -i -e "s:abcd:efgh:g"

 

Replace in multiple files:

find -type f -name '*.txt' -print0 | xargs --null perl -pi -e 's/Windows/Linux/'

 

Adding swapfile(file) to linux

create file:

dd if=/dev/zero of=/swapfile0 bs=1024 count=1048576 (for 1gb 1024*1024)

dd if=/dev/zero of=/swapfile0 bs=1024 count=3145728

add to swap:

mkswap /swapfile0

chown root:root /swapfile0

chmod 0600 /swapfile0

swapon /swapfile0

 

vi /etc/fstab

/swapfile0 swap swap defaults 0 0

 

Passwordless SSH

make it work:

ssh-keygen -t rsa

ssh user@host mkdir -p .ssh

cat .ssh/id_rsa.pub | ssh user@host `cat >> .ssh/authorized_keys'

ssh user@host “chmod 700 .ssh; chmod 640 .ssh/*”

ssh user@host

 

multi copy :

for host in $(cat serverlist.txt); do ssh-copy-id $host; done

 

multi run:

for host in $(cat hosts.txt); do ssh "$host" "$command" >"output.$host"; done

 

 

Check in HTTP server is running by curl:

 

while [[ $(curl -s -w "%{http_code}" http://server -o /dev/null) != "200" ]]; do

sleep 5

done

 

 

Clear all files in directory (no delete )

 

echo -n | tee *

 

Top 10 CPU consuming processes:

 

ps aux  --sort=-%cpu | grep -m 11 -v `whoami`

 

history with timeframe:

HISTTIMEFORMAT="%d/%m/%y %T " 

history

 

top 10 space consuming directories/files:

du -hsx * | sort -rh | head -10

 

find command by description

apropos “description”

 

find open ports:

netstat -lnp

 

list today files:

ls -al --time-style=+%D | grep `date +%D`

 

To print columns eg 1 and 3 from a file file1 into file2, with awk: awk '{print $1, $3}' file1 > file2

To output only characters from column 8 to column 15 of file1, with cut: cut -c 8-15 file1 > file2

To replace the word word1 with the word word2 in the file file1, with sed: sed "s/word1/word2/g" file1 > file2

find and execute: find . -name '*.gz' | xargs gunzip -vt

repeat last argument: mkdir /tmp/tmp cd !$

Epoch Date conversion: date -d @1234567890

perl: perl -le 'print scalar localtime $ARGV[0]' 1234567890

printf: printf '%(%F %T)T\n' 1234567890

log: tail -f /var/log/nagios/nagios.log | perl -pe 's/(\d+)/localtime($1)/e' Memory usage: ps -e -orss=,args= | sort -b -k1,1n | pr -TW$COLUMNS ps aux --sort '%mem' ps -e -orss=,args= | sort -nr | head

 

Watch output to file: while true do watch -n 60 <command> 2>&1 | tee -a logfile done

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.
Powered by Zendesk