Rename Files and Directories (Add Prefix)

LinuxPerlShell

Linux Problem Overview


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/

Regards,

Linux Solutions


Solution 1 - Linux

Thanks to https://stackoverflow.com/users/31799/peter-van-der-heijden">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)

Solution 2 - Linux

Use the rename script this way:

$ rename 's/^/PRE_/' *

There are no problems with metacharacters or whitespace in filenames.

Solution 3 - Linux

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.

Solution 4 - Linux

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

Solution 5 - Linux

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.

Solution 6 - Linux

If you have Ruby(1.9+)

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

Solution 7 - Linux

with Perl:

perl -e 'rename $_, "PRE_$_" for <*>'

Solution 8 - Linux

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: $!";
}

Solution 9 - Linux

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

Solution 10 - Linux

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRafaelView Question on Stackoverflow
Solution 1 - LinuxCanSpiceView Answer on Stackoverflow
Solution 2 - LinuxtchristView Answer on Stackoverflow
Solution 3 - LinuxZheng QsinView Answer on Stackoverflow
Solution 4 - LinuxCyclonecodeView Answer on Stackoverflow
Solution 5 - LinuxkoyaeView Answer on Stackoverflow
Solution 6 - LinuxkurumiView Answer on Stackoverflow
Solution 7 - LinuxtadmcView Answer on Stackoverflow
Solution 8 - LinuxJoel BergerView Answer on Stackoverflow
Solution 9 - LinuxconradkleinespelView Answer on Stackoverflow
Solution 10 - LinuxSteinAirView Answer on Stackoverflow