Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Linux tips and tricks

System conguration
Bash shotcuts l Bash command output and status l Bash script l Set system time l ls aliases l File
permission l Process limitation l Command line mail l Fuser l pkill l System bell l Computer startup
l System log l Network confguration l Wireless network l Fat32 l xinitrc, Xclients and xsession l
Collect hardware informations l yum / rpm l rescue and recovery l decimal separator l PBS /
TORQUE l 32 / 64 bits l
FiIe renaming and conversion
File renaming l Replace space in flename l Convert pdf to png l Convert pdf to booklet l Print .pdf
with several page per output page (pdfnup) l Decrease PDF fle size l Extract pictures from pdf l
Convert eps to png l Convert mp3 to wav l Convert ogg to wav l Convert wma to wav l Convert 3gp
to avi l pdfnup l Save video stream to disk l Remove transparent background from png picture l
Misc
Screenshots l Awk l nvidia l CD burning l CD copy l DVD burning l Gtk2 fonts l Nedit l wget l Cron l
Gtklp l SSH port forwarding l SSH login without password l Search and dig l Search and destroy l
Search and list l Search and archive l Search and copy l Wipe and erase hard disk l System
version l
Bash shortcuts
!! recall lhe lasl command
!cmd recall lhe lasl command slarl1n by cmd
!$ lasl arumenl oi lhe lasl command
CTRL + r search 1n lhe lasl commands
CTRL + a ump lo lhe slarl oi lhe l1ne
CTRL + e ump lo lhe end oi lhe l1ne
CTRL + u delele backWard irom cursor
CTRL + k delele iorWard irom cursor
CTRL + W delele lhe Word beiore lhe cursor
Bash command output and status
Store in variables output and status of a bash command
oulpul=${command}
slalus=$?
A command succeeds if status code = 0
Bash script
Automate the use of a program with a bash script:
#! 1b1n1bash
proram > resulls << !BL0C!
param1
param2
Linux tips & tricks http://pierrepoulain.free.fr/tips/tips.html
1 of 11 11/12/2013 12:17 PM
param3
!BL0C!
The tags !BL0C! delineate parameters (param1, param2 and param3) send to proram. Output is send
to resulls fle.
Change system time or date
Defne date and time manually:
dale -s "MM1JJ1AAAA RR.MM.SS"
One can also synchronize to a time server (nlp package), for instance pool.nlp.or or
europe.pool.nlp.or. The confguration of ntp is done in 1elc1nlp.coni. The time synchronization
daemon is loaded via 1elc11n1l.d1nlpd reslarl or 1elc11n1l.d1nlp reslarl
To check if the synchonization is working: nlpq -np, columns 'when' and 'reach' should not be null
or undefned. List of French time servers
Is aIiases
Color accordong to fle types and add '/' after directory names: al1as ls="ls --color=aulo -I"
Sort by modifcation date (last modifed fle last listed): al1as ll="ls -rl"
FiIe permissions
Change fle permissions to rwx: chmod 777 i1le.lesl
Change directory access permissions recursively: i1nd . -lype d -pr1nl0 ] xars -0 chmod +x
B1nary ----- Lo1c --- 0ec1mal
000 -------- {---} ------- 0
001 -------- {--x} ------- 1
010 -------- {-W-} ------- 2
011 -------- {-Wx} ------- 3
100 -------- {r--} ------- 4
101 -------- {r-x} ------- 5
110 -------- {rW-} ------- 6
111 -------- {rWx} ------- 7
choWn to change owner: choWn roup i1le or choWn lo1n.roup i1le or choWn lo1n i1le
chrp to change group
Processus number Iimitation (avoid fork bombs)
Check for limitations: l1m1l To add limitations, append the fle 1elc1pam.d1lo1n with:
sess1on requ1red 1l1b1secur1ly1pam_l1m1ls.so
And edit 1elc1secur1ly1l1m1ls.coni and add:
@users hard nproc 23
@users hard dala 2000
@users hard noi1le 20
@users hard slack 4000
@users hard core 8000
@users hard is1ze 100000
Linux tips & tricks http://pierrepoulain.free.fr/tips/tips.html
2 of 11 11/12/2013 12:17 PM
Command Iine maiI
echo "done" ] ma1l -s "process 1n $R0STNAME done" user@doma1n.com
For a more sophisticated output, one can use a bash script:
#! 1b1n1bash
ma1l -s "error W1lh $uSER" rool@doma1n.com << !messae!
Rello,
There 1s an error W1lh $uSER on $R0STNAME compuler.
Can you i1x 1l ?
By by
!messae!
Wich process uses which Ies?
List processes using the sound card:
1sb1n1iuser -v 1dev1dsp0
List processes using the cdrom drive (usefull for a clean unmount):
1sb1n1iuser -v 1mnl1cdrom
Option -k instead of -v automatically kills processes found.
pkiII, more powerfuII than kiIIaII
Kill frefox: pk1ll i1reiox or also pk1ll i1rei
Stop system beII
From the text consol: sellerm -blenlh 0
From the X windows consol: xsel b oii or xsel -b
In the .bashrc fle, add the line sel bell-slyle none
Load a program after computer startup
On Linux systems, manual additions have to be done in 1elc1rc.d1rc.local or 1elc1rc.local since
this fle is loaded after at the end and after all paths and defnitions are performed.
System Iog
System log is in 1var1lo1messae and can be print on screen by:
color-xlerm -e la1l -i 1var1lo1messae &
or
color-xlerm -ls -eomelry 80x5+45+705 -rv -sb -name "Syslem messaes" -in 5x7 -T "Syslem messaes" -e la1l -
One can also redirect syslog outputs toward a terminal (for instance tty8). Add kern.* 1dev1lly8 in
Linux tips & tricks http://pierrepoulain.free.fr/tips/tips.html
3 of 11 11/12/2013 12:17 PM
1elc1syslo.coni.
Network conguration
1iconi1 . l1sl nelWork 1nleriaces "up" {acl1valed}
1iconi1 -a . l1sl all nelWork 1nleriaces
dhcl1enl elh0 . el an 1p address irom 0RCP ior elh0 1nleriace
1iup elh0 . acl1vale elh0 1nleriace
WireIesse network conguration (wi)
List all network interfaces and label wireless interfaces: 1pWconi1
Scan reachable wif networks: 1Wl1sl elh0 scann1n
Assign Ies from a FAT32 partition to specic user
Modify the fle 1elc1islab with the uid and gid of the user. For instance:
1dev1hda1 1mnl1W1n_c vial 1ocharsel=1so8859-15,codepae=850,umask=0,u1d=501,1d=501 0 0
The uid and gid may be found in 1elc1passWd (frst two numbers of the user line).
Difference between .xinitrc, .XcIients and .xsession
If X windows is launched by slarlx, .x1n1lrc is read frst, and then .Xcl1enls. If X windows is
automatically launched and a user session is opened, .xsess1on is read followed by .Xcl1enls.
Scripts .xsess1on and .x1n1rc can be symbolic links to .Xcl1enls. Whatever script is used, it has to
be executable (chmod u+x i1le).
CoIIect hardware informations
cal 1proc1cpu1nio gives information of the CPU(s) (number of processors, model and fags)
cal 1proc1cpu1nio ]rep processor ]Wc -l outputs the number of cpus.
1sb1n1lspc1 gives information on the PCI bridges
1sb1n1lspc1 ]rep `PCT Express x16` outputs the PCI bridge for video card (PCI Express 16x)
1sb1n1lspc1 ]rep v0A gives info on the video card
lx1nio ]rep 0pen0L gives more info on nVidia video card
cal 1proc1mem1nio ]rep `MemTolal` outputs the total RAM
id1sk -l (as root) lists all connected hard drives (internal and external)
yum / rpm
Install the package lesl: yum 1nslall lesl
Install the package lesl.rpm: rpm -1vh lesl.rpm
Remove the package lesl: yum remove lesl or rpm -e lesl
Linux tips & tricks http://pierrepoulain.free.fr/tips/tips.html
4 of 11 11/12/2013 12:17 PM
List packages available for installation: yum l1sl
List installed packages: yum l1sl 1nslalled or rpm -qa
List all packages (installed and available): yum l1sl all
List updated packages: yum l1sl updales
Search the package lesl: yum l1sl *lesl*
Find if package lesl is installed : yum l1sl 1nslalled lesl or rpm -qa ]rep lesl
List and location of fles contained in the package lesl.rpm: rpm -qlp lesl.rpm
List and location of fles contained in the installed package lesl: rpm -ql lesl
Find which package provides the fle 1palh1lo1i1le: yum Whalprov1des 1palh1lo1i1le or rpm -qi
1palh1lo1i1le
Check the packages to be updated: yum check-updale
Update the package lesl: yum updale lesl
Update the package lesl.rpm: rpm -uvh lesl.rpm
Update all installed packages: yum updale
Clean all installed packages from disk cache: yum clean all
Rescue and recovery
Mount the read-only root partition in read-write mode: mounl -n -o remounl,rW 1
Check disk (fsck) at next reboot: shuldoWn -I noW
Scan for bad blocks on 1dev1hda2 device: badblock -sv 1dev1hda2
Redene decimaI separator
On French operating systems, the comma , is the default decimal separator. To use the dot .
separator instead, declare LC_NuMERTC=C
Either redefne the decimal separator for a given program (for instance xmgrace):
al1as xmrace="LC_NuMERTC=C xmrace"
Either redefne the decimal separator globally (in .bashrc fle):
exporl LC_NuMERTC=C
PBS / TORQUE (cIuster jobs management)
Get list of available nodes
pbsnodes -l iree
Get list of active (working) nodes
pbsnodes -l acl1ve
Get number of available cores (assuming a cluster of 13 nodes x 16 cores)
qslal -n ] sed `s1node1node\n1` ] rep -c node ] aWk `{pr1nl 13*16 - $1)`
32 / 64 bits
Linux tips & tricks http://pierrepoulain.free.fr/tips/tips.html
5 of 11 11/12/2013 12:17 PM
Find if your system is 32 or 64 bits:
uname -m
Find if a binary is compiled for 32 or 64 bits:
i1le -be eli 1usr1b1n1ned1l
answer could be
ELI 64-b1l LSB execulable, x86-64, vers1on 1 {SYSv}
or
ELI 32-b1l LSB execulable, Tnlel 80386, vers1on 1 {SYSv}
Batch Ie renaming
Rename all .txt fles from the current directory into .dat
ior 1 1n *.lxl, do mv $1 ${17lxl)dal, done
or
rename `s1\.lxl$1\.dal1` *.lxl
RepIace spaces by _ in Ie names
ior 1 1n *.dal, do mv "$1" `echo $1 ] lr ` ` `_``, done
Convert the rst page of a .pdf document into .png image
pdiloppm i1le.pdi pae
converl pae-000001.ppm pae.pn
rm pae-000001.ppm
Convert a .pdf Ie to a bookIet
pdi2ps -sPAPERSTZE=a4 i1le.pdi i1le.ps
psbook i1le.ps ] psnup -2 ] a2ps -1 -s lumble -o booklel.ps
Print on both side with the option "long edge (standard)". If the original document is not in the A4
format (for instance 14,88 cm x 20,98 cm), we have to do then:
pdi2ps i1le.pdi i1le.ps
psbook i1le.ps ] psnup -2 -W18.88cm -R20.98cm -pa4 ] a2ps -1 -s lumble -o booklel.ps
Print .pdf with severaI page per output page (pdfnup)
pdinup 1npul.pdi --nup 1x3 --irame lrue --scale 0.96
pdinup 1npul.pdi --nup 1x3 --irame lrue --scale 0.96 --ouli1le oulpul.pdi
Linux tips & tricks http://pierrepoulain.free.fr/tips/tips.html
6 of 11 11/12/2013 12:17 PM
Decrease PDF Ie size (with gs)
s -s0EvTCE=pdiWr1le -dCompal1b1l1lyLevel=1.4 -dN0PAuSE -dBATCR -s0ulpulI1le=oulpul.pdi 1npul.pdi
Verbosity can be decreased with the -duTET option
Extract pictures from .pdf Ie
With the packages xpdi-ul1ls and 1maema1k
pdi1maes i1le.pdi p1clure
ior 1 1n p1clure-*.ppm, do converl $1 ${17ppm)pn, done
Convert .eps or .ps Ie to .png
pslopnm -xborder=0 -yborder=0 -xs1ze=neW-s1ze-x -ys1ze=neW-s1ze-y -sldoul i1le.eps > i1le.ppm
pnmlopn i1le.ppm > i1le.pn
rm -i i1le.ppm
or
mor1iy -dens1ly 120 -rolale 90 -iormal pn i1le.ps
Convert .mp3 to .wav
ior 1 1n *.mp3, do lame --decode $1 `basename $1 .mp3`.Wav, done
Convert .ogg to .wav
ior 1 1n *.o , do o123 -d Wav -i `basename $1 .o`.Wav $1, done
Convert .wma to .wav
mplayer i1le.Wma -ao pcm -aoi1le i1le.Wav
or
mplayer -vo null -vc dummy -ai resample=44100 -ao pcm -Waveheader i1le.Wma -aoi1le i1le.Wav
Convert .3gp movies to .avi
.3gp fles are movies from mobile phone. To convert them into .avi fles:
iimpe -1 1npul.3p -i av1 -vcodec xv1d -acodec mp3 oulpul.av1
or with more advanced option :
iimpe -1 v1deo.3p -b 250 -s 160120 -r 15 -i av1 -an v1deo.av1
Convert .pdf to .pdf with 4 pages per page
Linux tips & tricks http://pierrepoulain.free.fr/tips/tips.html
7 of 11 11/12/2013 12:17 PM
pdinup lesl.pdi --nup 2x2 --or1enl landscape --irame lrue
pdinup is provided with the package pdiam.
Save to disk a video stream
mplayer -dumpslream -dumpi1le lesl.Wmv -nocache mms.11slream-address.Wmv
or
mencoder mms.11slream-address.Wmv -oac copy -ovc copy -o lolo.av1
Remove transparent background from png picture
converl -backround Wh1le -ilallen p1c.pn p1c_Wh1le.pn
Sum the numbers of a given coIumn with awk
With a fle containing:
TeslA 3 11
TeslB 26 45
TeslA 7 87
TeslB 18 14
To sum numbers of the second column of all lines starting by "TestA":
aWk `1TeslA1 {sum+=$2, pr1nl sum)` i1le
To print numbers of the second column and their sum:
aWk `1TeslA1 {sum+=$2, pr1nli "7d 7d\n", $2, sum)` i1le
NVidia drivers setup
type CTRL+ALT+I1 to open a text terminal 1.
connect to root 2.
type 1n1l 3 3.
type sh NvT0TA-L1nuxxxxxxx.run to load the settup utility 4.
open 1elc1X111XI86coni1-4 and replace nv by nv1d1a 5.
type 1n1l 5 6.
CD burning
With 1dev1cdrom defned as CD-ROM drive device
Burn .iso fle: cdrecord dev=1dev1cdrom speed=8 -v i1le.1so
Clean (blanck) CD-RW: cdrecord dev=1dev1cdrW blanck=iasl
CD copy
mk1sois, then cdrecord
Linux tips & tricks http://pierrepoulain.free.fr/tips/tips.html
8 of 11 11/12/2013 12:17 PM
or
dd 1i=1dev1cdrom oi=1mae.1so
or
cdda2Wav -0 1dev1cdrom -s -0 Wav -S 1 -B
cdrecord -v is=6m speed=10 dev=0,0,0 -aud1o *.Wav
DVD burning
With 1dev1dvd the DVD-ROM drive.
Files:
roW1sois -dvd-compal -speed=2 -Z 1dev1dvd -R -J i1les
Iso image:
roW1sois -dvd-compal -speed=2 -Z 1dev1dvd=i1le.1so
Directory content:
roW1sois -dvd-compal -speed=2 -Z 1dev1dvd -R -J d1reclory_name
One or several directories (with their directory name):
roW1sois -dvd-compal -speed=2 -Z 1dev1dvd -R -J -rail-po1nls 1d1r1=1palh1lo1d1r1 1d1r2=
1palh1lo1d1r2
Clean DVD:
dvd+rW-iormal -iorce 1dev1dvd
Note: is flenames are too long, use the -ol1el-lon option instead of -J
Gtk2 fonts
Create the fle .lkrc-2.0 in the home directory and append the line
lk-ionl-name = "Ar1al 11"
Nedit scripts
Scripts have to be Unix type fles (and not Dos type).
Wget
Download of a directory recursively: Wel -r -np url
Contrab
List existing crontab: cronlab -l
Edit contrab: conlrab -e
Automatically append a new job to crontab:
cronlab -l > old.cron
echo "# add neW cron ob al 3.00 AM" > append.cron
echo "0 3 * * * scr1pl.sh >& 1lmp1scr1pl.lo" >> append.cron
cal old.cron append.cron > neW.cron
cronlab neW.cron
Screenshots
Linux tips & tricks http://pierrepoulain.free.fr/tips/tips.html
9 of 11 11/12/2013 12:17 PM
1mporl -W1ndoW rool screenshol.p or 1mporl screenshol.p
GtkIp
gtklp is a Gtk-interface above cups that may be used on its own or via other software. The
confguration of each printer is written in ~1.cups1lpopl1ons.
The set up of applications such as frefox, openoffce.org or acrobat reader to use gtklp is
explained here (FR): http://doc.ubuntu-fr.org/gtklp
SSH port forwarding
Build a SSH connection directly from computerA to computerC via serverB (computerA - serverB
- computerC):
ssh -L 2222.compulerC.22 lo1n@serverB
And in a different shell:
ssh -X localhosl -p 2222
The local port (2222 here) must be > to 1024 since port < 1024 are reserved to root.
SSH Iogin without password
To perform SSH login without password from computerA (user loginA) on computerB (user loginB).
First, create a public/private key pair on computer A:
ssh-keyen -l rsa
Accept all default choices and leave an empty passphrase.
Then, append the public key of computerA to the authorized keys of computerB:
ssh-copy-1d -1 ~1.ssh11d_rsa.pub compulerB
Enter one last time your password of userB on computerB to accept the copy.
You can now connect via SSH from computerA to computerB without any password.
Search and dig
Search for text fles (*.lxl) and search into:
i1nd .1palh1 -name `*.lxl` ] xars rep `slr1n`
Search and destroy
Search text fles (*.lxl) and remove:
i1nd .1palh1 -name `*.lxl` -exec rm {) \,
Search and Iist
Linux tips & tricks http://pierrepoulain.free.fr/tips/tips.html
10 of 11 11/12/2013 12:17 PM

You might also like