Files exists with same name and not empty


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Files exists with same name and not empty
# 1  
Old 09-14-2017
Files exists with same name and not empty

Hi everyone, I am new to shell scripting.Please help.

I have list of files under some path /opt/thomas/
FileNames :
Code:
 1.txt,2.txt,3.txt

I would like to check if these three files exists and not empty,continue with the script or else write error message file not found or file empty

Thanks

Last edited by jim mcnamara; 09-14-2017 at 10:43 AM..
# 2  
Old 09-14-2017
Just using simple commands:
Code:
export ok=0   # means files are there and non-empty

test_sz() {  # function to return 0 or 1
{
    sz="$1"
    sz=$( wc -c "$sz)
    sz=${sz:0:1}  # first character of sz string
    retval=0   # assume failure
    [ $sz -gt 0 ]  && retval=1
    return retval
}
[[ -e 1.txt && -e 2.txt && 3.txt ]] &&  ok=1 || echo 'files missing'
if [ ok -eq 1 ]; then 
   [[ $(test_sz 1.txt) -eq 0 || $(test_sz 2.txt) -eq 0  || $(test_sz 3.txt) -eq 0  ]] && ok=0
fi
if  [ ok -eq 0 ] ; then
   echo 'file(s) did not pass requirements'
   exit 1
fi
# rest of script here

This can be shortened, but it is meant to be a clear example.
# 3  
Old 09-14-2017
Quote:
Originally Posted by thomas9192
Hi everyone, I am new to shell scripting.Please help.

I have list of files under some path /opt/thomas/
FileNames :
Code:
 1.txt,2.txt,3.txt

I would like to check if these three files exists and not empty,continue with the script or else write error message file not found or file empty

Thanks
Please, try the following as you are in /opt/thomas/
Code:
for f in 1.txt 2.txt 3.txt; do
    if ! [ -s "$f" ]; then
        echo "$f does not exist or is empty"
        echo "Exiting..."
        exit 1
    fi
done
# Continue with script, here.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Code for checking if certain no of files exists

Hi, I am writing the shell script in ksh to check certain no of files exists,In my case there are 7 files exist like below Sales1_timstamp.csv Sales2_timstamp.csv Sales3_timstamp.csv Sales4_timstamp.csv Sales5_timstamp.csv Sales7_timstamp.csv Sales7_timstamp.csv Once all the files... (4 Replies)
Discussion started by: SRPR
4 Replies

2. Shell Programming and Scripting

How to check more than 1 file specified files exists?

Hi all, One of my script crated created 2 files in a dirs Output.log and Output.tmp. Now in another script i need to check if both of the above mentioned files are present in a directory or not. I know to check one file but need to check both the files. Anyone could please tell me how... (3 Replies)
Discussion started by: girijajoshi
3 Replies

3. UNIX for Dummies Questions & Answers

Outputting 1 file per row if pattern exists between files

I have many files that can have various amounts of rows. I essentially want to output each row into a new file if a pattern is matched between two files. I have some code that does something similar but I want it to output every single input row from every file into a separate output file; that... (5 Replies)
Discussion started by: verse123
5 Replies

4. UNIX for Dummies Questions & Answers

check if two files exists at the same time in one location

Hi friends, I am trying to check if the two files i am expecting are created in a specific location. if both files does not exist, do an echo if -a ]; then echo "files not found" fi It gives me the following message: bash: Please help! :) (3 Replies)
Discussion started by: kokoro
3 Replies

5. Shell Programming and Scripting

check if files exists

i writing a program that checks if any .txt files exist in the current directory if it does, then it lists the files... i have got everything right, except the validation part doesnt work! .... if then ls *.txt else echo "file not found" everytime it tells me file not found!! by... (3 Replies)
Discussion started by: bshell_1214
3 Replies

6. Shell Programming and Scripting

Find records between two files which are not exists in one another in one

Hello all, Would like to know how to find records between two files which are not exists in one another in one. For example: I've two files "fileA" and "fileB" and want to find record from "fileB" which does not exists in "fileA". fileA -------- ABCD DEFG GHIJ KLMN NOPQ RSTU VUWX... (5 Replies)
Discussion started by: nvkuriseti
5 Replies

7. Shell Programming and Scripting

How to see the list of files which exists with nonzero bytes

Hi, I would like to see the file list which contains the data. Ex : - When I enter the "ls -ltr" command it will show all the files which are exists in the directory, But I want only those files list which are non zero byte by using only ls command. I knew that I can write a script with... (12 Replies)
Discussion started by: kandi.reddy
12 Replies

8. Shell Programming and Scripting

Copy Files to Dir and Check If File Exists

Hi everyone. I am trying to write a bash script that will copy files from one directory to another but I need to be able to check the directory that I'm copying the files to and see if the file already exists. If it does I need to add a number at the end of the copied file. Thanks for your help. (3 Replies)
Discussion started by: snag49ers
3 Replies

9. Shell Programming and Scripting

HOW TO CHECK ONLY .C FILES EXISTS OR NOT IN A FOLDER using IF in C shell script?

Hi friends.. I hav a problem.... I dont know how to check .c files exists r not in a folder using IF in C shell script actually i tried like this if(=~ *.c) even though some .c files or there in the current folder..it is not entering int o the if control statement...... (17 Replies)
Discussion started by: p.hemadrireddy
17 Replies

10. UNIX for Dummies Questions & Answers

File exists and is empty

I've got a script with this snippet: if then I know that the -s indicates that the file exists and is not empty. How can I specify that the file exists, but is empty? I thought I could do: if ] then But this generates a "else unmatched" error on the else that follows. (5 Replies)
Discussion started by: dstinsman
5 Replies
Login or Register to Ask a Question