Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Check Directory for files & Email

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 02-25-2016
Nvizn Nvizn is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 10 November 2016, 9:40 AM EST
Posts: 35
Thanks: 11
Thanked 2 Times in 2 Posts
Check Directory for files & Email

I'm working on a bash script to move files from one location, to two. The first part of my challenge is intended to check a particular directory for contents (e.g. files or other items in it), if files exists, then send the list of names to a txt file and email me the text file. If files do not exist, do nothing. It's not working as intended. Mailing the file is fine. Checking for contents, sending contents to file, is not. Assistance is appreciated.

Here's where I am.


Code:

file2=/directory/file/*
file=/usr/local/mailfile.txt


if [ -f ${file2} ];
 then
  ls -la $file2 > $file
fi

if [ -s ${file} ] ; then
  mail -s "$Subject" "$Recipients"  < $file
fi

Sponsored Links
    #2  
Old Unix and Linux 02-28-2016
Peasant's Unix or Linux Image
Peasant Peasant is offline Forum Advisor  
Registered User
 
Join Date: Mar 2011
Last Activity: 25 May 2017, 12:13 AM EDT
Posts: 1,050
Thanks: 29
Thanked 304 Times in 267 Posts
file2 variable will expand to multiple values (file names from directory specified)

It cannot be used in if [ -f $file2 ] .. , the error will be to many arguments (it expects one not a list of files).

I cannot be sure about the exact requirement from your post..

Code:
file2=/directory/file/* # lets create a variable which contains all the files in a directory.
file=/usr/local/mailfile.txt


if [ -f ${file2} ]; # if a file exists (this will work only if a one file is a file2 directory)
 then
  ls -la $file2 > $file # list all the files into a txt file  ? You could do these without any variables or conditions ...
fi
....

So the entire code can be :

Code:
ls -al /directory/file/*  > ${file}.txt
if [ -s ${file}.txt ]; then # if ls output gave something to file.txt (there are files)
... mail or whatever
else
printf "%s\n" "Directory is empty, will do nothing"
fi

The Following User Says Thank You to Peasant For This Useful Post:
Nvizn (03-02-2016)
Sponsored Links
    #3  
Old Unix and Linux 03-02-2016
Nvizn Nvizn is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 10 November 2016, 9:40 AM EST
Posts: 35
Thanks: 11
Thanked 2 Times in 2 Posts
Thanks for your response, Peasant. I had decided to do the following before your response, and is working great. It looks to see if any files exists, sends the contents to a text file and emails the output of the file. If no items exists in the directory, it does nothing. This is only a quarter of the entire script and what needs to be done. I may be posting additional questions on this.:


Code:
dir=/directory/file/*
file=/usr/local/mailfile.txt


if [ "$(ls -a $dir)" ] ; then
        ls -la $dir > $file
else
        :
fi

if [ -s ${file} ] ; then
  mail -s "$Subject" "$Recipients"  < $file
fi

    #4  
Old Unix and Linux 03-03-2016
Peasant's Unix or Linux Image
Peasant Peasant is offline Forum Advisor  
Registered User
 
Join Date: Mar 2011
Last Activity: 25 May 2017, 12:13 AM EDT
Posts: 1,050
Thanks: 29
Thanked 304 Times in 267 Posts
You might want to try something like :

Code:
ls -al $dir > $file &&
if [ -s $file ]; then mail -s "$Subject" "$Recipients" < $file ; fi ||
printf "%s\n" "No files in directory or some other ls exit code error"

Hope that helps
Regards
Peasant.
Sponsored Links
    #5  
Old Unix and Linux 03-03-2016
MadeInGermany MadeInGermany is offline Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 24 May 2017, 10:21 PM EDT
Location: Simplicity
Posts: 3,547
Thanks: 277
Thanked 1,181 Times in 1,069 Posts
Quote:
Originally Posted by Peasant View Post
You might want to try something like :

Code:
ls -al $dir > $file &&
if [ -s $file ]; then mail -s "$Subject" "$Recipients" < $file ; fi ||
printf "%s\n" "No files in directory or some other ls exit code error"

Hope that helps
Regards
Peasant.
Is the logic okay?
I would prefer a straight logic

Code:
if ls -al $dir > $file && [ -s $file ]
then
  mail -s "$Subject" "$Recipients" < $file
else
  printf "%s\n" "No files in directory or some other ls exit code error"
fi

Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to check whether directory has files in it or not in shell script? VSom007 Shell Programming and Scripting 4 05-15-2013 01:56 PM
Need Help in reading N days files from a Directory & combining the files dsfreddie UNIX for Dummies Questions & Answers 1 12-30-2011 11:33 AM
Need to find occurrences of email domains in all files in a directory linuxhombre Shell Programming and Scripting 4 10-15-2009 01:39 PM
Extracting the different files from directory & its sub directories rcvasu UNIX for Advanced & Expert Users 3 07-08-2009 02:19 PM
How to check if 3 files have same size in directory oggle Shell Programming and Scripting 5 02-16-2005 11:51 AM



All times are GMT -4. The time now is 01:15 AM.