Moving and renaming multiple files in a directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Moving and renaming multiple files in a directory
# 1  
Old 01-22-2013
Moving and renaming multiple files in a directory

Hi.

I am trying to automate the movement and renaming of a number of files in a directory. I am using the 'mv' command as I do not have access to 'rename'. I have the following scripted
Code:
FILES=$(ls /transfer/move/sys/mail/20130123/)
	if [ ! -z "${FILES}" ] ; then
		for i in ${FILES} ; do
			mv /transfer/move/sys/mail/20130123/${FILES} /transfer/archive/sys/mail/20130123/MYFILENAME_`date +%Y%m%d%M%S00`.dat ; done
	fi

which works perfectly when there is only one file in the directory, but when there are multiple files I get the following error
Quote:
Usage: mv [-I] [-i | -f] [-E{force|ignore|warn}] [--] src target
or: mv [-I] [-i | -f] [-E{force|ignore|warn}] [--] src1 ... srcN directory
Usage: mv [-I] [-i | -f] [-E{force|ignore|warn}] [--] src target
or: mv [-I] [-i | -f] [-E{force|ignore|warn}] [--] src1 ... srcN directory
.

I have attempted to add a sleep to the command, but the same error appears.

Can anyone advise what I am doing wrong, or even suggest a better way of achieving this?

Thanks in advance.
# 2  
Old 01-22-2013
You should use $i instead of $FILES on the mv commandline.

If time with seconds is just for unique filenames try mktemp instead:

Code:
DIR=/transfer/move/sys/mail/20130123
FILES=$(ls $DIR)
if [ ! -z "${FILES}" ] ; then
    for i in ${FILES} ; do
        mv -f $DIR/$i $(mktemp --suffix=.dat $DIR/MYFILENAME_XXXXXX)
    done
fi

# 3  
Old 01-22-2013
Thanks Chubler_XL.

Unfortunately mktemp does not work and teh following error is returned
Quote:
mktemp: not found
Usage: mv [-I] [-i | -f] [-E{force|ignore|warn}] [--] src target
or: mv [-I] [-i | -f] [-E{force|ignore|warn}] [--] src1 ... srcN directory
TEST.ksh[12]: mktemp: not found
Usage: mv [-I] [-i | -f] [-E{force|ignore|warn}] [--] src target
or: mv [-I] [-i | -f] [-E{force|ignore|warn}] [--] src1 ... srcN directory
I have modified your suggestion to the following
Code:
DIR=transfer/move/sys/mail/20130123/
FILES=$(ls $DIR)
if [ ! -z "${FILES}" ] ; then
    for i in ${FILES} ; do
        mv $DIR/$i /transfer/archive/sys/mail/20130123/MYFILENAME_`date +%Y%m%d%M%S00`.dat
    done
fi

but if there is more than 1 file in the source directory, it will only transfer one across and rename it, discarding the remaining files.

Any other ideas?
# 4  
Old 01-22-2013
mktemp is not available on all systems, it's not POSIX.

How about this?

Code:
DIR=/transfer/move/sys/mail/20130123
FILES=$(ls $DIR)
if [ ! -z "${FILES}" ] ; then
    for i in ${FILES} ; do
        F=$DIR/MYFILENAME_$(openssl rand -hex 4).dat
        while [ -f $F ]
        do
            F=$DIR/MYFILENAME_$(openssl rand -hex 4).dat
        done
        mv $DIR/$i $F
    done
fi

# 5  
Old 01-22-2013
Thank you Chubler_XL, that works perfectly!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script for renaming and moving Files - Easy?

Hey guys, ive been working on this for about 2hrs now - without any solution. At first I need to say I dont have skills in linux bash scripting, but I tried to use some codesnippets and manuals from google. What I want to do: I have different folders including 2 different filestypes with... (15 Replies)
Discussion started by: peter1337
15 Replies

2. UNIX for Dummies Questions & Answers

Moving and renaming files

I have a directory full of directories, say called A B C D E .... In each of these directories there are files called 1.dsp 2.dsp 3.dsp ..... along with others (with different extensions) I need to go through each of these directories and move the dsp file to another folder, but with the name now... (6 Replies)
Discussion started by: claire.a
6 Replies

3. Shell Programming and Scripting

Renaming multiple files in a directory

Hello, I would like to rename all available files in a directory from Filename to Filename_Normal. I tried to use below script but it is giving some error: #!/bin/sh for i in `ls` do echo Changing $i mv $i $i_Normal done Error received: Usage: mv src target or: mv ... (10 Replies)
Discussion started by: manishdivs
10 Replies

4. Shell Programming and Scripting

Complex renaming then moving files

I am a biologist and using an program on a computer cluster that generates a lot of data. The program creates a directory named ExperimentX (where X is a number) that contains files "out.pdb" and "log.txt". I would like to create a script that renames the out.pdb file to out_ExperimentX.pdb (or... (1 Reply)
Discussion started by: yaledocker
1 Replies

5. Shell Programming and Scripting

Moving and renaming large ammount of files

Hey, I'm kinda new to the shell scripting and I don't wanna mess things up yet :) Looking for a solution to the following: I need to move all the files like "filename.failed.dateandtime" to another directory also renaming them "filename.ready". I can't figure how to do this with multiple files... (4 Replies)
Discussion started by: sg3
4 Replies

6. Shell Programming and Scripting

Need script for renaming and moving files one by one...

Dears, I need your help! I got a problem and found some workaround solution but I donno how to realize it. I have a number of files (about 300 each day) and I need them to be renamed. All these files has fixed number of letters and name looks like this one:... (7 Replies)
Discussion started by: nypreH
7 Replies

7. UNIX for Dummies Questions & Answers

Moving files out of multiple directories and renaming them in numerical order

Hi, I have 500 directories each with multiple data files inside them. The names are sort of random. For example, one directory has files named e_1.dat, e_5.dat, e_8.dat, etc. I need to move the files to a single directory and rename them all in numerical order, from 1.dat to 1000(or some... (1 Reply)
Discussion started by: renthead720
1 Replies

8. UNIX for Dummies Questions & Answers

Renaming files after their directory name in multiple sub directories

So I am not sure if this should go in the shell forum or in the beginners. It is my first time posting on these forums. I have a directory, main_dir lets say, with multiple sub directories (one_dir through onehundred_dir for example) and in each sub directory there is a test.txt. How would one... (2 Replies)
Discussion started by: robotsbite
2 Replies

9. Shell Programming and Scripting

Moving multiple files and renaming them on the fly

Hi All, Being new to scripting I am facing a new situation. We have an application that generates a file lets say dumpfile for each user under the users home directory when they execute the application. This is quite a huge file and imagine having that for over 40 users on a daily basis. The... (1 Reply)
Discussion started by: daemongk
1 Replies

10. Shell Programming and Scripting

moving and renaming multiple files

Greetings, I know i can use the mv command to move and rename one file. How can I do this with multiple files? example pic01.bmp to pic0001.bmp how can i perform this function on an entire directory of sequential files and keep them in sequence? Hints, suggestions are most welcome:) ... (1 Reply)
Discussion started by: rocinante
1 Replies
Login or Register to Ask a Question