Posted: . At: 1:57 AM. This was 5 years ago. Post ID: 13216
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.



Sponsored



Basic bash scripting. How to find if a file exists or not.


This simple example shows how to check if a file exists or not.

This is how we do this.

if [ -f "/home/jason/foo" ]

Putting it together in a script makes it simple to verify if the file exists or not.

if [ -f "/home/jason/foo" ]
then
    echo "file does exist."
else
    echo "file not found."
fi

To check if a user exists on your machine, use this syntax.

if getent passwd jason > /dev/null 2>&1; then
    echo "yes the user exists"
else
    echo "No, the user does not exist"
fi

This will check if the user “jason” exists or not.

This type of scripting is very useful if a file needs to be created, and you wish to check if it is already there.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.