shell script to check for any excel files

February 23, 2011 at 08:11:58
Specs: Linux
I would like to write a korn shell script to look in a directory for any excel files. If there are new excel files, I would like to run some commands. Can someone please help me do this?


See More: shell script to check for any excel files

February 23, 2011 at 08:57:24
What is your definition of new? The following find command finds all files with an xls extension created/modified in the last day:

cd <your directory>
find . -type f -name "*.xls" -mtime -1 -print

Report •

February 23, 2011 at 11:07:31
I apologize for the term new.

I wrote the following code to determine the number of .xls files in my_dir. I would not like to know how to store that number to use in an IF statement afterwards.

ls /my_dir/*.xls | wc -l

Can you please show me how best to store the results in a variable rather than print them?

Report •

February 23, 2011 at 11:26:51
Use command substiution, $(..) to capture the output of a command into a bash shell variable:


# untested
myvar=$(/my_dir/*.xls|wc -l)
echo $myvar

Report •

Related Solutions

February 23, 2011 at 13:17:12
This does not seem to be working for me. Does it matter that I am using a korn shell? Here is the contents of my executable file:


myvar = $(/my_dir/*.xls | wc -l)

echo $myvar

if[$myvar -gt 0]


mail -s "This is the subject, no attachment" < data_analysis_workbook16.log


Report •

February 23, 2011 at 13:40:54
The shell - both ksh and bourne - is very particular about spaces and the lack there of. If $myvar isn't correct, try removing the spaces around the equal sign:

myvar=$(/my_dir/*.xls | wc -l)

If the if statement isn't functioning correctly, insert spaces around the brackets:

if [ $myvar -gt 0 ]

Report •

February 23, 2011 at 14:00:00
Beautiful! Thank you so much for your help. You made my day!

Report •

February 23, 2011 at 15:57:34

Likewise. Thank you for your kind words.

Report •

February 24, 2011 at 07:23:48
One more quick question if you do not mind. The whole purpose of this check is to see if there are any xls files and if so then run some commands. If there are none, I don't want the script to do anything. When I run the script on a cron job and there are no xls files, I get the following emailed to me.

/my_dir/*.xls: No such file or directory

How can I shut that message off, or do something so that I do not get the email? If there are no xls files, I am happy and do not want to be notified at all.

Thanks again,

Report •

February 24, 2011 at 08:30:25
In *nix, there are 3 file descriptors - standard output, input, and error. The ls command you describe writes to standard error when no xls files exist. This command redirects stderr to /dev/null:

ls -l /my_dir*.xls 2> /dev/null

You could also redirect to a file if you wish.

Report •

February 24, 2011 at 09:06:53
Thank you for your help, but that is not doing what I need it to either.

Here is my script that I run in the cron job. If this script runs and there are not any xls files in my_dir, I get a cron deamon email saying that /my_dir/*.xls: not found. I do not want to get these cron deamon errrors. I am not sure how to ignore that error or what to do to keep from getting these emails. Thanks again for your help.

myvar=$(/my_dir/*.xls | wc -l)
echo $myvar

if [ $myvar -gt 0 ]
mail -s "Test" < data.log

Report •

February 24, 2011 at 09:24:12
This should redirect that "file not found error" to /dev/null:

myvar=$(ls /my_dir/*.xls 2> /dev/null |wc -l)

If that doesn't do it, try redirecting standard error from cron. Suppose your script is called myscrpt, In cron try something like this:

# untested
* * * * * myscript 2> /dev/null

Report •

February 24, 2011 at 11:00:10
Again, perfect! You made my day! Thank you so much, Betsy

Report •

Ask Question