I have a folder which contains some folders, these folder are moved very often so I made a script to see if they exist, and if not then create them. This is what I did to (which I though would) achieve it:if [ ! -f $DIR/0folder ]thenmkdir $DIR/0folderfiBut, even if 0folder already exists, it still tries to make it which mkdir tells me. Like here;mkdir: /Allfoldersgoeshere/subfolder/0folder: File existsWhy? It should just ignore it because it already exists?
Script tries to create files even though it shouldn't have to?
shell script;files;test;mkdir
The -f in your test is checking if FILE exists and is a regular file.What you need is -d to test if FILE exists and is a directory.if [ ! -d $DIR/0folder ]thenmkdir $DIR/0folderfiIt is not mandatory to check if a directory exists though.  According to the man page of mkdir we see the followingman mkdir | grep -A1 -- -p   -p, --parents          no error if existing, make parent directories as neededHowever, if FILE exists and is a regular file mkdir -p will fail with mkdir: /Allfoldersgoeshere/subfolder/0folder': Not a directory.  In this scenario handling the file that is expected to be a directory will be necessary before directory creation.