How to use the test command to check for the existence of files and directories
You can use the test -e command to check for the existence of a file. By connecting them with &&, it is possible to say that the command will be executed only if the file exists.
$ test -e a.txt && cat a.txt
The existence of the directory is checked with the test -d command.
$ test -d dd && ls -l dd
In addition, it seems to be able to check symbolic links and empty files.
Reference Site
![](https://confrage.jp/wp-content/uploads/cocoon-resources/blog-card-cache/6e19239841f180975a9e73b27cf0bef2.png)
【 test 】コマンド(基礎編)――ファイルの有無などを判定する
本連載は、Linuxのコマンドについて、基本書式からオプション、具体的な実行例までを紹介していきます。今回は、条件式を評価することでファイルの有無などを判定する「test」コマンドです。
![株式会社CONFRAGE](https://i0.wp.com/confrage.jp/wp-content/uploads/2018/08/aaaaaaaaa-1.jpg?resize=100%2C100&ssl=1)
コメント