Exercices Chapitre 5 -- 30 exos en ligne de commande


Exercice 1

Donner la liste de tous les comptes et uniquement ceux-là, issus du fichier passwd triés par ordre alphabétique. Le résultat sera mis dans le fichier /tmp/out. Vous utiliserez cut et sort.

cat /etc/passwd | cut -d : -f1 | sort >> /tmp/out
cat /etc/passwd | cut -d : -f1 | sort > /tmp/out && cat /tmp/out


Exercice 2

ls /usr/bin/ | wc -l
ls -l /usr/bin/ | grep '^-' | wc -l
find /usr/bin/ -type f | wc -l


Exercice 3

find /usr/share/doc | grep '\.gz$' | wc -l
find /usr/share/doc -name *.gz | wc -l
find /usr/share/doc -type f  -name "*.gz" | wc -l


Exercice 4

ps aux | grep bash


Exercice 5

cat /etc/passwd | tr '\n' '\r' > /tmp/newpasswd

Cette commande remplace tous les passages à la ligne par un retour chariot dans le fichier /etc/passwd et écrit le résultat dans /tmp/newpasswd le résultat de la commande cat est passé à la commande tr qui va créer une nouvelle ligne avec un retour chariot dans un fichier /tmp/passwd

Exercice 6

sort -n /tmp/test.txt
cat /tmp/test.txt | cut -d / -f 2 > /tmp/newtest.txt
sort -r /tmp/newtest.txt
echo -e '/aa\n/bb\n/cc\n/dd\n/ee' | nl | sed 's/^[ \t]*//' | tr -d [:blank:] | tr -d [:digit:] | tr -d / | sort -r >> ok
echo -e '/aa\n/bb\n/cc\n/dd\n/ee' | nl -n ln | tr -d [:blank:] > /tmp/ok.bak && cat /tmp/ok.bak | tr -d [:digit:] | tr -d / | sort -r > /tmp/ok && cat /tmp/ok


Exercice 7

find /etc -maxdepth 1 -type f | grep '\.conf' | wc -l
grep pas nécessaire : mêmes les fichiers sans extension .conf peuvent être des fichiers de configuration
find /etc/ -maxdepth 1 -type f -prune | wc -l


Exercice 8

find /etc -maxdepth 1 -type d | grep 'conf' | wc -l
grep pas nécessaire
find /etc/ -maxdepth 1 -type d | wc -l


Exercice 9

sudo find . -type f > /tmp/files-list.txt
find . -maxdepth 1 -type f | tr -d /. > /tmp/ok
ls -l | grep ^- >> /tmp/ok


Exercice 10

ls -l | grep '^-' > /tmp/files.list
find . -maxdepth 1 -type f -ls > /tmp/files.list
ls -lh >> ok && sed -i '/^d/d' /tmp/ok
ls -lh | grep ^- >> /tmp/ok


Exercice 11

ls -l | grep '^l'

OU

find . -maxdepth 1 -type l
ls -la | grep ^l


Exercice 12

man ls | grep file -i | wc -l
man ls | grep -i file | wc -w -l


Exercice 13

find /usr/bin -type f -ls | sort -k 7 -rn | head
ls -l /usr/bin/ -S | head -11
find /usr/bin/ -type f | xargs du -s | sort -rn | head
find /usr/bin/ -type f -exec du -s {} \; | sort -rn | head
ls -lSh /usr/bin/ | head -n 11


Exercice 14

grep '^a' regexp/zola_curee.txt -i -c

J’ai remplacé le . par un retour à la ligne, ensuite j’ai viré les espaces de début de ligne, j’ai ensuite compté le nombre de ligne commençant par A

sed  'y/\./\n/' zola_curee.txt | sed 's/^ //' >> ok && sed -n '/^[Aa]/p' ok | wc -l


Exercice 15

grep '[[:digit:]]' regexp/zola_curee.txt -c
sed -n '/[0-9]/p' zola_curee.txt | wc -l


Exercice 16

grep '[[:punct:]]$' regexp/zola_curee.txt | grep -cv '\.$'
sed -n '/[\,\;\:\?\!]$/p' zola_curee.txt  | wc -l


Exercice 17

sed 's/\b/\n/g' regexp/zola_curee.txt | grep '^.r'
grep -o '\b\wr' regexp/zola_curee.txt | wc -l
sed 's/\b/\n/g' regexp/zola_curee.txt | grep -i '^.r' --color=always | sort -u | wc -l
cat zola_curee.txt | grep '\<.r' | wc    -l        247
cat zola_curee.txt | grep -E '(\b.\Br)' | wc -l        247
regex:  groupe  ( )    
boundary word  \b.    
non-word boundary \Br


Exercice 18

sed 's/\b/\n/g' regexp/zola_curee.txt | grep '.r'
sed 's/\b/\n/g' regexp/zola_curee.txt | grep '.r' | wc -l
cat zola_curee.txt | grep -E '(r\w+)' | wc -l


Exercice 19

grep 'the' regexp/texte_01.txt

grep 'the' regexp/texte_01.txt | wc -l
grep -n 'the' regexp/texte_01.txt | cut -d : -f1 | column
cat texte_01.txt | grep -i -o -n 'the' | wc -w
cat texte_01.txt | grep -i -o -n 'the' && cat texte_01.txt | grep -i -o -n 'the' | wc -l


Exercice 20

sed 's/\b/\n/g' regexp/texte_01.txt | grep '^NASA$' | wc -l
grep -i -o 'nasa' texte_01.txt | wc -w


Exercice 21

grep '^$' regexp/texte_01.txt | wc -l
sed -n '/^$/p' texte_01.txt | wc -l


Exercice 22

sed 's/\s/\n/g' regexp/texte_01.txt | grep -E '^[0-9]+$' | column

Tout dépend de ce qu’on cherche exactement…

sed 's/\s/\n/g' regexp/texte_01.txt | grep -E '^[0-9]+$' | sort -nu | wc -l
cat texte_01.txt | grep -P '(\d+)' | wc -l

-P, c’est pour –perl-regexp

Exercice 23

find /usr/bin -type f -executable | cut -d / -f4 | grep -E '^..$'

OU

find /usr/bin -type f -executable | grep -E '^/usr/bin/..$'
ls /usr/bin/ | sed -n '/^..$/p'


Exercice 24

sed -e 's/</\n</g' -e 's/>/>\n/g' regexp/page.html | grep '^<.*>$'
sed -e 's/</\n</g' -e 's/>/>\n/g' regexp/page.html | grep '^<.*>$' | sort -u | wc -l
cat page.html | grep -P -o '(<\w+|\/\w+>)' | tr -d \/ | tr -d \< | tr -d \> | sort -u
cat page.html | grep -P -o '(^<(\w+))' | tr -d \< | sort -u


Exercice 25

grep '<strong>' regexp/page.html | wc -l
sed -n '/<b>/p' page.html        oui le doc contient bien des balises <b><b/>


Exercice 26

sudo find /etc -type f | grep -Ei '\/[a-z]+[^\.\/]*[a-z]+[^\.\/]*[a-z]+[^\.\/]*[a-z]+\.[^\/]*$'
sudo ls -R /etc/ | sed '/.{4,}\..{3}/p' | more


Exercice 27

sudo apt install rename
rename 's/\.test/.old/' *.test
find ~ -name "*.test" -type f -exec bash -c "mv '{}' \`dirname '{}'\`/\`basename '{}' .test\`.old" \;


Exercice 28

find . -maxdepth 1 -type f | while IFS= read -r f; do f="$(basename $f)" f="$(sed 's/[^a-z]//g' <<< "$f")"; f="${f:0:3}"; echo ">> $f matching '/[$f]{3,}.*$'"; find /etc/ -maxdepth 1 -type f | grep -E --color=always "/.*[$f]{3,}.*$"; done
ls | cut -c 1-3 | uniq | xargs -I letters find /etc -maxdepth 1 -name '*letters*'

xargs va faire une boucles

ls | cut -c 1-3 | uniq | xargs -I lettres find /etc/ -maxdepth 1 -name '*lettres*'


Exercice 29

sudo find . -type f -mmin -10 find ~ -type f -mmin -10


Exercice 30

sudo find . -type f -ctime -1
sudo find . -type f -ctime -2
sudo find . -type f -ctime -7

→ mtime = modification des datas → ctime = modification du status

http://www.expreg.com/symbole.php

echo -e "\nToday\n*****"; find ~ -type f -mtime -1 && echo -e "\nHier\n****"; find ~ -type f -mtime +1 && echo -e "\n7Jours\n*******"; find ~ -type f -mtime +6

Ubuntu Serveur