Bash - Check If File Exists - if [-e FILE] - Tutorial Kart
[Pages:4]Bash ? Check If File Exists ? if [-e FILE]
Bash ? Check If File Exists
Bash Script to Check If File Exists ? To check if file exists in bash scripting, we can use [ -a FILE ] and [ -e FILE ] expressions in bash if statement. [-e FILE] is preferred as [-a FILE] is depreciated.
In this tutorial, we will go through Bash Script examples to check if file exists covering both the above said expressions.
Examples
Example 1 ? Using [ -a FILE ] (Depreciated method to check in bash if file exists.) Example 2 ? Using [ -e FILE ] (Preferred method to check in bash if file exists.)
We shall use a sample.txt file for this example
arjun@arjun-VPCEH26EN:/home/tutorialkart$ ls -lt
total 125012
-rw-r--r-- 1 root
root
12 Oct 5 09:35 sample.txt
Example 1 ? Using [ -a FILE ] expression with if statement
For this example, there is a file /home/tutorialkart/sample.txt present and /home/tutorialkart/dummy.txt not present. We shall demonstrate the result with the help of following example.
#!/bin/bash
# Scenario - File exists if [ -a /home/tutorialkart/sample.txt ]; then
echo "sample.txt - File exists." else
echo "sample.txt - File does not exist." fi
# Scenario - File does not exists if [ -a /home/tutorialkart/dummy.txt ]; then
echo "dummy.txt - File exists." else
echo "dummy.txt - File does not exist." fi
fi
When the above bash shell script is run in Terminal
arjun@arjun-VPCEH26EN:~/workspace/bash$ ./bash-script-if-file-exists sample.txt - File exists. dummy.txt - File does not exist.
Example 2 ? Using [ -e FILE ] expression with if statement
We shall use the same files as in Example 1, but with a new expression [ -e FILE ]
#!/bin/bash # Scenario - File exists if [ -e /home/tutorialkart/sample.txt ]; then
echo "sample.txt - File exists." else
echo "sample.txt - File does not exist." fi # Scenario - File does not exists if [ -e /home/tutorialkart/dummy.txt ]; then
echo "dummy.txt - File exists." else
echo "dummy.txt - File does not exist." fi
When the above bash shell script is run in Terminal
arjun@arjun-VPCEH26EN:~/workspace/bash$ ./bash-script-if-file-exists-2 sample.txt - File exists. dummy.txt - File does not exist.
Conclusion
In this Bash Tutorial ? Bash ? Check If File Exists, we have learnt to check if a specified file exists using [-e FILE] condition.
Bash Shell Scripting Bash Tutorial Bash Script Example
Bash Script Example Bash File Extension Bash Echo Bash Comments Bash Variable Bash Command Line Arguments Bash Read User Input Bash Read Password Bash Date Format Bash Sleep
Operators Bash Arithmetic Operators
Conditional Statements Bash If Bash If Else Bash Else If Bash Case
Loops Bash For Loop Bash While Loop Bash Until Loop
Strings Bash String Manipulation Examples Bash String Length Bash If String Equals Bash Split String Bash SubString Bash Concatenate String Bash Concatenate Variables to Strings
Functions
Functions Bash Function Bash Override Buitlin Commands
Arrays Bash Array
Files Bash Write to File Bash Read File Bash Read File line by line Bash If File Exists Bash If File is Directory Bash If File is Readable
Bash Others Bash Check if variable is set
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- using vmrun to control virtual machines vmware
- pysftp documentation read the docs
- csv file 1 akshay xii a 2 abhishek xii a 3 arvind xii a 4 ravi xii a 5
- bash check if file exists if e file tutorial kart
- kotlin check if file exists example tutorial kart
- introduction to computation for the humanities and social sciences
- s3fs documentation read the docs
- import somefile everything abc from
- object exist a macro to check if a specified object exists
- python files i o
Related searches
- python check if key exists in dictionary
- python check if key exists in map
- python check if directory exists and create
- check if class exists javascript
- jquery check if id exists on page
- check if id exists jquery
- check if variable exists javascript
- check if element exists js
- javascript check if element exists in dom
- check if object exists javascript
- python if file exists remove
- powershell check if folder exists and create