How to change some GID
ZIYAD A. M. AL-BATLY
zamb at spymac.com
Sat Apr 2 09:11:23 UTC 2005
On Sat, 2005-04-02 at 10:33 +0200, Sebastian Müsch wrote:
> Hallo,
>
> Once upon a time Sebastian Müsch wrote:
> > This simple bash-script appended to this mail
>
> Uups ... did some mistakes while writing the script (too fast).
> The corrected script is appended. Another advice, please don't use this
> script for groups with to many files, as the file list is written to memory
> first before processing (piping or redirecting to a file and then processing
> linewise would be the better way, but for now the script would be enough)
> ;-)
>
>
> cu
> Sebastian
>
This message is entirely for learning (no rudeness intended) and somehow
off-topic.
Your script has some flaws, first: it is very heavy on the system (as
you mentioned), second: it can't handle files/dirs with spaces in them.
Change:
for i in `find / -group $oldgid` ; do
if $DEBUG ; then
echo "Changing ${i} ..."
fi
chgrp -f $newgid "$i" 2> /dev/null
retval=$?
if [ $retval -ne 0 ] ; then
echo "Error while changing gid for file ${i}, chgrp returned
error $retval!" >&2
problem=true
elif $DEBUG ; then
echo "Successfully changed gid"
fi
done
To:
find / -group $oldgid -print0 | xargs -0 chgrp -f $newgid
And that's it! By using "xargs" ("man xargs") you only run one process
of "chgrp" for all the files/dirs (unlike the original script which run
"chgrp" for each file/dir). And by passing "-print0" for "find" and "-0"
for "xargs", files/dirs with weird chars (like spaces) will work
flawlessly.
Ziyad.
More information about the ubuntu-users
mailing list