Check if file exist Linux bash [duplicate] - linux

This question already has answers here:
How do I tell if a file does not exist in Bash?
(20 answers)
Difference between ./ and ~/
(6 answers)
Closed 2 years ago.
So I'm trying to check if a file exists or not and then the script is supposed to do something if it does. The problem I'm having is actually getting it to recognize that something is actually there.
if [ -e /temp/file.txt ]; then
echo "file found!"
sudo cp -r temp/* extra
else
echo "file not found! Creating new one..."
./create.sh
fi
below is an example of the files in the directory I'm testing. they are clearly there, but for some reason I can't get the script to see that. what am I doing wrong?
nima#mkt:/docs/text$ ls -a temp
. .. more file.txt file2.txt

You are using absolute paths in your test while you should be using relative paths:
if [ -e ./temp/file.txt ]; then

/temp/file.txt vs /docs/text/temp/file.txt?

You script looks in /temp while you are looking in /docs/text/temp

Related

Linux remove all *.sln file exclude one file [duplicate]

This question already has answers here:
Bash script to remove all files and directories except specific ones
(5 answers)
Closed 6 months ago.
Can I know how to remove all *.sln file but exclude "work.sln" in the same folder
I try run rm *.sln !("work.sln")
it return /bin/bash: eval: line 128: syntax error near unexpected token ('`
Thank you
Assuming you are already inside the folder that contains the files you need, this is one way to do it (also assuming you are looking for a one-line version, given your example)
for file in $(ls);do if [[ ! $file == work.sln ]]; then rm $file; fi; done
EDIT: as #tripleee pointed out, the ls can be avoided and re-written like this
for file in *; do if [[ ! $file == work.sln ]]; then rm $file; fi; done

How to check whether a directory is empty or not in Shell Scripting? [duplicate]

This question already has answers here:
Checking from shell script if a directory contains files
(30 answers)
How do I check if a folder has contents? [duplicate]
(3 answers)
Closed 6 years ago.
I have a directory. It is empty. If i perform ls -lrt , it shows total 0
How do I specify an If condition to perform something, only if the directory is empty.
I mean to ask how to capture that 0 value.
From here. This should help you run your statements within the if else loop. I saved the DIR in the variable
#!/bin/bash
FILE=""
DIR="/empty_dir"
# init
# look for empty dir
if [ "$(ls -A $DIR)" ]; then
echo "Take action $DIR is not Empty"
else
echo "$DIR is Empty"
fi
# rest of the logic
Remove the -A option :
$ mkdir /tmp/aaa
$ ls /tmp/aaa
$ a=\`ls /tmp/aaa`
$ [[ -z $a ]]
$ echo $?
0

Cannot stat mv, file not found [duplicate]

This question already has answers here:
Tilde in path doesn't expand to home directory
(5 answers)
Closed 8 years ago.
could somebody please help me find the problem with this code? getting this error "mv: cannot stat '~Desktop/RecyclingBin/testtest' : No such file or directory. It does exist and it is in the location ~Desktop/RecyclingBin/testtest
fileName=$1
fileLocation='cat ~/Desktop/RecyclingBin/logs/$fileName
if [ -z "$1" ]
then
echo "please enter a valid filename"
else
echo "do you want to restore?"
read ans
if [ "$ans" =="y" ]
then
mv "~/Desktop/RecyclingBin/$fileName" "$fileLocation"
fi
fi
The quotes prevent expansion of ~. Put it outside the quotes:
mv ~/Desktop/RecyclingBin/"$fileName" "$fileLocation"

Check if a file exists in a linux script [duplicate]

This question already has answers here:
How do I tell if a file does not exist in Bash?
(20 answers)
Closed 4 years ago.
i made a linux script which receives as first argument a path to a directory. I don't know the path. And i want to check if "file.txt" exists at that certain path . For example :
if [ -e $1/file.txt ];then
echo HAHA
fi
if [[ -e "$1/file.txt" ]]; then
echo "It exists"
fi

How to check directory exist or not in linux.? [duplicate]

This question already has answers here:
How do I check if a directory exists or not in a Bash shell script?
(35 answers)
Closed 2 years ago.
Given a file path (e.g. /src/com/mot), how can I check whether mot exists, and create it if it doesn't using Linux or shell scripting??
With bash/sh/ksh, you can do:
if [ ! -d /directory/to/check ]; then
mkdir -p /directory/toc/check
fi
For files, replace -d with -f, then you can do whatever operations you need on the non-existant file.
Check for directory exists
if [ -d "$DIRPATH" ]; then
# Add code logic here
fi
Check for directory does not exist
if [ ! -d "$DIRPATH" ]; then
# Add code logic here
fi
mkdir -p creates the directory without giving an error if it already exists.
Well, if you only check for the directory to create it if it does not exist, you might as well just use:
mkdir -p /src/com/mot
mkdir -p will create the directory if it does not exist, otherwise does nothing.
test -d /src/com/mot || mkdir /src/com/mot
This is baisc, but I think it works. You'll have to set a few variables if you're looking to have a dynamic list to cycle through and check.
if [ -d /src/com/mot ];
then
echo Directory found
else
mkdir /src/com/mot
fi
Hope that's what you were looking for...

Resources