0

Possible Duplicate:
Rename all the files within a folder with prefix “Unix_”
how can I rename multiple files by inserting a character?

Is there a way to make a change in name of some files, for example to append name of each user to files that reside in his home folder that if they moved somewhere can find files of a particular user? I use CentOS 5.5

Sam
  • 2,488

3 Answers3

3

Yes:

$ for file in *; do owner=$(stat -c %U "$file"); mv "$file" "${owner}_${file}"; done

You should use stat -c %U filename because $USER expands to current user, not to a file's owner.

A file's owner could change from one argument to other, hence rename's based solutions might have to cope with /e modifier of substitution operator or be combined with some call to stat. That said, I don't consider this question an exact duplicate of question 13147 as the prefix would be dynamic.

mmoya
  • 6,008
2
for f in * ; do mv "$f" $USER_"$f" ; done
George M
  • 13,959
Mahesh
  • 123
0
for f in * ; do mv "$f" "$f"_$USER ; done
George M
  • 13,959
user unknown
  • 10,482