Check Directory for files & Email

Tags
shell scripts

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

This User Gave Thanks to Peasant For This 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

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Similar Threads More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to check total files size in hdfs directory? rohit_shinez Shell Programming and Scripting 1 06-11-2018 06:09 AM
Script to check the files existence inside a directory. sadique.manzar Shell Programming and Scripting 16 11-01-2017 09:42 PM
How can I check, if on remote server directory is empty or have files? digioleg54 Shell Programming and Scripting 2 04-08-2017 05:51 AM
Compressing & removing files in a directory & subdirectory Jesshelle David Shell Programming and Scripting 8 10-17-2016 06:06 PM
Check daily files arrival in a directory flagboy2014 Shell Programming and Scripting 5 09-28-2014 03: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 02:56 PM
Script for creating a directory & move the .tif files in it. paragnehete Programming 9 05-04-2012 08:55 PM
Need Help in reading N days files from a Directory & combining the files dsfreddie UNIX for Dummies Questions & Answers 1 12-30-2011 12:33 PM
Just listing size, timestamp & name of files in a directory Jazmania Shell Programming and Scripting 4 01-25-2010 02:21 PM
Need to find occurrences of email domains in all files in a directory linuxhombre Shell Programming and Scripting 4 10-15-2009 02:39 PM
Extracting the different files from directory & its sub directories rcvasu UNIX for Advanced & Expert Users 3 07-08-2009 03:19 PM
Find & Copy Selected files to another Directory hellfyre UNIX for Dummies Questions & Answers 3 04-07-2009 10:46 PM
Check if certain files exist in a directory, if not add name to a textfile SunnyK Shell Programming and Scripting 1 02-07-2008 09:21 AM
list largest files in a directory & its subdirectories igidttam UNIX for Dummies Questions & Answers 6 09-25-2006 12:31 PM
How to check if 3 files have same size in directory oggle Shell Programming and Scripting 5 02-16-2005 12:51 PM
All times are GMT -4. The time now is 03:12 PM.

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





Not a Forum Member?
Forgot Password?