How to rename multiple file names?

Hi all,

I need to rename more file name in one command or script.
The files have this structure:

XxY - filename.doc

where X and Y are numbers and the x is the letter itself.

I need to rename these files with this structure:

string.S0XEY.filename.doc

the string is a suffix that changes time to time.

Renaming can be done using mv command. But you cannot rename multiple files at once. You need to script it. I can think of one solution:
Have two arrays with the values of X and Y. Loop over them and for each value of X and Y, rename the file to desired format.
Try it out and let us know.

Something like this:

X=( 1 2 3 4 5 )
Y=( 2 3 4 5 6 )
idx=0
while idx < length_of_array
do
    mv "form_source_file_name_string_yourself" "form_destination_file_name_string_yourself"
    increment idx
done

Store all file name in one temporary file

ls *filename*.doc > Temp.tmp

Now use the loop to remove value line by line

For Line in $(cat Temp.tmp);
do mv Line "yourSting".line;
done

This will give a good idea on how to do it

Try

ls *.doc | while read FN; do echo mv "$FN" "string.S0${FN:0:1}E${FN:2:1}.${FN##* }"; done

; remove the echo when happy.

This is bad advice. That is a useless use of cat and a useless use of ls * and a useless use of backticks, not to mention a dangerous use of backticks! Also, a pointless use of temporary file. I often see two or three of these habits, but to see all five of them simultaneously is interesting. Remember that ls doesn't expand *, the shell does, so you can feed it straight into for.

A simpler and safer version is

for FILE in *filename*.doc
do
        echo mv "$FILE" some-destination-string
done

Remove the echo once you've gotten some-destination-string as exactly what you want it to be.

Rudic, yours is better, but there's no point using a pipe there -- too many arguments for for would also be too many arguments for ls, it doesn't help you.

What i do, looks like:
(might be a little overkill for such a simple substitution, but for certain sed commands, this preview is just 'required' (feels alot more saver)!
(as in, i would had deleted (renamed to empty) quite a few files already)

:) vid $ cmd=tui-echo
+ vid $ for f in sc* ;do $cmd "$f" "${f/screen/Desktop}"; done
# | screen-.mkv                                                                                     Desktop-.mkv | #
# | screen-out.0.mkv                                                                           Desktop-out.0.mkv | #
# | screen-out.10.mkv                                                                         Desktop-out.10.mkv | #
# | screen-out.11.mkv                                                                         Desktop-out.11.mkv | #
# | screen-out.11.webm                                                                       Desktop-out.11.webm | #
# | screen-out.1.mkv                                                                           Desktop-out.1.mkv | #
# | screen-out.2.mkv                                                                           Desktop-out.2.mkv | #
# | screen-out.3.mkv                                                                           Desktop-out.3.mkv | #
# | screen-out.4.mkv                                                                           Desktop-out.4.mkv | #
# | screen-out.5.mkv                                                                           Desktop-out.5.mkv | #
# | screen-out.6.mkv                                                                           Desktop-out.6.mkv | #
# | screen-out.7.mkv                                                                           Desktop-out.7.mkv | #
# | screen-out.8.mkv                                                                           Desktop-out.8.mkv | #
# | screen-out.9.mkv                                                                           Desktop-out.9.mkv | #
# | screen-out.mkv                                                                               Desktop-out.mkv | #
+ vid $ cmd=mv
+ vid $ for f in sc* ;do $cmd "$f" "${f/screen/Desktop}"; done
 vid $ ll D*{mkv,webm}
-rw-rw-r--. 1 sea sea 1.8M 28. M�r 23:44 Desktop-.mkv
-rw-rw-r--. 1 sea sea 593M  7. M�r 23:17 Desktop-out.0.mkv
-rw-rw-r--. 1 sea sea 267K  4. Mai 10:05 Desktop-out.10.mkv
-rw-rw-r--. 1 sea sea  15M  4. Mai 10:10 Desktop-out.11.mkv
-rw-rw-r--. 1 sea sea  13M  4. Mai 11:15 Desktop-out.11.webm
-rw-rw-r--. 1 sea sea 318K 27. M�r 16:39 Desktop-out.1.mkv
-rw-rw-r--. 1 sea sea 1.1M 28. M�r 08:04 Desktop-out.2.mkv
-rw-rw-r--. 1 sea sea 2.5M 28. M�r 12:29 Desktop-out.3.mkv
-rw-rw-r--. 1 sea sea 1.5M 28. M�r 12:35 Desktop-out.4.mkv
-rw-rw-r--. 1 sea sea 805K 28. M�r 17:31 Desktop-out.5.mkv
-rw-rw-r--. 1 sea sea 789K 28. M�r 17:42 Desktop-out.6.mkv
-rw-rw-r--. 1 sea sea  73M 30. M�r 11:13 Desktop-out.7.mkv
-rw-rw-r--. 1 sea sea  98M  4. Apr 11:23 Desktop-out.8.mkv
-rw-rw-r--. 1 sea sea  19M 30. M�r 12:26 Desktop-out.9.mkv
-rw-rw-r--. 1 sea sea  70M 27. M�r 15:11 Desktop-out.mkv
:) vid $ 

hth & hf

EDIT:
What i'm saying is:
The more files you have (to rename), the more important it is to PRE-view the changes that will be done.
And having the strings aligned left and right of the screen/window just helps to compare, rather than having a list that is just space delimited. imho

All you need to do is to define a 'prefix' string, and add it in front of RudiC's example.

Hi.

For Debian (this from Jessie):

NAME
       rename - renames multiple files

SYNOPSIS
       rename [ -h|-m|-V ] [ -v ] [ -n ] [ -f ] [ -e|-E perlexpr]*|perlexpr
       [ files ]

... -- see man rename

For RedHat (this from Fedora 19, similar one available from CentOS 6.4 )

NAME
       rename - rename files

SYNOPSIS
       rename [options] expression replacement file...

DESCRIPTION
       rename  will  rename  the specified files by replacing the first occur
       rence of expression in their name by replacement.

You may also be able to find an old shell script:

# @(#) mved     Rename, change filename by mv-ing with special pattern, =.
# $Id: mved 291 2007-10-23 11:26:32Z dennisl $
# See: http://raf.org/mved/

# From comp.sources.wizards & Martin Marietta at ornl, off Usenet.
# 90.09.27.
# mved.sh
# Move-and-edit filenames.
#
#       Usage: mved [-n] from-pattern to-pattern
...

Note that the three are different from one another.

Best wishes ... cheers, drl

F22: dnf info rename shows nothing.
man rename , shows the same though :stuck_out_tongue:

Where the heck did that come from?
I'm 120% sure that rename was one of the first commands i've tried in the shell, and (in 2011) it showed "command not found"...

Hi, sea.

Perhaps you need to install:

-- https://admin.fedoraproject.org/pkgdb/package/renameutils/

On my F19:

renameutils.x86_64 : A set of programs to make renaming and copying of files
                   : easier

Claims approved for F22 and earlier ... cheers, drl

---------- Post updated May 7th, 2015 at 06:38 ---------- Previous update was May 6th, 2015 at 13:38 ----------

Hi, sea.

Perhaps more usefully (all on Fedora 19):

yum whatprovides rename

produces (in part):

Loaded plugins: langpacks, refresh-packagekit
util-linux-2.23.1-3.fc19.x86_64 : A collection of basic system utilities
Repo        : fedora
Matched from:
Filename    : /usr/bin/rename
...

And also:

yum info ren

produces:

Loaded plugins: langpacks, refresh-packagekit
Available Packages
Name        : ren
Arch        : x86_64
Version     : 1.0
Release     : 18.fc19.2.1
Size        : 14 k
Repo        : fedora/19/x86_64
Summary     : Rename multiple files
URL         : http://linux.maruhn.com/sec/ren.html
License     : Public Domain
Description : Whereas mv can rename (as opposed to move) only one file at a
            : time, ren can rename many files according to search and
            : replacement patterns, ala VMS and MS-DOS (but better). ren checks
            : for replacement name collisions and handles rename chains (1 goes
            : to 2 goes to 3 etc.) gracefully.

Best wishes ... cheers, drl