193

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/
1
  • Use AWK as on this answer: awk '$0="prefix"$0' file > new_file
    – Mark
    Jan 15, 2019 at 18:20

11 Answers 11

302

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)

8
  • Heh, true, this will completely not work if you have a file with a space in it.
    – CanSpice
    Jan 24, 2011 at 21:37
  • 14
    If you change the ls command to * and put double quotes around the arguments to mv, it will work for files containing spaces.
    – heijp06
    Jan 24, 2011 at 21:45
  • @CanSpice Is it possible to reverse this operation (remove the prefix) with a similar script?
    – Pascal Qyy
    Jul 7, 2012 at 9:46
  • 5
    Thanks, mate. The intern here was loosing his finger caps renaming all 67 files. Cheers.
    – Felipe
    Feb 26, 2016 at 18:14
  • 1
    It should be "mv --" not just "mv", to guard against files starting with dashes, that would otherwise be interpreted as command switches. I will contribute an edit to correct this.
    – Jacob C.
    Jul 31, 2018 at 18:45
106

Use the rename script this way:

$ rename 's/^/PRE_/' *

There are no problems with metacharacters or whitespace in filenames.

3
  • 4
    Some systems have a util-linux utility called "rename" which works differently (on Ubuntu it's called "rename.ul"). Jan 24, 2011 at 23:07
  • rename has always helped me handle multiple file renames very easily. Feb 12, 2011 at 17:17
  • Had issue with @ symbol in prefix. Needed to \@ but that was easy. Worked well.
    – null
    Dec 13, 2020 at 11:12
80

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.

3
  • 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 7, 2012 at 9: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, 2013 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 Dec 3, 2019 at 16:55
53

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

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.

2
  • util-linux on Debian Stretch seems to provide this as /usr/bin/rename.ul. Dec 13, 2018 at 5:39
  • This doesn't work
    – Jonathan
    May 11, 2022 at 21:32
7

If you have Ruby(1.9+)

ruby -e 'Dir["*"].each{|x| File.rename(x,"PRE_"+x) }'
7

with Perl:

perl -e 'rename $_, "PRE_$_" for <*>'
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
1
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: $!";
}
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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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