cancel
Showing results for 
Search instead for 
Did you mean: 

Help

SOLVED
Go to solution
Shaf_1
Advisor

Help

I have a bunch of files that I need to add a common pharse at the beginning of each file name. What command should I use to run this for a bunch of files?

e.g.

67898.txt
subject_67898.txt (I want to add subject to every file name)

Thanks
3 REPLIES
Stuart Browne
Honored Contributor
Solution

Re: Help

for FILE in *
do
mv $FILE subject_${FILE}
done

when you're in the directory with the 'bunch of files'.
One long-haired git at your service...
Ross Minkov
Esteemed Contributor

Re: Help


Here is a classic perl script (with some examples to get you started) that I use for renaming files.

================================

#!/usr/bin/perl -w
# rename - rename files with a Perl expression
#
# This is a very short and simple but powerful script for renaming files.
# The script iterates through the @ARGV array and uses Perl's built-in
# rename function rename one file at a time according to a Perl statement
# that is entered by the user at the time the program is executed.
# The script uses eval to compile and execute a user-supplied string used
# to control renaming of the files.
#
#########################################################################
#
# Example 1
#
# rename 's/\.orig$//' *.orig
#
# Example 1 uses a substitute operator to change $_ and eliminate the
# string '.orig' appearing at the end of each file name.
#
#------------------------------------------------------------------------
#
# Example 2
#
# rename 'y/A-Z/a-z/ unless /^Make/' *
#
# Example 2 uses Perl's y operator to change all uppercase characters to
# lowercase characters in $_, with the trailing condition that this only
# be done if the file name starts with 'Make'.
#
#------------------------------------------------------------------------
#
# Example 3
#
# rename '$_ .= ".bad"' *.f
#
# Example 3 concatenates a '.bad' extention onto each file name.
#
#------------------------------------------------------------------------
#
# Example 4
#
# rename 'print "$_: "; s /foo/bar/ if =~ /^y/i' *
#
# Example 4 prompts the user with each file name abd reads the user response
# from the standard input. If the user's response starts with 'y' or 'Y',
# then a substitute operator changes 'foo' to 'bar' in the file name.
#
#------------------------------------------------------------------------
#
# Example 5
#
# find /tmp -name '*~' -print | rename 's|^(.*)/(.+)~$|$1/.#$2|'
#
# Example 5 demonstrates that the rename script can also receive its list
# of file names from via the standard input instead of through command line
# arguments. The find command is used to search for files under the /tmp
# directory whose names end with a '~'. These are backup files created by
# the emacs editor. The find command prints the pathnames to all such files
# and pipes this list of pathnames to the rename script. The rename script
# uses a substitute operator to capture everything in the file name leading
# up to the final '~' and renames the file with a '.#' prefix and no '~' at
# the end. For example a file called /tmp/d/myfile~ is renamed to
# /tmp/d/.#myfile
#
#------------------------------------------------------------------------
#

# use shift to extract the first command line argument - this is the string
# containing the Perl statement(s) to be compiled and executed.
# These Perl operations are stored in a $op variable.

($op = shift) || die "Usage: rename expr [files]\n";

chomp(@ARGV = ) unless @ARGV;

foreach (@ARGV) {
# save the current file name in $was
$was = $_; # $_ is the default loop variable
eval $op; # compile and execute the Perl
# operations submitted by the user
die if $@; # means eval 'failed'
rename($was, $_) unless $was eq $_; # rename is a Perl built-in function
}

================================

HTH,
Ross
Shaf_1
Advisor

Re: Help

Thanks for the help!