Renaming Files and Directories (Add Prefix)

I would like to add a prefix for 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 the prefix "PRE _"

 PRE_Hi.jpg PRE_1.txt PRE_folder/ PRE_this.file_is.here.png PRE_another_folder.ok/ 

Hello,

+78
linux shell perl
Jan 24 2018-11-21T00:
source share
9 answers

Thanks to Peter van der Heijden , here, which will work with file names with spaces in them:

 for f in * ; do mv "$f" "PRE_$f" ; done 
+129
Jan 24 2018-11-21T00:
source share

Use the rename script as follows:

 $ rename 's/^/PRE_/' * 

No problems with metacharacters or spaces in file names.

+64
Jan 24 2018-11-21T00:
source share

To add a prefix or suffix for files (directories), you can use the simple and powerful xargs method:

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

It uses the parameter replacement parameter xargs: -I. And you can get more details on the manual page.

+45
Dec 07
source share

This can be done by running the simple find :

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

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

+16
Oct 23 '15 at 7:57
source share

To add a prefix to all files and folders in the current directory using util-linux rename (unlike prename , the Perl version from Debian and some other systems), you can do:

 rename '' <prefix> * 

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

For suffixes you need to use the perl version or use the search .

+7
Jan 28 '16 at 10:15
source share

with Perl:

 perl -e 'rename $_, "PRE_$_" for <*>' 
+6
Jan 25 '11 at 3:40
source share

If you have Ruby (1.9+)

 ruby -e 'Dir["*"].each{|x| File.rename(x,"PRE_"+x) }' 
+5
Jan 24 2018-11-11T00:
source share

Here is a simple script that you can use. I like to use the non-standard File::chdir module to control the cd control operations, so to use this script as-it 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: $!"; } 
+2
Jan 25 2018-11-11T00:
source share

On my system, I do not have a rename command. Here is a simple liner. It finds all 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 
0
Aug 14 '14 at 10:04 on
source share



All Articles