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.

Rename all the files within a folder with prefix “Unix_” i.e. suppose a folder has two files a.txt and b.pdf than they both should be renamed from a single command to Unix_a.txt and Unix_b.pdf

share|improve this question
2  
I would call it a prefix, not a suffix. –  jlliagre May 14 '11 at 4:53
add comment

5 Answers

$ for f in * ; do mv "$f" Unix_"$f" ; done
share|improve this answer
add comment

The rename command can rename files using regular expressions, which makes it very powerful. In your case, you could do

rename 's/(.*)/Unix_$1/' *.txt
share|improve this answer
    
... and the pdf-File? :) –  user unknown May 15 '11 at 3:40
    
rename 's/(.*)/Unix_$1/' *.pdf –  fromnaboo Jul 3 '12 at 1:51
    
No need to capture, just replace the beggining-of-string: rename 's/^/Unix_/' *.pdf –  mmoya Nov 12 '13 at 23:03
add comment

If you're using Zsh as your shell, you could also use the function zmv.

Add this line to your .zshrc:

autoload -U zmv

then you could run:

% zmv -W '*' 'Unix_*'

See man zshcontrib for further information.

share|improve this answer
    
The more I read about zsh, the more I like it. –  boehj May 15 '11 at 3:34
add comment

With the rename utility included in the util-linux package (the one on dj_segfault's answer comes from perl), you could do rename '' Unix_ *

share|improve this answer
add comment

Some of the other answers might be better however, if I thought that xargs deserved a mention since it is a very powerful tool (and on many systems):

In this particular you could do:

ls | xargs -n1 -I{} mv {} Unix_{}

Edit: Retracted per Gilles' comment. For this situation this solution should be considered only a hack due to the caveats as pointed out by the cited article. The other answers are much better. I still think that xargs is still a useful tool (I use it with svn status relatively frequently), but he's right, for simple execute some command on all files in a tree of directories, this isn't the answer and find is much better. (Leaving the answer since the I think the comment is good for people who'd make the same mistake).

share|improve this answer
2  
Don't parse the output of ls. xargs is actually rarely useful, especially now that find … -exec … + exists. –  Gilles May 14 '11 at 23:11
    
Thanks @Gilles that's a good article. Edited retracting my answer. –  Charlie May 15 '11 at 1:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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