Unix exist file




















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 " No need to press anything else. Some times, you need a capital letter.

Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. Ask Question. Asked 8 years, 1 month ago. Active 1 year, 6 months ago. Viewed k times. We can use find command and then suppress the output to check if the target directory has some content or not. Again as I said since there is no easy way to check if a directory is empty or not using some attributes, we will list the content of the directory.

If you get no output then the directory is empty or else not empty. Similarly there are many more attributes which you can use with shell scripts to check for different file types, such as symbolic links, file permissions etc.

Attributes What it does? In this tutorial I shared brief examples of test operators to check for files and directories. You can use any attribute from the table I have shared to enhance your script execution with proper tests. Lastly I hope the steps from the article to check if file exists or not on Linux was helpful. So, let me know your suggestions and feedback using the comment section. If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

This will fail if the directory contains a lot of files. Show 11 more comments. Letting find handle the wildcard is best because bash , as it expands the pattern, tries to sort the list of the matching file names, which is wasteful and can be expensive.

I guess it all depends on the situation; on the other hand, if there are a huge number of files, the wildcard expansion of bash can take more time than launching find. So it is not as big a waste of resources as you suggest. Moreover, find doesn't simply "expand" the wildcard as the shell does, but checks each file it finds against the pattern to see if it is a match, so it doesn't fail for a huge number of files. Show 1 more comment. Pankaj Parashar Pankaj Parashar 8, 5 5 gold badges 31 31 silver badges 43 43 bronze badges.

You should add unsetopt nomatch if zsh reports errors. It shouild perhaps be pointed out more clearly that using an array makes this decidedly non-POSIX sh. Otherwise, if stars align in an unlucky way such that both set -u and shopt -s nullglob are in effect, the expansion would fail. The most portable solution! Alas, it doesn't work when there's no matches. You could set "nullglob" in bash so it WILL blank out. THat's not portable, though : — Chris Cogdon.

Therefore you don't need nullglob, or other tricks. Tried with zsh as shell. It works if typed as a command, but fails with 'no matches found' when called from a zsh script. Strangely — jmary. Some special character? It will print multiple "BLAH" if there are multiple matches. Maybe add a break to exit the loop after the first match.

By using only native globbing and the file test operator, it avoids even raising the question of corner cases, that comes with using commands like ls or find or from forwarding globs. I think it is free of all the issues, like names with blanks, nullglob setting and bashisms, that were raised for some other answers.

Swift Swift In my shell zsh it works if there is only one match to the glob, otherwise it expands all the files and the test fails too many arguments. Update my code. I'm sure this works, I just installed zsh and tested.

If the globbing matches a directory name, ls will spit out the contentes of that directory which may cause false positives. Running ls and wc require to launch two external programs. Not efficient at all. Marian Marian 6, 2 2 gold badges 20 20 silver badges 32 32 bronze badges.

This is the simplest, easiest and most elegant answer that actually works! SergeStroobandt Not sure I agree. The command substitution may be necessary here, but it tickles my cringe reflex.

Not elegant at all because it forks a sub shell to run the echo command. MIF — Faris. The PowerShell way - which treats wildcards different - you put it in the quotes like so below: If Test-Path ".



0コメント

  • 1000 / 1000