Introduction
In Bash, you can check the existence of a file using various options. This challenge focuses on using the '-f' option to test the file existence.
In Bash, you can check the existence of a file using various options. This challenge focuses on using the '-f' option to test the file existence.
Create a Bash script named file_exist.sh
that takes a filename as an argument and checks if the file exists in the current location.
If the file exists, the script should print "File exists" and if the file does not exist, the script should print "File does not exist".
~/project/file_exist.sh
.-f
option to check the file existence.After you finish this challenge, you can verify the script works as expected.
Check if the file filename.txt
exists:
touch filename.txt
bash file_exist.sh filename.txt
Output:
File exists
Check if a non-existent file non_existent_file.txt
exists:
bash file_exist.sh non_existent_file.txt
Output:
File does not exist
In this challenge, we learned how to check the existence of a file in Bash using the '-f' option. We created a Bash script that takes a filename as an argument and checks if the file exists in the current location. If the file exists, the script prints "File exists" and if the file does not exist, the script prints "File does not exist".