Linux / Unix‎ > ‎


Dupicate a debian install with dpkg options

# on originate system : 
debconf-get-selections --installer > server.selections
debconf-get-selections >> server.selections
dpkg -l | awk '{print $1}' >  server.dpkg
# on target system
debconf-set-selections server.selections
apt-get install -y $(grep -vE "^\$*#" server.dpkg | tr "\n" " ")

Delete files from their inode value (filename with special chars)

ls -il .
rm -i xxxxx

Prevent BIPs on tab completion

add this line at end of file /etc/inputrc
set show-all-if-ambiguous on

Intelligent tab completion

uncomment line . /etc/bash_completion in file /etc/profile (or in your .bashrc)

shell (bash/sh/tcsh/etc...) usefull and common options

  • -v --> verbose (show commands before executing them)
  • -e --> stop on error
3 ways to activate these options :
  #!/bin/bash -v -e someScript 
  #!/bin/bash -v -e 
  #!/bin/bash set -v set -e  


To check which locale you currently have as your default just run: localeChanging the default locale is a little different on Ubuntu compared to most Linux distros, these are the steps we needed to go through to get it changed:Add the locale to the list of 'supported locales' Edit /var/lib/locales/supported.d/local and add the following line: fr_FR ISO-8859-1Regenerate the supported locales
  • Run sudo dpkg-reconfigure locales
Change the default locale Edit /etc/environment and ensure the LANG and LANGUAGE lines read as follows: LANG="fr_FR" LANGUAGE="fr_FR:fr"Reboot!

Keyboard / terminal

  • switch to terminal n
    chvt _n_  
  • switch to fr keyboad
     loadkeys fr  

Hot add SCSI disk

   # echo "- - - " > /sys/class/scsi_host/host1/scan

Hot add swap

   # dd if=/dev/zero of=/var/file.swap bs=128M count=1
   # sync
   # mkswap -c /var/file.swap
   # chmod -v 600 /var/file.swap
   # swapon /var/file.swap


List by size

du /var -akhx  | sort -nr |less

ACL exemple

list opened files


Find incorrect CVS archives and delete them

Find and replace

find . -type f -exec perl -pi.bak -e "s%^# (store-passwords = no)#$1%g"  {} \;
Incorrect CVS (RCS) archives are 79/80 bytes long, head is undefined.
find /data/cvs -type f -name '*,v' -size -81c -exec egrep -l 'head     ;' {} \; | xargs -i rm {}

Find files modified in last 10 minutes :

find .  -type f -cmin -10 

Find files containing some text

find . -type f | xargs -i grep -l some_text {}


Create crypted tgz

create a key file (text file containing your secret key),
tar -cvzf - someDir 2>/var/log/someLogFile  | ccrypt -e -k myKeyFile myCrypted.tgz.cpt 
to decrypt, run
crypt -d -k  myKeyFile myCrypted.tgz.cpt

Back-up / Retsore MBR

sfdisk -d /dev/hda > mbr-hda.sfdisk
sfdisk /dev/hda < backup-hda.sfdisk 
can also use dd but sfdisk make it more readable and preserve MBR boot loader codesee DiskDisasterRecoveryTools?

Copy files via TAR + pipe + ssh

tar -czf - somePath | ssh user@host "cat > toto.tgz"
ssh root@machineName tar -cpzf - -C /some/path someDir | tar -xzf - -C /some/other/path 


tar -czf - /some/file | ssh tar -xzf - -C /destination

Backup / Restore disk

Backup / Restore disk partition using dd

backup hda before doing backup, you can zero unused blocks to make gzip file smaller :
dd if=/dev/zero of=delme bs=8M; rm delme
dd if=/dev/hda1 conv=sync,noerror bs=512 | gzip -c  > /somwhere/hda1.img.gz
restore hda
gzip -x /somwhere/hda1.img.gz | dd of=/dev/hda1 conv=sync,noerror bs=512

Backup / Restore disk partition using dd via ssh

dd if=/dev/hda1  conv=sync,noerror bs=512 | rsh user@dest "gzip -9 >20290117-backup-hda1.img.gz"
ssh user@dest "gzip -x 20290117-backup-hda1.img.gz" | dd of=/dev/hda1  conv=sync,noerror bs=512

syncrhonise dirs using rsync

for dir in /data/cvs/*; do
  rsync -nauv --delete somHost:$dir/ $dir/

copy dir structure using rsync

rsync -av   -f"+ */" -f"- *"  

backup disk using rsync

# rsync --progress --stats -avxzl --exclude "/mnt/usbharddrivemain/" --exclude "/mnt/usbharddriveboot/" --exclude "/usr/portage/" --exclude "/proc/"
--exclude "/root/.ccache/" --exclude "/var/log/" --exclude "/sys" --exclude "/dev" --exclude "tmp/" /* /mnt/usbharddrivemain

# rsync --progress --stats --delete -avxzl --exclude "/mnt/usbharddrivemain/" --exclude "/mnt/usbharddriveboot/" --exclude "/usr/portage/"
--exclude "/proc/" --exclude "/root/.ccache/" --exclude "/var/log/" --exclude "/sys" --exclude "/dev" --exclude "tmp/" /* /mnt/usbharddrivemain

syncrhonise dirs using rsync over ssh

rsync -avz -e ssh remoteuser@remotehost:/remote/dir /this/dir/ 


mysql / PMB

  • suppression base de données (pmbtest) :
    mysqladmin drop pmbtest
  • creation de la base de données pmbtest :
mysql> create database pmbtest character set utf8;
mysql> GRANT ALL PRIVILEGES ON pmbtest.* TO 'pmb'@localhost identified by 'pmb-password';
mysql> flush privileges;
mysql> quit
  • init tables :
cd /data/pmb/tables
mysql library <empty_example_set.sql

GRUB :splash

# generation du splash a partir d'une image 16 couleurs
pngtobogl parkeon-splash.png > parkeon-splash.c
gcc -Os -g -I/usr/include/bogl -fPIC -c parkeon-splash.c -o parkeon-splash.o
gcc -shared -Wl,-soname, parkeon-splash.o -o

#extraction de initrd qui est sur le cdrom
mkdir afac && cd afac
cat ../ubuntu-livecd/casper/initrd.gz | gzip -d | cpio -i

# modifier initrd ... puis le reconstruire :
cp /someWhere/ usr/lib/usplash/

#on recree initrd.gz
find | cpio -H newc -o | gzip >../ubuntu-livecd/casper/initrd.gz

Command line resize

  1. umount partition : umount /dev/sdc1
  2. delete / recreate partition : fdisk
  3. force reread partition table : partprobe
  4. check fs (mandatory) : e2fsck -f /dev/sdc1
  5. resize fs to max free : resize2fs /dev/sdc1
  6. re-mount : mount /dev/sdc1


  • umount -l /dev/sdb1
  • losf /dev/sdb1
  • fuse -m /dev/sdb1

Remove Esc chars

perl -ne 's/\x1b[[()=][;?0-9]*[0-9A-Za-z]?//g;s/\r//g;s/\007//g;print'