[NTLUG:Discuss] How to get mv to not report when there are no files?
Robert Pearson
e2eiod at gmail.com
Wed Nov 7 00:42:08 CST 2007
On Nov 7, 2007 12:22 AM, Leroy Tennison <leroy_tennison at prodigy.net> wrote:
> Fred James wrote:
> > Neil Aggarwal wrote:
> >
> >> Hello:
> >>
> >> I have a cron job with this command:
> >> mv /home/johnc/* /var/www/html/incoming
> >>
> >> If there is a file in the /home/johnc directory, I get
> >> no output, but when the directory is empty, I get this
> >> notification to the root email account:
> >>
> >> mv: cannot stat `/home/johnc/*': No such file or directory
> >>
> >> This job runs every 5 minutes so this is annoying.
> >>
> >> Is there a way to supress that output?
> >>
> >> Thanks,
> >> Neil
> >>
> >>
> > Neil Aggarwal
> >
> > mv /home/johnc/* /var/www/html/incoming 2> /dev/null
> > ... redirects the stderr to /dev/null (the special file that never contains anything no matter how much you put into it).
> >
> > On my system that would man null, for more information
> > Regards
> > Fred James
> >
> >
> >
> > _______________________________________________
> > http://www.ntlug.org/mailman/listinfo/discuss
> >
> I was going to reply naively
>
> if [ -f /home/johnc/* ]; then ...
Wouldn't this return either a file name, a list of file names or nothing?
In the case of nothing it would fail with an error.
"-f" also checks to see if any file it finds is a regular file. Not a
directory or special file.
Maybe a working solution would be to use the negation...
"if [ ! -f /home/johnc/* ]; then ..." or "if [ ! -e /home/johnc/*
]; then ..."
The full test line I used was...
"if [ ! -f ~/tmp/* ]; then echo "No Files Found" ; fi"
The space between the "!" and [-f,-e] is necessary. I get an error
message otherwise.
>
> but fortunately I tested before I did. The best I could come up with
> quickly was
>
> for i in /home/johnc/*; do if [ -f "$i" ] mv ...
>
> which leads to the question "Is there no wildcard file existence test in
> bash?"
The "*" wildcard is alive and well in bash, according to my bash shell book.
> _______________________________________________
> http://www.ntlug.org/mailman/listinfo/discuss
>
More information about the Discuss
mailing list