Re: shell script removing log files.
On Lu, 22 dec 14, 10:51:26, pe...@easthope.ca wrote: > This command in a shell script removes unwanted log files. > > for i in $( echo *.Log ); do > /bin/rm $i; > echo "Removed $i." > done > > In the edge case of no matching files, rm complains. > /bin/rm: cannot remove `*.Log': No such file or directory > > If echo is replaced with ls, it complains when there > is no match. > > Does anyone have a tidy solution for this task? find -name '*.Log' -delete Kind regards, Andrei -- http://wiki.debian.org/FAQsFromDebianUser Offtopic discussions among Debian users and developers: http://lists.alioth.debian.org/mailman/listinfo/d-community-offtopic http://nuvreauspam.ro/gpg-transition.txt signature.asc Description: Digital signature
Re: shell script removing log files.
On Mon, Dec 22, 2014 at 10:51:26AM -0800, pe...@easthope.ca wrote: > This command in a shell script removes unwanted log files. > > for i in $( echo *.Log ); do > /bin/rm $i; > echo "Removed $i." > done > > In the edge case of no matching files, rm complains. > /bin/rm: cannot remove `*.Log': No such file or directory > > If echo is replaced with ls, it complains when there > is no match. > > Does anyone have a tidy solution for this task? First of all, you have an unnecessary use of "echo". Although seemingly innocuous, if you have file names with spaces, the spaces will mess things up as things will be going through the shell twice. Which is once too many. Second, remember that when the shell performs pathname expansion, it will leave the original pattern in there if nothing matches. Hence the error message you see. This should do the trick: for i in *.Log ; do if test -f "$i"; then rm "$i" echo Removed $i. fi done or if you want to make things *really* simple: rm --force --verbose *.Log :-) -- Karl E. Jorgensen -- To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org Archive: https://lists.debian.org/2014101542.GB5035@hawking
Re: shell script removing log files.
On 22/12/14 19:51, pe...@easthope.ca wrote: > This command in a shell script removes unwanted log files. > > for i in $( echo *.Log ); do > /bin/rm $i; > echo "Removed $i." > done > > In the edge case of no matching files, rm complains. > /bin/rm: cannot remove `*.Log': No such file or directory > > If echo is replaced with ls, it complains when there > is no match. > > Does anyone have a tidy solution for this task? rm --verbose --force *.Log hth, Jerome > > Thanks, ... Peter E. > > > -- To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org Archive: https://lists.debian.org/549870b1.6030...@rezozer.net
Re: shell script removing log files.
On 12/22/2014 at 01:51 PM, pe...@easthope.ca wrote: > This command in a shell script removes unwanted log files. > > for i in $( echo *.Log ); do > /bin/rm $i; > echo "Removed $i." > done > > In the edge case of no matching files, rm complains. > /bin/rm: cannot remove `*.Log': No such file or directory > > If echo is replaced with ls, it complains when there > is no match. > > Does anyone have a tidy solution for this task? I'm not quite sure just what it is that you're wanting to do, but if my guess is correct, this should get the job done: if [ -e "/path/to/*.log" ] ; then for i in /path/to/*.log ; do /bin/rm "$i" &>/dev/null && echo "Removed $i." ; done fi That should run the removal loop only if there is at least one log file present to be removed, and report removal only if the removal actually succeeded - rather than claiming success even if, e.g., the file could not be removed because appropriate write permission was missing. If that isn't what you want to do, then you'll need to explain better what it is you're trying to accomplish. -- The Wanderer The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore all progress depends on the unreasonable man. -- George Bernard Shaw signature.asc Description: OpenPGP digital signature