👤
Home Man
Search
Today's Posts
Register

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

👤 Login to reply

 
Thread Tools Search this Thread
# 1  
Old 02-25-2016
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

# 2  
Old 02-28-2016
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)
# 3  
Old 03-02-2016
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 03-03-2016
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.
# 5  
Old 03-03-2016
Quote:
Originally Posted by Peasant
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

👤 Login to reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Compressing & removing files in a directory & subdirectory Jesshelle David Shell Programming and Scripting 8 10-17-2016 05:06 PM
Check daily files arrival in a directory flagboy2014 Shell Programming and Scripting 5 09-28-2014 02:30 AM
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
Just listing size, timestamp & name of files in a directory Jazmania Shell Programming and Scripting 4 01-25-2010 01:21 PM
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
Find & Copy Selected files to another Directory hellfyre UNIX for Dummies Questions & Answers 3 04-07-2009 09:46 PM
list largest files in a directory & its subdirectories igidttam UNIX for Dummies Questions & Answers 6 09-25-2006 11:31 AM
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 02:19 AM.



All times are GMT -4. The time now is 02:19 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password