Bash shell script if file does not exist




















Other answers below address the syntax error missing the space between [ and! A Unix command requires some whitespace between the command name and its arguments. Add a comment. Active Oldest Votes. Improve this answer. No need to modify the file time any of them if you are just trying to ensure it exists.

The question is about if does not exist. You also don't need the null operation, you should be able to simply do:! Ant: mkdir -p : create all the necessary directories, without complaining if one already exists. One could even get rid of the if To start with, shell script is not bash script , so let's make your code more general:! James K. Lowden James K. Lowden 1, 10 10 silver badges 14 14 bronze badges. DopeGhoti DopeGhoti 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. When checking if a file exists, the most commonly used FILE operators are -e and -f.

In the following I demonstrate several bash shell script examples for this use case. The easiest way to check if a file exists is to use the test command. Alternatively, you can use the bash 's built-in flag -f , which can check for existence of a file when used inside bash 's conditional expressions.

It is also possible to use the following shorthand one-liner, where the conditional check and a corresponding action are written in a single line. If you want to check if a file does not exist in bash , you can simply add! Philip Kirkbride Philip Kirkbride This code means: "if ls is successful, there is such file, otherwise, there is none". If ls failed, it does not mean that file is missing. It might be some other error. For example, create file in directory owned by root and try to do ls under regular user.

It will fail with Permission denied , which is not equivalent that file does not exist. Tigran That's a good distinction to know, in my case doesn't matter, but thanks for pointing out. But there are a few treacherous rapids on this river: Everything is a file. Scripts have real power only if they solve general tasks To be general, we use variables We often use -f force in scripts to avoid manual intervention And also love -r recursive to make sure we create, copy and destroy in a timely fashion.

Consider the following scenario: We have the file we want to delete: filesexists. Does the files exist? Just issue rm filename and it will be gone after that, whether it was there or not. T G T G 2 2 silver badges 7 7 bronze badges. This is actually quite a valid answer for the rm command.

For other commands I suggest rather testing with -e. Except that it's not what the question asks. It is very much what the question asks, if you read the whole question including the "



0コメント

  • 1000 / 1000