logo

Bash スクリプト – ファイルが存在するかどうかを確認する方法

この記事では、ファイルが存在するかどうかを確認する bash スクリプトを作成します。

構文:

  • テスト[式]
  • [ 表現 ]
  • [[ 表現 ]]

ここで、表現上、次のように書きます。 パラメータ そして ファイル名 。式で使用できるいくつかのパラメーターを見てみましょう。 –

  • f: ファイルが共通 (通常) ファイルとして存在する場合、True を返します。
  • -d : ディレクトリが存在する場合は True を返します。 -e : 何らかの種類のファイルが存在する場合は True を返します。 -c : キャラクターファイルが存在する場合はTrueを返します。 -r : 読み取り可能なファイルが存在する場合は True を返します。
  • : 書き込み可能なファイルが存在する場合は True を返します
  • -x : 実行可能ファイルが存在する場合は True を返します。 -p : ファイルがパイプとして存在する場合は True を返します。 -S : ファイルがソケットとして存在する場合は True を返します。 -s : ファイルが存在し、ファイルのサイズがゼロでない場合は True を返します。 -L : シンボリックリンクのファイルが存在する場合にTrueを返します -g : ファイルが存在し、ホールド セット グループ ID フラグが設定されている場合は True を返します。 -G : ファイルが存在し、処理中のものと同じグループ ID を保持している場合、t は True を返します。 -k : ファイルが存在し、スティッキー ビット フラグが設定されている場合は True を返します。

ここで、2 つのファイルを比較するためのパラメータがさらにいくつかあります。



    -ef: 両方のファイルが存在し、同じファイルを示す場合は True を返します。

例 :

FirstFile -ef SecondFile>
    -nt: FirstFile が Secondfile より新しい場合は True を返します。

例 :

反復マップJava
FirstFile -nt FileOld>
    -ot: FirstFile が SecondFile より古い場合は True を返します。

例:

FirstFile -ot SecondFile>

構文に基づいていくつかの例を見てみましょう。

    [式]: まず、FirstFile.shという名前のファイルを作成し、その上に次のスクリプトを記述します。
#!/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>

次のコマンドを使用してファイルを保存して実行します。

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

出力:

出力

注記: File.txt がシステム内に存在するため。そのため、 File isowned と表示されました。

インスタンスの
    test [式]: 次に、FirstFile.sh 内の上記のスクリプトを次のように変更します。
#!/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>

ここで、次のコマンドを使用してファイルを再度保存して実行します。

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

出力:

出力

注記: File2.txt がシステムに存在しないため。したがって、「ファイルは存在しません」と表示されました。

    [[式]]: FirstFile.sh 内の上記のスクリプトを次のように再度変更します。
#!/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>

ここで、次のコマンドを使用してファイルを再度保存して実行します。

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

出力:

出力

注記: File3.txt がシステム内に存在するため。したがって、 File is present と表示されました。

パラメーターに基づいた例を見てみましょう。

    -d パラメータを使用する: FirstDir.sh という名前のファイルを作成します。 そこに次のスクリプトを書きます
!/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>

次のコマンドを使用してファイルを保存して実行します。

Javaの文字列に等しい
$ chmod +x ./FirstDir.sh $ ./FirstDir.sh>

出力:

出力

注記: GFG_dir がシステム内に存在するため。したがって、 Directory is present と出力されました。

同様に、次のように使用できます -f-それは-で-r-c 、等。 (用途に応じて) の代わりに -d さまざまな種類のファイルの存在を確認します。

2 つのファイルの比較に基づく例を見てみましょう。

  • 使用する -nt パラメータ

Comparison_File.shというファイル名を作成し、次のスクリプトを記述します。

#!/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>

次のコマンドを使用してファイルを保存して実行します。

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

出力:

出力

注記: 両方のファイルがシステムに存在し、New_File.txt が Old_File.txt より新しいため

ファイルが存在しないことを確認する例を見てみましょう。

Check_Exist.sh という名前のファイルを作成します。 そこに次のスクリプトを書きます

#!/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>

次のコマンドを使用してファイルを保存して実行します。

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

出力:

出力

注記: GFG.txt がシステムに存在しません。したがって、「ファイルは存在しません」と表示されます

If-else 条件を使用しない例を見てみましょう。

Geeks_File.sh という名前のファイルを作成し、その中に次のスクリプトを記述します。

#!/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'>

次のコマンドを使用してファイルを保存して実行します。

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

出力:

Java文字列の部分文字列

出力

注記: GFG_File.txt がシステム内に存在するため。そのため、 File is present と表示されました。