How to check if a files exists in a specific directory in a bash script?
You can use $FILE
to concatenate with the directory to make the full path as below.
FILE="$1"
if [ -e ~/.myexample/"$FILE" ]; then
echo "File exists"
else
echo "File does not exist"
fi
How to check if a file exists in a shell script
You're missing a required space between the bracket and -e
:
#!/bin/bash
if [ -e x.txt ]
then
echo "ok"
else
echo "nok"
fi
Check if same file exists in another directory using Bash
When you have a construct like for file in $firstPath/*
, the value of $file
is going to include the value of $firstPath
, which does not exist within $secondPath
. You need to strip the path in order to get the bare filename.
In traditional POSIX shell, the canonical way to do this was with an external tool called basename
. You can, however, achieve what is generally thought to be equivalent functionality using Parameter Expansion, thus:
for file in "$firstPath"/*; do
if [[ -f "$secondPath/${file##*/}" ]]; then
# file exists, do something
fi
done
The ${file##*/}
bit is the important part here. Per the documentation linked above, this means "the $file
variable, with everything up to the last /
stripped out." The result should be the same as what basename
produces.
As a general rule, you should quote your variables in bash. In addition, consider using [[
instead of [
unless you're actually writing POSIX shell scripts which need to be portable. You'll have a more extensive set of tests available to you, and more predictable handling of variables. There are other differences too.
linux script to check if file exists at a particular directory more than an hour
I think you should try this:
#!/bin/bash
if [[ `find /path/to/my/directory/ -type f -mmin +60 | wc -l` -gt 0 ]];then
echo "files do exist"
else
echo "files do not exist"
fi
Explain: i will find all files under directory: /path/to/my/directory/ with modify time greater than 60 minutes then count them.
How do I check whether a file or file directory exist in bash?
Checking file and/or directory existence
To check whether a file exists in bash, you use the -f
operator. For directories, use -d
. Example usage:
$ mkdir dir
$ [ -d dir ] && echo exists!
exists!
$ rmdir dir
$ [ -d dir ] && echo exists!
$ touch file
$ [ -f file ] || echo "doesn't exist..."
$ rm file
$ [ -f file ] || echo "doesn't exist..."
doesn't exist...
For more information simply execute man test
.
A note on -e
, this test operator checks whether a file exists. While this may seem like a good choice, it's better to use -f
which will return false if the file isn't a regular file. /dev/null
for example is a file but nor a regular file. Having the check return true is undesired in this case.
A note on variables
Be sure to quote variables too, once you have a space or any other special character contained in a variable it can have undesired side effects. So when you test for existence of files and directories, wrap the file/dir in double quotes. Something like [ -f "/path/to/some/${dir}/" ]
will work while the following would fail if there is a space in dir
: [ -f /path/to/some/${dir}/ ]
.
Fixing the syntax error
You are experiencing a syntax error in the control statements. A bash if
clause is structured as following:
if ...; then
...
fi
Or optional with an else
clause:
if ...; then
...
else
...
fi
You cannot omit the then
clause. If you wish to only use the else
clause you should negate the condition. Resulting in following code:
if [ ! -f "/usr/share/icons/$j/scalable" ]; then
mkdir "/usr/share/icons/$j/scalable/"
fi
Here we add an exclamation point (!
) to flip the expression's evaluation. If the expression evaluates to true, the same expression preceded by !
will return false and the other way around.
Checking from shell script if a directory contains files
The solutions so far use ls
. Here's an all bash solution:
#!/bin/bash
shopt -s nullglob dotglob # To include hidden files
files=(/some/dir/*)
if [ ${#files[@]} -gt 0 ]; then echo "huzzah"; fi
Check if file exist relative to current script (one level up)
There are three problems in your script.
- To store the output of a command in a variable, use
$()
, not${}
. [ -f "$dir" ]
checks if$dir
is a a file, which will never happen, becausedirname
outputs a directory.- Your script can be executed from any other working directory as well. Just because the script is stored in
···/app/scripts/
does not mean it will always run from there.
Try
file=$(dirname "$BASH_SOURCE")/../dist/file.js
if [ -f "$file" ]; then
echo "file exists."
else
echo "file does not exist."
fi
How to check if a file is present in a particular directory in Unix?
We can make use of [ls] and [$?] command to do this.
Use below command :
ls -l filename.txt
Followed by,
echo $?
If the file exists, the [ls] command will be successful.Hence [echo $?] will print 0.
If the file does not exists, then [ls] command will fail and hence [echo $?] will print 1.
Related Topics
Debugging in Pycharm with Sudo Privileges
What Tools Do I Need to Develop in Actionscript (In Linux)
How to Tail -F the Latest Log File with a Given Pattern
Git- How to Kill Ssh-Agent Properly on Linux
How Does Stat Command Calculate the Blocks of a File
Udev - Run Program on Usb Flash Drive Insert
How to Calculate the Total Size of Certain Files Only, Recursive, in Linux
Interrupting Epoll_Wait with a Non-Io Event, No Signals
How to Know Whether a Copy-On-Write Page Is an Actual Copy
Need a Simple Linux C++ Ide (Android Ndk)
Adding a New System Call in Linux Kernel 3.3
Cannot Sudo Su Anymore, "No Tty Present and No Askpass Program Specified"