Detect & move Multiple files

Hewlett-packard / Dc7100u
February 6, 2009 at 04:12:25
Specs: RHEL4, P4 2.8GHz
I'm trying to check if any log files exist and if so move them to a different directory. The problem is that the filename is only diferenciated by the date (commonname_YYMMDD.log) and there may be a few months worth of logs.
I have tried the following:-
if [ -e commonname_*.log ] then
mv commonname_*.log logs

But get an error saying too many arguements. Any help would be greatly appreciated

See More: Detect & move Multiple files

Report •

February 6, 2009 at 09:56:19
When it tries to evaluate

if [ -e commonname_*.log ]

the Shell first expands the wildcard to produce a space-separated list of all the individual files. Since the -e expression only requires one filename, that's too many arguments. You need to prevent expansion either by escaping the * or enclosing in 'quotes':

if [ -e commonname_\*.log ]


if [ -e 'commonname_*.log' ]

Report •

February 13, 2009 at 02:37:09
Unfortunately neither of these seem to work.

I tried it with a directory as follows:-


I need to search the folder for existance of log files and if they exist then move them for safe storage.

Neither of the examples prove true and mine expands, as you said, so has too many arguements.

Report •

February 13, 2009 at 15:15:45
Sorry, I misled you. The if [ -e file ] statement doesn't accept wildcards. I can't think of a good way of testing if a file exists. There must be, so I hope someone who is more experienced at bash scripting will step in.

Report •

Related Solutions

February 13, 2009 at 21:21:54
oops! wrong OS

Report •

February 16, 2009 at 02:49:27
No, M2, Chris is using the right OS, Red Hat Enterprise Linux, great choice, it's just a pity Bash scripting doesn't give you the same expressive power as Windows bat scripting ;-)

(ducks for cover from flying projectile memory sticks)

Report •

Ask Question