logo

Bash Scripting: com comprovar si el fitxer existeix

En aquest article, escriurem un script bash per comprovar si els fitxers existeixen o no.

Sintaxi:

  • prova [expressió]
  • [expressió]
  • [[ expressió ]]

Aquí, en expressió, escrivim paràmetre i nom de l'arxiu . Vegem alguns paràmetres que es poden utilitzar en l'expressió:

  • f: Retorna True si el fitxer existeix com a fitxer comú (normal).
  • -d : retorna True si el directori existeix. -e : retorna True si existeix algun tipus de fitxer. -c : retorna True si el fitxer de caràcters existeix. -r : retorna True si existeix un fitxer llegible.
  • En : retorna True si existeix un fitxer que es pot escriure .
  • -x : retorna True si existeix un fitxer executable. -p : retorna True si el fitxer existeix com a canalització. -S : retorna True si el fitxer existeix com a sòcol. -s : retorna True si existeix un fitxer i la mida del fitxer no és zero. -L : retorna True si el fitxer d'enllaç simbòlic existeix . -g : retorna True si el fitxer existeix i manté la marca d'identificació de grup establerta.. -G : jo t retorna True si el fitxer existeix i conté el mateix identificador de grup que està en procés. -k : retorna True si el fitxer existeix i el senyalador de bits enganxosos està establert.

Ara, hi ha alguns paràmetres més per a la comparació entre els dos fitxers.



    -ef: retorna True si els dos fitxers existeixen i indiquen el mateix fitxer.

Exemple:

FirstFile -ef SecondFile>
    -nt: retorna True si FirstFile és més recent que Secondfile.

Exemple:

notes al peu de reducció
FirstFile -nt FileOld>
    -ot: retorna True si FirstFile és més antic que SecondFile.

Exemple:

FirstFile -ot SecondFile>

Prenguem alguns exemples basats en la sintaxi:

    [ expressió ]: primer, creeu un fitxer anomenat FirstFile.sh i escriviu-hi l'script següent
#!/bin/bash # using [ expression ] syntax and in place # of File.txt you can write your file name if [ -f 'File.txt' ]; then # if file exist the it will be printed echo 'File is exist' else # is it is not exist then it will be printed echo 'File is not exist' fi>

Ara deseu i executeu el fitxer amb l'ordre següent

$ chmod +x ./FirstFile.sh $ ./FirstFile.sh>

Sortida:

Sortida

Nota: Com que el fitxer File.txt està present al sistema. Per tant, el fitxer imprès existeix.

    prova [expressió]: ara, modifiqueu l'script anterior a FirstFile.sh de la manera següent
#!/bin/bash # using test expression syntax and in place # of File2.txt you can write your file name if test -f 'File2.txt' ; then # if file exist the it will be printed echo 'File is exist' else # is it is not exist then it will be printed echo 'File is not exist' fi>

Ara, torneu a desar i executar el fitxer amb l'ordre següent

$ chmod +x ./FirstFile.sh $ ./FirstFile.sh>

Sortida:

mysql mostra tots els usuaris

Sortida

Nota: Com que el fitxer File2.txt no està present al sistema. Per tant, el fitxer imprès no existeix.

    [[ expressió ]]: torneu a modificar l'script anterior a FirstFile.sh de la manera següent
#!/bin/bash # using [[ expression ]] syntax and in place # of File3.txt you can write your file name if test -f 'File3.txt' ; then # if file exist the it will be printed echo 'File is exist' else # is it is not exist then it will be printed echo 'File is not exist' fi>

Ara, torneu a desar i executar el fitxer amb l'ordre següent

 $ chmod +x ./FirstFile.sh $ ./FirstFile.sh>

Sortida:

Sortida

Nota: Com que el fitxer File3.txt està present al sistema. Per tant, el fitxer imprès existeix.

Vegem un exemple basat en paràmetres:

    Utilitzant el paràmetre -d: creeu un fitxer anomenat FirstDir.sh i escriu-hi el següent script
!/bin/bash if [[ -d 'GFG_dir' ]] ; # Here GFG_dir is directory and in place of GFG_dir you can write your Directory name then echo 'Directory is exist' # If GFG_dir exist then it will be printed else echo 'Directory is not exist' # If GFG_dir is not exist then it will be printed fi>

Ara deseu i executeu el fitxer amb l'ordre següent

ipconfig per a ubuntu
$ chmod +x ./FirstDir.sh $ ./FirstDir.sh>

Sortida:

Sortida

Nota: Com que el GFG_dir està present al sistema. Per tant, el directori imprès existeix.

De la mateixa manera, podeu utilitzar -f , -És , -En , -r , -c , etc. (segons els seus usos) en lloc de -d per comprovar l'existència de diferents tipus de fitxers.

Vegem un exemple basat en una comparació de dos fitxers:

  • Utilitzant -Nou Testament paràmetre

Creeu un nom de fitxer Comparison_File.sh i escriviu l'script següent

#!/bin/bash # New_file.txt and Old_File.txt are names of two files. if [[ 'New_File.txt' -nt 'Old_File.txt' ]] ; then # This will be printed if Condition is true echo 'New_File.txt is newer than Old_File.txt' else # This will be printed if Condition is False echo 'New_File.txt is not newer than Old_File.txt' fi>

Ara deseu i executeu el fitxer amb l'ordre següent

 $  chmod +x ./Comparison_File.sh $ ./Comparison_File.sh>

Sortida:

Sortida

Nota: Com que tots dos fitxers estan presents al sistema i New_File.txt és més nou que Old_File.txt

Vegem l'exemple Comprovar si el fitxer no existeix:

Creeu un fitxer anomenat Check_Exist.sh i escriu-hi el següent script

0,2 com a fracció
#!/bin/bash # using ! before -f parameter to check if # file does not exist if [[ ! -f 'GFG.txt' ]] ; then # This will printed if condition is True echo 'File is not exist' else # This will be printed if condition is False echo 'File is exist' fi>

Ara deseu i executeu el fitxer amb l'ordre següent

 $ chmod +x ./Check_Exist.sh $  ./Check_Exist.sh>

Sortida:

Sortida

Nota: GFG.txt no està present al sistema. Per tant, imprimirà El fitxer no existeix

Vegem un exemple sense utilitzar la condició If-else:

Creeu un fitxer anomenat Geeks_File.sh i escriviu-hi l'script següent

#!/bin/bash # If File exist then first statement will be # printed and if it is not exist then 2nd # statement will be printed. [ -f 'GFG_File.txt' ] && echo 'File is exist' || echo 'File is not exist'>

Ara deseu i executeu el fitxer amb l'ordre següent

 $ chmod +x ./Geeks_File.sh $ ./Geeks_File.sh>

Sortida:

Sortida

Nota: Com que el GFG_File.txt està present al sistema. Per tant, el fitxer imprès existeix.