Terminal Commands

From Notepedia
Jump to: navigation, search

Find command

# find ./ -mtime +5 -exec rm {} \;

Finds files are older than 5 days, change to -5 to find newer than 5 days

# find ./ -group firebird -a ! -user wwwrun

Finds all files with the group of firebird AND the user NOT wwwrun

Find biggest directories

du -sk * | sort -nr | head

Disk Destroyer

Simple command, if (input file), of (output file), bs (block size)

# dd if=/dev/sda of=/dev/sdb bs=1024k

SSH Tunneling

ssh -p 2200 -f [email protected] -L 2000:localhost:9091 -N
  • -p Determines which port ssh is on (if it's not on default 22)
  • -f tells ssh to go into the background just before it executes the command.
  • -L 2000:localhost:9091 is in the form of -L local-port:host:remote-port. If tunnelling to another host inside the tunnel is required then change "localhost".
  • -N instructs OpenSSH to not execute a command on the remote system.

The service running on the remote-host on port 9091 can now be accessed on localhost on port 2000

Secure copy through a tunnel

ssh -N -f -L 2123:<target>:22 repo.com.au

Then test:

ssh -p 2123 [email protected]

and execute:

scp -P 2123 [email protected]:/var/lib/firebird/backup/20100820.1200.hourly.fbk* .

Rsync

rsync -aumvv --delete /my/music/library/* 192.168.1.5:/backup/of/music/library/

that way i tell rsync to sync via ssh. You need an ssh server running on the target machine (192.168.1.5 in my case) and have rsync installed on both machines.

Here's an explanation if the options:

  • -a: List item archive all files (include options rlptgoD)
  • -u: update existing files, don't copy them again if they are already in place
  • -m: prune empty dirs
  • --delete: delete files on target which were deleted on source

inherited through flag -a:

  • -r: recurse through subdirs
  • -l: preserve symlinks as symlinks
  • -p: preserve permissions
  • -t: preserve modification time
  • -g: preserve group
  • -o: preserve owner
  • -D: preserve device and special files

SSH execute commands remotely

Using the "-t" flag

ssh -p 2200 [email protected] -t "grep vim /etc/bash.bashrc"

AWK

http://www.thegeekstuff.com/2010/01/awk-introduction-tutorial-7-awk-print-examples/

AWK like GREP: awk '/:/ { print $0 }' /etc/group

Print specific fields - using " " white space as a delimiter: awk '{print $2,$5;}' employee.txt

Print a specific field using ":" as a delimiter: awk -F ":" '{print $4}' /etc/group

SED

The -i flag deletes the file in place (requires GNU sed) The /<string>/d command deletes the line with the search string s/2000/2012/g will search and replace 2000 with 2012

sed -i '/vim/d' /etc/bash.bashrc"

Failing that on OSX the following works with the -e switch:

sed -ie '/vim/d' /etc/bash.bashrc"

Execute Sudo commands in one line

use the echo command and -S flag:

echo <password> | sudo -S vim /etc/rc.local

mdadm software RAID

Creating the RAID:

mdadm --create --verbose /dev/md0 --level=5 --raid-devices=3 /dev/sdc /dev/sdd /dev/sde

cat /proc/mdstat

Enable the RAID

mdadm --assemble --scan

LVM

Logic volume manager:

first run cfdisk and convert to LVM partitions, then

lvm
pvcreate /dev/sdaX /dev/sdaY
vgcreate davavg /dev/sdaX /dev/sdaY 
lvcreate --name downloads --size 40G datavg

to maximise all space in the that VG in just one LV use the following command:

lvm> lvcreate --name backup -l 100%FREE datavg

This will write the file system to the LVM partiton:

mkfs.ext4 /dev/datavg/downloads

edit fstab and mount the partitons mount -a, done!

To Disable and enable LVM Volume Groups

Best done when pulling out the drives etc... doe this *after* unmounting

To disable Volume Groups (Optional Name, otherwise it will disable all VG's)

/sbin/vgchange -a n <Optional_VG_Name>

To enable them again:

/sbin/vgchange -a y <Optional_VG_Name>

To Disable and enable Logic Volumes

  • first* make sure you unount

List the Logic Volumes to see what's happening

lvdisplay


To disable Volume Groups (Optional Name, otherwise it will disable all VG's)

lvchange -a n <LV_Name>

To enable them again:

lvchange -a y <LV_Name>


To Extend the LV

First see how much room in the PV you have to play with, this shows 598 Gigs free in the PV unused

pvs
  PV         VG     Fmt  Attr PSize PFree  
  /dev/md0   datavg lvm2 a-   2.73T 598.73G

Next give the LV of your choice more room, we give total amounts, you can also use +0.5G to add that much, so this:

lvextend -L1.5G /dev/datavg/downloads

OR, this:

lvextend --size +150G /dev/datavg/video 
  Extending logical volume video to 1.99 TB
  Logical volume video successfully resize

Unmount

umount /dev/datavg/downloads

Disk check

e2fsck -f /dev/datavg/downloads (for ext2/3/4)

Grow disk

resize2fs /dev/datavg/downloads

Remount

mount -a

Different for xfs which can be done

xfs_growfs /location/of/partition

excellent How To:
http://www.howtoforge.com/linux_lvm
http://tldp.org/HOWTO/LVM-HOWTO/
http://tldp.org/HOWTO/LVM-HOWTO/extendlv.html

This how to combines LVM and mdadm:
http://linuxdevcenter.com/pub/a/linux/2006/04/27/managing-disk-space-with-lvm.html