On Wed, 26 May, 2010 at 08:07:21 +0200, Per Jessen wrote:
David C. Rankin wrote:
Guys,
Doing a quick backup of config files (like .bashrc) from within bashrc and I need to find a better way to find the total number of files matching a glob in a directory. Currently I'm using:
total=$(ls $budir/bashrc-* | wc -l)
to determine how many copies I have in my bashrc.tar.bz2 backup + the new one I just copied to the backup location so I can determine the number to delete to just save the last 3 when I tar it up again.
The ls | wc combination works fine, but I don't like the fact that there is a pipe in the middle of it. If there is a cleaner way to do it, let me know.
I can't get rid of the pipe for you, but personally I'd do:
total=$(find $budir -iname bashrc-* -type f | wc -l)
As long as the glob matches (exactly) it can be done within bash, using an array. Something on the order of; array=(path/to/glob*) total=${#array[@]} Haven't tested it, but I'm pretty sure files with whitespace will break this, but you get the idea hth /jon -- YMMV -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org