Check Directory for files & Email


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check Directory for files & Email
# 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:
# 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 or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check for files and move it to another directory - ksh

I'm trying to wirte ksh script for given requirement, but i unable to achive it. In dir1 directory I need to check for the files which suffixed with .csv or .txt, If there is no files, then i need to exit. If any files found I need to move the each file found to dir2 directory. I have to repeat... (4 Replies)
Discussion started by: Kayal
4 Replies

2. Shell Programming and Scripting

Compressing & removing files in a directory & subdirectory

Hi, I want a simple line of code that will compress files within a directory specified (parameter) and its subdirectories and also i want to remove files which are exactly 365 days old from the sysdate after this compression. Please help. Thanks, JD (8 Replies)
Discussion started by: Jesshelle David
8 Replies

3. Shell Programming and Scripting

Check daily files arrival in a directory

Hi All, I wanted to write a script to check if set of files exist in a directory or not for a particular day. each file has a date stamp. Now i want to implement this - -i can check files of a particular date in a particular folder -generate log and send it to through email. Big THANKS !!... (5 Replies)
Discussion started by: flagboy2014
5 Replies

4. Shell Programming and Scripting

How to check whether directory has files in it or not in shell script?

hi, I am having script in which i want to check if directory has any file in it or not. If directory contains a single or more files then and only then it should proceed to further operations... P.S.: Directory might have thousand number of files. so there might be chance of getting error... (4 Replies)
Discussion started by: VSom007
4 Replies

5. UNIX for Dummies Questions & Answers

Need Help in reading N days files from a Directory & combining the files

Hi All, Request your expertise in tackling one requirement in my project,(i dont have much expertise in Shell Scripting). The requirement is as below, 1) We store the last run date of a process in a file. When the batch run the next time, it should read this file, get the last run date from... (1 Reply)
Discussion started by: dsfreddie
1 Replies

6. Shell Programming and Scripting

Just listing size, timestamp & name of files in a directory

How can I list the files in a directory and just show the file size, date stamp, timestamp and file name.. I've been trying to ls -lrt the directory to a file and then use the cut command but I'm not having any luck with getting the proper results.. I thought i could use the -f switch and count... (4 Replies)
Discussion started by: Jazmania
4 Replies

7. Shell Programming and Scripting

Need to find occurrences of email domains in all files in a directory

Hello Everyone! I trust you are off to a great week! Trying to output the name and count of each uniquely occurring domain in the current directory for a portion of a script I'm building. Here's what I'm stuck on: - Need to find UNIQUE occurences of domains (*@domain.com) in ALL files in... (4 Replies)
Discussion started by: linuxhombre
4 Replies

8. UNIX for Advanced & Expert Users

Extracting the different files from directory & its sub directories

Hi Everyone, It would be helpful if someone helps me on this. Requirement: I have a directory which includes different types of files(for example *.java,*.class),but not restricted for only these types. I need to find the same types of file extensions from its directories and subdirectories... (3 Replies)
Discussion started by: rcvasu
3 Replies

9. UNIX for Dummies Questions & Answers

Find & Copy Selected files to another Directory

I am wanting to find files within a directory that are over a certain number of days old and copy them to another directory. And unfortunately not having much luck.......is someone able to help. Would also like to add that there are literally thousands of files that I am wanting to copy in one... (3 Replies)
Discussion started by: hellfyre
3 Replies

10. Shell Programming and Scripting

How to check if 3 files have same size in directory

I need to determine if any three files have the same file size in a specified directly? I have got as far as listing the file sizes but where to go from here? ls -al |sort -n -r +4 | awq '{print $5}' Thanks in anticipation (5 Replies)
Discussion started by: oggle
5 Replies
Login or Register to Ask a Question