I can't find what I'm looking for in the bash(1) manpage, hoping there's an easy answer...
I want to use a construct like for i in ~/path/*; do something $i && rm $i; done
which works great as long as there are files in ~/path/. However, when the directory is empty, I get: rm: cannot remove `/home/athompso/path/*': No such file or directory
which isn't quite what I want. The quick fix in this case is to use for i in ~/path/*; do [ -f $i ] && something $i && rm $i; done
but once again, that strikes me as inelegant and I can't remember OR find the better way to do it.
Anyone?
Thanks, -Adam
Try this:
for i in `ls /path/to/dir`; do # i is set here, only when it contains something.. echo $i; done;
Adam Thompson writes:
I can't find what I'm looking for in the bash(1) manpage, hoping there's an easy answer...
I want to use a construct like for i in ~/path/*; do something $i && rm $i; done
which works great as long as there are files in ~/path/. However, when the directory is empty, I get: rm: cannot remove `/home/athompso/path/*': No such file or directory
which isn't quite what I want. The quick fix in this case is to use for i in ~/path/*; do [ -f $i ] && something $i && rm $i; done
but once again, that strikes me as inelegant and I can't remember OR find the better way to do it.
Anyone?
Thanks, -Adam
for i in `ls /path/to/dir`; do do something $i; rm $i; done;
If you need the power of regular expressions I would just use grep.. although the way you're using it doesn't look like its necessary. IE
for i in `ls /dev | grep tty`; do echo -n $i; done;
My last reply didn't go through ( I'm guessing due to the length. ) Hence the random blathering at 2AM to lengthen this one...
All the best, Robert Keizer
On 2010-07-06 Adam Thompson wrote:
I can't find what I'm looking for in the bash(1) manpage, hoping there's an easy answer...
I want to use a construct like for i in ~/path/*; do something $i && rm $i; done
which works great as long as there are files in ~/path/. However, when the directory is empty, I get: rm: cannot remove `/home/athompso/path/*': No such file or directory
What you want is:
shopt -s failglob for i in /tmp/A/* /dev/null; do echo $i; done bash: no match: /tmp/A/* # gives an error but doesn't exec the do clause
or
shopt -s nullglob for i in /tmp/A/* ; do echo $i; done # outputs nothing, executes no do clause
Using these options lets you still do sane arg quoting in case there are spaces in filenames, etc.
[sean@sergeant:~]$ mkdir /tmp/nothing [sean@sergeant:~]$ ls /tmp/nothing | while read A; do echo $A; done [sean@sergeant:~]$
Also gets around nasty "argument list too long" messages and doesn't need you to remember arcane syntax. $A is quotable if you have spaces in the filename.
Sean
On Tue, Jul 6, 2010 at 11:07 PM, Adam Thompson athompso@athompso.netwrote:
I can't find what I'm looking for in the bash(1) manpage, hoping there's an easy answer...
I want to use a construct like for i in ~/path/*; do something $i && rm $i; done
which works great as long as there are files in ~/path/. However, when the directory is empty, I get: rm: cannot remove `/home/athompso/path/*': No such file or directory
which isn't quite what I want. The quick fix in this case is to use for i in ~/path/*; do [ -f $i ] && something $i && rm $i; done
but once again, that strikes me as inelegant and I can't remember OR find the better way to do it.
Anyone?
Thanks, -Adam
Roundtable mailing list Roundtable@muug.mb.ca http://www.muug.mb.ca/mailman/listinfo/roundtable