Reputation:
I have been using this in my scripts an my coworkers disagree with me.
My script takes in a file as a parameter and creates the file. Then I use the following to see if it was actually created.
ls -p | grep [filename]
Then, I see if the file I am trying to create is in the grep list.
but they are suggesting I use
test -f [filename]
instead.
What is the proper way to check for if a file exists in Linux?
Upvotes: 1
Views: 437
Reputation:
I know the second method you mentioned is advised in the Linux Foundation's Introductory course. This method will test whether that name exists, and whether it is a file or not. Grep will simply tell you whether it found that string or not.
Upvotes: 2
Reputation: 1112
test -f [filename]
is the way to go. Running both ls and grep just for this operation is way overkill.
Upvotes: 3