Bash check if at least one file exists
I have one that exists which is AWL. I can use to check the existence of the directory. However when I try the following I cannot get it to work. RedHat Commands. OpenSolaris Commands. Linux Commands. SunOS Commands. FreeBSD Commands. Full Man Repository. Advanced Search. Contact Us. Forum Rules. Mark Forums Read. Thread Tools. View Public Profile for Rajesh Find all posts by Rajesh View Public Profile for Peasant. We can use " -d " attribute to check if a directory exists in shell programming.
We can use -d attribute within single [.. Similarly we use single brackets in this example to check if the directory is preset within shell script. In this example we will use test command to make sure if directory is present or not before we perform certain task. In this example we will use single and double brackets in single line form to check for the directory.
I would recommend using [[.. There are no shell attributes as I have used in all other examples and scenarios in this tutorial. But we do have some hacks which we can use to check if directory is empty or not- empty.
In this example we will list the content of the directory, suppress the output and then based on the exit code check if the directory is empty or contains some content. We can use find command and then suppress the output to check if the target directory has some content or not.
Asked 9 years, 2 months ago. Active 9 years, 2 months ago. Viewed 5k times. I have this if [[ -e file. Improve this question. William Pursell k 44 44 gold badges silver badges bronze badges. Lukap Lukap Add a comment. Active Oldest Votes. Many times when writing Shell scripts, you may find yourself in a situation where you need to perform an action based on whether a file exists or not.
In Bash, you can use the test command to check whether a file exists and determine the type of the file. If you want your script to be portable, you should prefer using the old test [ command, which is available on all POSIX shells. The new upgraded version of the test command [[ double brackets is supported on most modern systems using Bash, Zsh, and Ksh as a default shell.
0コメント