Solutions
Exercice 1
In a directory, create the following files:
annee1 Annee2 annee4 annee45 annee41 annee510 annee_saucisse bananeCreate a
yeardirectory and move all the previous files in theyeardirectory.List all the files that:
- end by 5
- start by
annee4 - start by
annee4and has 7 letters maximum - start by
anneewith no numerical number - contains
ana - start by
aorA
Copy the files whose second-to-last character is either 4 or 1 in the /tmp directory in a single command
Solution 1
$ touch annee1 Annee2 annee4 annee45 annee41 annee510 annee_saucisse banane
$ mkdir year
$ mv [aA]nnee* ./year
ls *5ls annee4*ls annee4?ls annee[^0-9]*ls *ana*ls [aA]*
cp *[41]? /tmp
Exercice 2 - Grep
- Create a directory called
essai-grepin your home directory. In this directory, create the following files:
tomate poire pomme cerise Fraise fraise courgette POMME3 afraise
Edit the files (output of the ‘ls’ command redirected to ‘grep’) with the following criteria based on their names:
- Criterion 1: The name must be “Fraise” or “fraise”.
- Criterion 2: “se” is at the end of the name.
- Criterion 3: “ai” is present in the name.
- Criterion 4: Name contains a numeric digit.
- Criterion 5: Name contains the string “mm” or “MM”.
Solution Exercice 2 - Grep
mkdir ~/essai-grep
cd ~/essai-grep
touch tomate poire pomme cerise Fraise fraise courgette POMME3 afraise
- Criterion 1:
ls | grep "^[fF]raise$" - Criterion 2:
ls | grep "se$" - Criterion 3:
ls | grep "ai" - Criterion 4:
ls | grep "[0-9]" - Criterion 5:
ls | grep "[mM]\{2\}"
Exercice 3 - Find
Search for files in the entire directory tree:
Whose names end with “.c” and redirect errors to the /dev/null file.
Starting with X or x.
Whose names do not contain any digits.
Search for files in the /usr directory whose size exceeds 1MB (2000 blocks of 500KB) and whose permissions are set to 755 (-rwxr-xr-x).
Count the number of files in the entire directory tree that belong to you and have permissions set to 666 (-rw-rw-rw-).
Solution Exercice 3 - Find
find / -name "*.c" -print 2>/dev/nullfind / -name "[Xx]*" -print 2>/dev/nullfind / -type f -name "*[^0-9]*" -print 2>/dev/null
find /usr -type f -size +2000k -perm 755 -print 2>/dev/nullfind / -user <USER> -perm 666 -type f -print | wc -l
Exercice 4
Create 10 files
.tar.gzin one lineWrite a script that renames the
*.tar.gzto*.tar.gz.old
Solution Exercice 4
for num in {0..9} ; do touch fichier$num.tar.gz ; done
#!/bin/bash
for x in ./*.tar.gz ; do
echo "$x -> $x.old"
mv "$x" "$x.old"
done
exit