Check that at least one file exists in the directory.


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Check that at least one file exists in the directory.
# 1  
Check that at least one file exists in the directory.

There are some files with suffix dates like abc_20032019.dat abc_17032019.dat
If at least one file exists then perform some operation else exit from execution.
Korn shell
----------------------------------
Code:
array=($inputdir/abc*.dat)
If [  [ -f ${array[0]  ] ]
then 
echo " file exits"
else
echo " file does not exists"
fi

error inspected "("

Please let me know how to fix it.

Last edited by RavinderSingh13; 10-09-2019 at 02:10 AM..
# 2  
Don't think array here is required, as well as a testing if file(s) exists.
Only in some corner cases, if file is deleted during program execution..
Is that your case ?

Will this fill your requirment :
Code:
INPUTDIR=/path/with/files
for i in $INPUTDIR/abc*.dat
do
        echo "$i" # action on existing file, here echo.
done

Hope that helps
Regards
Peasant.
# 3  
If at least one file does not exists then script should throw error and execution should stop.
Code:
array=($inputdir/abc*.dat)
If [  [ -f ${array[0]  ] ]
then 
EXITCD=44
else
echo " file does not exists"
fi
return $EXITCD

--- Post updated at 05:27 AM ---

If at least one file does not exists then script should throw error and execution should stop.
Code:
array=($inputdir/abc*.dat)
If [ [ -f ${array[0] ] ]
then
echo "file exists"
else
echo " filesl does not exists"
EXITCD=44
fi
return $EXITCD


Last edited by Peasant; 10-09-2019 at 03:47 AM..
This User Gave Thanks to Rajesh123 For This Post:
# 4  
Complement the code with :
Code:
INPUTDIR=/path/with/files
for i in $INPUTDIR/abc*.dat
do
        if  [ -f $i ]; then
                echo "File exist, processing..."
        else
                echo "File does not exist, exiting.."
                exit 1
        fi
done

Please use code tags for code, as required by forum rules.

Hope that helps
Regards
Peasant.
This User Gave Thanks to Peasant For This Post:
# 5  
Thanks.
I have doubt

When I use below command it deletes the file even there is no file exist.
Code:
INPUTDIR=/path/with/files
If [[ 'ls $INPUTDIR/abc*.dat | wc -l > 0' ]]
then
rm fiile1
else 
exit 4
if

While executing above script returning 0 and deleting file instead of exit with code 4 and log file says no such file or directory.
What is the mistake I am making?.

Last edited by Peasant; 10-09-2019 at 12:03 PM.. Reason: Added code tags
# 6  
Using the file_exists function
Code:
if file_exists $inputdir/abc*.dat
then
  echo "a file exists"
fi

# 7  
You never used code i posted, and you have expanded your code with pipes and line counts.

If [[ 'ls $INPUTDIR/abc*.dat | wc -l > 0' ]] -> will always evaluate to true
Without test it will default on -n, at least on bash here debian linux.

The command inside single quotes is never executed, just checked if the length of string passed 'ls $INPUTDIR/abc*.dat | wc -l > 0' is greater then zero.
This will always be true, and your code will never hit else condition.

if should be lower case.
Ending the if clause is done with fi, not another if as you wrote.
The fiile1 is a hard-coded file name and will be removed when you execute the script from current running directory, deleting the file if it exists.

I'm not sure what are you are trying to write here, hopefully i cleared some or all misunderstanding.
Also MiG suggested an alternative with custom function found in thread linked.

Can you be more precise about your exact requirement, with input and output expected or desired effect of your code ?
Please specify your operating system and shell details as well.

Hope that helps.
Regards
Peasant.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #96
Difficulty: Easy
Unix and Unix-like operating systems are a family of computer operating systems that are derived from the original Unix System developed at Xerox Park.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check whether file exists in directory

Hi guys, I am beginner trying to learn unix. So any help is welcomed. My requirement is to check whether is a file exists in a particular directory or not. The directory path and filename are taken dynamically with user interaction. So the program should continue only if the $filename... (1 Reply)
Discussion started by: maris_markur
1 Replies

2. Shell Programming and Scripting

How to check if the file exists in directory?

Hi Gurus, I have a requests to find if all the file in the filelist exist in certain directory. example: my filelist abc def ddd cde afg how can I find these 5 files exists at director /home/abc Thanks in advance (7 Replies)
Discussion started by: ken6503
7 Replies

3. Shell Programming and Scripting

how to check if a directory exists or not.if not need to create it

Hi, I am using solaris 10 OS and bash shell.just checking for small code snippet which follows below. /export/home/vomappservers/spa/common/5.0 /export/home/vomappservers/spa/common/scripts /export/home/vomappservers/spa/tools /export/home/vomappservers/spa/scm5.0/SCCS... (5 Replies)
Discussion started by: muraliinfy04
5 Replies

4. Shell Programming and Scripting

How to check if a file exists in a directory?

I want to perform SQL *Loader operation only if a file named "load.txt" exists in a directory "/home/loc/etc". Please help how to check this with a if condition. (8 Replies)
Discussion started by: vel4ever
8 Replies

5. Shell Programming and Scripting

check if directory and file exists

cp $PATHLOGS/$DATE/*.* $TMP/logs_tmp/ cp $PATHLOGS/$DATE1/*.* $TMP/logs_tmp/ Before copying the files I have to check if the directory $DATE1 and $DATE2 exists. If directory exists then, check if the folder contains some files. if the file exists then, check if the file size is greater... (3 Replies)
Discussion started by: sandy1028
3 Replies

6. Shell Programming and Scripting

check if a directory exists if not make it

Hey guys im trying to check if the directory exists i get a syntax error on the elif statement iv tried using else and still same result im not sure. If the directory does not exist can i just insert mkdir /tmp/old under the elif once that part gets working if ; then #do nothing elif echo... (20 Replies)
Discussion started by: musicmancanora
20 Replies

7. Shell Programming and Scripting

my scripts does not check if directory exists

Hello: Can someone please help me figure out what is wrong here, my script does not move on to the "else" part even though there is no .ssh directory on my remote server: $more putkey.sh #!/bin/ksh for server in `cat list` do if ; then cat $HOME/.ssh/id_rsa.pub |ssh $server ' cat >>... (4 Replies)
Discussion started by: Sara-sh
4 Replies

8. UNIX for Dummies Questions & Answers

How can I check if directory exists in a makefile

Hi. I what to write a make file that inside a target I want to check if a directory exists. some like: ### make a: if ;then <command 1> else <command 2> fi ### make end Thanks a lot ---------------------- (2 Replies)
Discussion started by: zivsegal
2 Replies

9. Shell Programming and Scripting

how to check if masked directory exists?

I'm trying to write a script that identifies whether a directory of the form AWL.????????.IP exists. I have one that exists which is AWL.05301032.IP. When I test like this: If ] I get true, but when I test like this: If ] Or like this If ] Or any other variation of wild cards, I... (4 Replies)
Discussion started by: philplasma
4 Replies

10. Shell Programming and Scripting

check if directory exists

Hi, I need to prompt for a response from a user to enter a path read dest_dir?"Please Enter Directory :" How do I do this until a valid directory is entered by the user. I can use to check the existence of the directory. However when I try the following I cannot get it to work. while ... (2 Replies)
Discussion started by: jerardfjay
2 Replies

Featured Tech Videos