198

I would like to add prefix on all folders and directories.

Example:

I have

Hi.jpg
1.txt
folder/
this.file_is.here.png
another_folder.ok/

I would like to add prefix "PRE_"

PRE_Hi.jpg
PRE_1.txt
PRE_folder/
PRE_this.file_is.here.png
PRE_another_folder.ok/
brian d foy
  • 129,424
  • 31
  • 207
  • 592
Rafael
  • 1,989
  • 2
  • 12
  • 3
  • Use AWK as on [this](https://stackoverflow.com/a/2099492/9874050) answer: awk '$0="prefix"$0' file > new_file – Mark Jan 15 '19 at 18:20

11 Answers11

308

Thanks to Peter van der Heijden, here's one that'll work for filenames with spaces in them:

for f in * ; do mv -- "$f" "PRE_$f" ; done

("--" is needed to succeed with files that begin with dashes, whose names would otherwise be interpreted as switches for the mv command)

Jacob C.
  • 345
  • 1
  • 16
CanSpice
  • 34,814
  • 10
  • 72
  • 86
109

Use the rename script this way:

$ rename 's/^/PRE_/' *

There are no problems with metacharacters or whitespace in filenames.

tchrist
  • 78,834
  • 30
  • 123
  • 180
82

For adding prefix or suffix for files(directories), you could use the simple and powerful way by xargs:

ls | xargs -I {} mv {} PRE_{}

ls | xargs -I {} mv {} {}_SUF

It is using the paramerter-replacing option of xargs: -I. And you can get more detail from the man page.

Zheng Qsin
  • 1,463
  • 11
  • 6
  • 3
    P.S.: If you just want to **rename part files** (directories) of current directory, just filter it before xargs, such as: `ls *.old | xargs -I {} mv {} PRE_{}` – Zheng Qsin Dec 07 '12 at 09:17
  • interestingly using rename did not work. using xargs went well for my RHEL setup so +1 for this option, makes it easy to understand the command – Acewin Nov 19 '13 at 19:05
  • Doesn't work if filename contains single quote, fails with: xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option – Evgeny Veretennikov Dec 03 '19 at 16:55
54

This could be done running a simple find command:

find * -maxdepth 0 -exec mv {} PRE_{} \;

The above command will prefix all files and folders in the current directory with PRE_.

Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
15

To add a prefix to all files and folders in the current directory using util-linux's rename (as opposed to prename, the perl variant from Debian and certain other systems), you can do:

rename '' <prefix> *

This finds the first occurrence of the empty string (which is found immediately) and then replaces that occurrence with your prefix, then glues on the rest of the file name to the end of that. Done.

For suffixes, you need to use the perl version or use find.

Community
  • 1
  • 1
koyae
  • 720
  • 7
  • 12
7

If you have Ruby(1.9+)

ruby -e 'Dir["*"].each{|x| File.rename(x,"PRE_"+x) }'
kurumi
  • 25,121
  • 5
  • 44
  • 52
7

with Perl:

perl -e 'rename $_, "PRE_$_" for <*>'
tadmc
  • 3,714
  • 16
  • 14
3

On my system, I don't have the rename command. Here is a simple one liner. It finds all the HTML files recursively and adds prefix_ in front of their names:

for f in $(find . -name '*.html'); do mv "$f" "$(dirname "$f")/prefix_$(basename "$f")"; done
conradkleinespel
  • 6,560
  • 10
  • 51
  • 87
2

Here is a simple script that you can use. I like using the non-standard module File::chdir to handle managing cd operations, so to use this script as-is you will need to install it (sudo cpan File::chdir).

#!/usr/bin/perl

use strict;
use warnings;

use File::Copy;
use File::chdir; # allows cd-ing by use of $CWD, much easier but needs CPAN module

die "Usage: $0 dir prefix" unless (@ARGV >= 2);
my ($dir, $pre) = @ARGV;

opendir(my $dir_handle, $dir) or die "Cannot open directory $dir";
my @files = readdir($dir_handle);
close($dir_handle);

$CWD = $dir; # cd to the directory, needs File::chdir

foreach my $file (@files) {
  next if ($file =~ /^\.+$/); # avoid folders . and ..
  next if ($0 =~ /$file/); # avoid moving this script if it is in the directory

  move($file, $pre . $file) or warn "Cannot rename file $file: $!";
}
Joel Berger
  • 20,180
  • 5
  • 49
  • 104
1

This will prefix your files in their directory.

The ${f%/*} is the path till the last slash / -> the directory

The ${f##*/} is the text without anything before last slash / -> filename without the path

So that's how it goes:

for f in $(find /directory/ -type f); do 
  mv -v $f ${f%/*}/$(date +%Y%m%d)_Prefix_${f##*/}
done
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
SteinAir
  • 96
  • 3
0

Open cmd and set the directory to the folder and run the following command:

for /f "tokens=*" %a in ('dir /b') do ren "%a" "00_%a"

00_ is prefix in "00_%a", so you can change it according to your requirements. It will rename all of the files in the selected folder.

Koedlt
  • 4,286
  • 8
  • 15
  • 33
m.imran38
  • 11
  • 4