Python basics
  • Tutorial
  • Exercices
  • About
  1. CLI
  2. Solutions
  • Introduction
    • Python
  • Fundamentals
    • 0-Basic Syntax
    • 1-Variables
    • 2-Data types
    • 3-Numbers
    • 4-Strings
    • 5-Lists
    • 6-Booleans
    • 7-Loops
    • 8-Functions
    • 9-Dictionary
  • Advanced
    • 10-Numpy
    • 11-Matplotlib
    • 12-DataFrame
  • CLI
    • Exercices
    • Solutions
  • Git
    • Git introduction
    • Git branch
    • Git exercices
  • SQL
    • SQL Exercices
    • SQL Solutions

On this page

  • Exercice 1
  • Solution 1
  • Exercice 2 - Grep
  • Solution Exercice 2 - Grep
  • Exercice 3 - Find
  • Solution Exercice 3 - Find
  • Exercice 4
  • Solution Exercice 4
  1. CLI
  2. Solutions

Solutions

Exercice 1

  1. In a directory, create the following files: annee1 Annee2 annee4 annee45 annee41 annee510 annee_saucisse banane

  2. Create a year directory and move all the previous files in the year directory.

  3. List all the files that:

    • end by 5
    • start by annee4
    • start by annee4 and has 7 letters maximum
    • start by annee with no numerical number
    • contains ana
    • start by a or A
  4. Copy the files whose second-to-last character is either 4 or 1 in the /tmp directory in a single command

Solution 1

  1. $ touch annee1 Annee2 annee4 annee45 annee41 annee510 annee_saucisse banane

$ mkdir year 
$ mv [aA]nnee* ./year
  • ls *5
  • ls annee4*
  • ls annee4?
  • ls annee[^0-9]*
  • ls *ana*
  • ls [aA]*
  1. cp *[41]? /tmp

Exercice 2 - Grep

  1. Create a directory called essai-grep in 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

  1. 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.

  2. 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).

  3. 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/null
  • find / -name "[Xx]*" -print 2>/dev/null
  • find / -type f -name "*[^0-9]*" -print 2>/dev/null
  1. find /usr -type f -size +2000k -perm 755 -print 2>/dev/null

  2. find / -user <USER> -perm 666 -type f -print | wc -l

Exercice 4

  1. Create 10 files .tar.gz in one line

  2. Write a script that renames the *.tar.gz to *.tar.gz.old

Solution Exercice 4

  1. 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
Exercices
Git introduction
 

Copyright 2023, Clément Bernard