Hi All 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. Thanks Eddie
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)
Thanks Ted (and others) I tried using your script and it worked perfectly. If I may impose on you further, how can I make this script work recursively so that it functions on any subdirectory, while ensuring that it does not enter directories above the one that it is started in? This is not something that I need urgently (or perhaps may never need) so don't lose any sleep over it. Thanks again. Eddie On Monday 26 November 2001 10:32 am, Ted Harding wrote:
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)
Fax-to-email: +44 (0)870 167 1972 Date: 26-Nov-01 Time: 10:32:55 ------------------------------ XFMail ------------------------------
May be this is not a good solution, but you can move files into sub directory for each user.. the command should be find . -u tommy -exec mv {} tommyDir \; Note that the tommyDir must exist before otherwise you'll move all files to a unique file named tommyDir and you'll loose all files or may be replace the mv {} tommyDir command by a shell script to do what you REALLY want. I'll checks how you could do this. HTH Regards -----Message d'origine----- De : Eddie Howson [mailto:eddie@cs.rhul.ac.uk] Envoyé : lundi 26 novembre 2001 10:43 À : suse-linux-e@suse.com Objet : [SLE] shell script help Hi All 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. Thanks Eddie -- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the FAQ at http://www.suse.com/support/faq and the archives at http://lists.suse.com
participants (3)
-
Eddie Howson
-
Pascal Miquet
-
Ted.Harding@nessie.mcc.ac.uk