On 26-Nov-01 Eddie Howson wrote:
Can some shell script guru help me with creating a scipt that takes the owners of a file and renames that file prefixing it with the owners name and an underscore? For example if the file work.txt is owned by tommy, the file is renamed tommy_work.txt. The directory holds a number of files owned by different users.
#! /bin/bash
ls -l | awk '
/^total/{next}
{command=sprintf("mv %s %s_%s",$9,$3,$9);
system(command)}'
Hope this helps!
Ted.
PS As it stands, you don't want to run this too
often on the same directory, e.g. to update it
when new files come in. Otherwise you'll end up
with a lot of files named
tommy_tommy_tommy_tommy_tommy_tommy_...
The above script could be adapted to recognise
filenames ($9) beginning with "user_" ($3_)
and ignore them, if that would be useful.
E.g. you could insert the line
(index($9,$3 "_")==1){next}
above the line which composes the command.
--------------------------------------------------------------------
E-Mail: (Ted Harding)