Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question

marked as duplicate by jasonwryan, sr_, Chris Down, Mat, Mikel May 18 '12 at 14:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers 3

up vote 3 down vote accepted

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.

share|improve this answer
for f in * ; do mv "$f" $USER_"$f" ; done
share|improve this answer
1  
exact duplicate from unix.stackexchange.com/a/13149/18275 –  Jameelopix May 18 '12 at 10:51
    
yes, it is. helps to have an answer here for lazy ones. marking CW. –  Mahesh May 18 '12 at 17:16
for f in * ; do mv "$f" "$f"_$USER ; done
share|improve this answer
1  
exact duplicate from unix.stackexchange.com/a/13149/18275 –  Jameelopix May 18 '12 at 10:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.