shell

At

Commande

1
2
# atq
13 Mon Jun 30 19:53:00 2014 a jerem

Liste les taches plannifiées

1
atrm 13

Efface une tache

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
at 17:33
at now + 4 min
at now + 4 minutes
at now + 2 hours
at 10pm + 3 days
at 10pm + 3 months
at 10am Jul 31
at 10am tomorrow
at midnight
at noon
at teatime (16h)
...
at> echo "toto" > /tmp/zob
at> echo "pouya" >> /tmp/zob
at> <EOT>
job 13 at Mon Jun 30 19:53:00 2014

at + la date souhaitée
ensuite, une liste de commande (enter entre chaque)
et “Ctrl-d” pour sortir

Shell tips

Tips en vrac

Minuscule à majuscule :

1
2
echo "EEdddzZ" | tr '[:lower:]' '[:upper:]' 
EEDDDZZ

Supprime les sauts de ligne

1
2
tr -d '\r\n'
awk '!x[$0]++' truc > zob

Demande de pass interactive :

1
echo -n Password: ; read -s pass ; for i in ` ls access.log*` ; do echo $pass | scp --stdin access.log* root@10.0.0.1:/toto ; done

Affiche sans les commentaires

1
sed -e '/^[ ]*#/d' -e '/^$/d'

exec a partir de awk

1
cat /etc/hosts  | awk '{ system("echo " $2" "$1)}'

Append 2 files in columns

1
paste -d " " fileliste1 fileliste2

Dos2unix

1
$ cat mon_fichier.txt | tr -d '\r' > mon_nouveau_fichier.txt

Kill un process zombi

1
gdb -p <zombi_pid>

Affiche de 1 à 5

1
for i in {1..5} ; do echo $i ; done

Compare 2 lists

1
$ comm list1.txt list2.txt

Chmod +x only directory

1
$ chmod -R +X dir/

Random sleep max 10s

1
sleep $(( RANDOM % 10 ))

Proxy tcp

1
mkfifo /tmp/fifo.socket
nc -lk -p 8080 </tmp/fifo.socket | nc www.unvrai.info 80 >/tmp/fifo.socket

Close shell keeping all subprocess running

1
disown -a && exit

Edit a file on a remote host using vim

1
vim scp://user@host//etc/fstab

Find duplicate files (based on MD5 hash)

1
find -type f -exec md5sum '{}' ';' | sort | uniq --all-repeated=separate -w 33

Show current working directory of a process

1
pwdx <pid>

Limit the cpu usage of a process

1
cpulimit -p pid -l 50

Start a command on only one CPU core

1
taskset -c 0 <command>

Find which directory in one filesystem that contains most inodes or files.

1
find / -xdev -printf '%h\n' | sort | uniq -c | sort -k 1 -n | tail                                                                                                                                                                        │

bash shortcut

Ctrl + a Go to the beginning of the line (Home)
Ctrl + e Go to the End of the line (End)
Ctrl + p Previous command (Up arrow)
Ctrl + n Next command (Down arrow)
Alt + b Back (left) one word
Alt + f Forward (right) one word
Ctrl + f Forward one character
Ctrl + b Backward one character
Ctrl + xx Toggle between the start of line and current cursor position

Editing:
Ctrl + L Clear the Screen, similar to the clear command

Alt + Del Delete the Word before the cursor.
Alt + d Delete the Word after the cursor.
Ctrl + d Delete character under the cursor
Ctrl + h Delete character before the cursor (Backspace)

Ctrl + w Cut the Word before the cursor to the clipboard.
Ctrl + k Cut the Line after the cursor to the clipboard.
Ctrl + u Cut/delete the Line before the cursor to the clipboard.

Alt + t Swap current word with previous
Ctrl + t Swap the last two characters before the cursor (typo).
Esc + t Swap the last two words before the cursor.

ctrl + y Paste the last thing to be cut (yank)
Alt + u UPPER capitalize every character from the cursor to the end of the current word.
Alt + l Lower the case of every character from the cursor to the end of the current word.
Alt + c Capitalize the character under the cursor and move to the end of the word.
Alt + r Cancel the changes and put back the line as it was in the history (revert).
ctrl + _ Undo

Ctrl+I = Tab
Ctrl+J = Newline
Ctrl+M = Enter
Ctrl+[ = Escape

Ctrl + r Recall the last command including the specified character(s)
searches the command history as you type.
Equivalent to : vim ~/.bash_history.
Ctrl + p Previous command in history (i.e. walk back through the command history)
Ctrl + n Next command in history (i.e. walk forward through the command history)

Ctrl + s Go back to the next most recent command.
(beware to not execute it from a terminal because this will also launch its XOFF).
Ctrl + o Execute the command found via Ctrl+r or Ctrl+s
Ctrl + g Escape from history searching mode
!! Repeat last command
!abc Run last command starting with abc
!abc:p Print last command starting with abc
!$ Last argument of previous command
ALT + . Last argument of previous command
!* All arguments of previous command
^abc­^­def Run previous command, replacing abc with def

Ctrl + C Interrupt/Kill whatever you are running (SIGINT)
Ctrl + l Clear the screen
Ctrl + s Stop output to the screen (for long running verbose commands)
Then use PgUp/PgDn for navigation
Ctrl + q Allow output to the screen (if previously stopped using command above)
Ctrl + D Send an EOF marker, unless disabled by an option, this will close the current shell (EXIT)
Ctrl + Z Send the signal SIGTSTP to the current task, which suspends it.
To return to it later enter fg ‘process name’ (foreground).

Sed

TIPS

Afficher les lignes de 1 à 10 ou de 1 à la fin

1
2
sed -n 1,10p  zob
sed -n '1,$p' zob

Suppression de la 3ème ligne

1
sed '3d' mon_fichier.txt

Suppression de la ligne contenant la chaîne “awk”

1
sed '/awk/d' mon_fichier.txt

Suppression de la dernière ligne

1
sed '$d' mon_fichier.txt

Suppression de toutes les lignes vides

1
2
sed '/^$/d' mon_fichier.txt
sed '/./!d' mon_fichier.txt

Remplacement

1
sed -i 's/GROS/PETIT/g' zob

Insert line at specific line

1
sed -i '13i la ligne apres 12' plop

Insert before pattern

1
sed '/^truc/i before' test.txt

Insert after pattern

1
sed '/^truc/a after' test.txt

Suppression des lignes commentées d’un fichier

1
sed -e '/^[ ]*#/d' -e '/^$/d'

Comment

1
sed -i 's/^/#/' plopfile

uncomment

1
sed -i 's/^#//' plopfile