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

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

Related

I am trying to write a shell script to read username from a file, but it is not working. I am posting script I am writing and output [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 3 years ago.
#!/bin/bash
if [ -f "$1" ]
then
for users in 'cat $1'
do
useradd $users
done
else
echo "input is not a file"
fi
You just have to get the input for the do loop right:
#!/bin/bash
if [ -f "$1" ]
then
for user in $(cat "$1")
do
useradd "$user"
done
else
echo "input is not a file"
fi
Remarks: this works for reading out a file word-by-word and I tried to keep your structure.
For reading out files line by line this is an elegant way: https://stackoverflow.com/a/4642213/2819581

how I could get a echo output if file is empty in linux bash? [duplicate]

This question already has answers here:
How to check if a file is empty in Bash?
(11 answers)
Closed 5 years ago.
I have a simple command for identify when file is empty but not works
MN=$(echo "$(cat empty)") ;
if [ MN == "" ]; then echo "This file is empty"; else echo "This file has been edited. You'll need to do it manually."; fi
whats I doing wrong
Please help me
test -s tests whether a file exists and is nonempty.
if test -e empty && ! test -s empty; then
echo "This file exists but is empty"
fi

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 file exist Linux bash [duplicate]

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

Resources