If you need to take some action if the file does NOT exist, then you must test for that yourself. Based on your example code, this is not the case in this instance. How are we doing? Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How to check if a file exists in a shell script Ask Question. Asked 5 years, 2 months ago. Active 7 months ago. Viewed k times. Kurt Peek Kurt Peek Possible duplicate of How do I tell if a regular file does not exist in Bash? You must set one or more whitespace between opening square bracket "[" and option "-e" same as between filename and closing square bracket "]" — Konstantin Yaniv.
Related: Why do we need a space around square brackets in Bash if. Add a comment. Active Oldest Votes. You're missing a required space between the bracket and -e :!
The script seems to work now. The main difference here is the fact that you are using "bash" scripting instead of "shell" scripting. Notice that the first line that you have added was! The first line executes the test to see if the file exists.
The second command, echo , displays the results 0 meaning that the file exists, 1 means no file was found. To check if a directory exists, switch out the —f option on the test command for —d for directory :. This command works the same as it does for files, so using brackets instead of the test command works here also. Note: If you are searching for a file or directory because you need to delete it, refer to our guide on removing files and directories with Linux command line.
Typically, testing for a file returns 0 true if the file exists, and 1 false if the file does not exist. For some operations, you may want to reverse the logic. The exclamation point! This command makes sure there is not a file named test. You should see test. You can use a similar command for a directory — replace the —f option with —d :. As usual, changing the —f option to —d lets you run the same test for multiple directories.
There are many other options available. Please consult the main page test ——help for additional options. The previous commands work well for a simple two-line command at a command prompt. You can also use bash with multiple commands. When several commands are strung together, they are called a script. You can use bash conditional expressions with [[ ]] or use test with [ ] to check if file exists. We will be using bash if and else operator for all the examples so I would recommend you to read: Bash if else usage guide for absolute beginners.
We can use single or double brackets here in this example. I would recommend always to use double brackets when you are writing one liner code in shell as it minimises the risk of getting warnings on the console when word splitting takes place.
Similarly using test command to check if regular file exists in bash or shell script in single line. To check if the file exists and if it is empty or if it has some content then we use " -s " attribute.
Using test command we combine -s attribute to check if file exists and is empty or has some content:. We can use both double or single brackets for such one liner but as I said earlier I would recommend using double brackets.
I hope you know that in Linux everything is a file. So in that way, a directory is also considered to be a file. 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.
0コメント