6

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

camh
  • 38,261
  • 8
  • 74
  • 62
user7470
  • 61
  • 1
  • 1
  • 2

5 Answers5

15
$ for f in * ; do mv "$f" Unix_"$f" ; done
Warren Young
  • 71,107
  • 16
  • 178
  • 168
7

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
dj_segfault
  • 183
  • 6
3

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.

yibe
  • 461
  • 1
  • 4
  • 5
3

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_ *

Random832
  • 10,476
  • 1
  • 34
  • 40
0

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).

Charlie
  • 101
  • 4