Visit Our UNIX and Linux User Community

mv command to rename multiple files that retain some portion of the original file nam

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting mv command to rename multiple files that retain some portion of the original file nam
# 1  
Old 12-20-2008
mv command to rename multiple files that retain some portion of the original file nam

Well the title is not too good, so I will explain.

I need to move (rename) files using a simple AIX script.


to be:


The ??? are realy numbers 001 through 999, and there are two filenames per number. The 'date' is easy `date +%Y%m%d`. but due to my limited scripting ability, I cannot seem to get this accomplished.

Any help would be greatly appreciated.

Thanks in advance.
# 2  
Old 12-20-2008
Something like this? Try it first without the coloured portion to see if you get the right commands:

ls ???file??.txt | sed "s/\(...\)\(.*\)\.\(.*\)/mv & \1renamed\2""`date +%Y%m%d`"".\3/" | sh

# 3  
Old 12-20-2008
It works greatly on UBUNTU
ls ???file*.txt | sed "s/\(...\)\(.*\)\.\(.*\)/mv & \1renamed\2`date +%Y%m%d`.\3/" | sh

Last edited by lifegeek; 12-20-2008 at 05:09 PM..
# 4  
Old 12-20-2008
For whatever reason (did a copy and paste of the line into a script) this did not work. I only got one file with a `date +%Y%m%d`.txt

for f in *pepfilled.txt ; do
mv $f /usr/$fclaims`date +%y%m%d`.txt

Am I missing a quote(s) somewhere? The red claims above is the new file name with a format of ???claims20081220.txt, renamed from ???pepfilled.txt.

This will get half my files ???pepfilled.txt, I will just write the same script for the other half ???pepfilledcr.txt.
# 5  
Old 12-21-2008
You provided weak examples, in your first post, of the input and the desired output. Try this:

ls ???pepfilled*.txt | sed "s/\(...\).*\.\(.*\)/\1claims`date +%Y%m%d`.\2/" | sh


Previous Thread | Next Thread
Test Your Knowledge in Computers #376
Difficulty: Medium
HP-UX was UNIX V7 compliant.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to rename portion of file using match to another

In the portion of bash below I am using rename to match the $id variable to $file and when a match (there will alwsys be one) is found then the $id is removed from each bam and bam.bai in $file and _test is added to thee file name before the extension. Each of the variables is set correctly but... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Renaming portion of multiple files

Hi, I have some files as below. Temp6001_5025.cpp Temp6002_5025.cpp Temp6003_5025.cpp Temp6004_5025.cpp Temp6005_5025.cpp Temp6006_5025.cpp Temp6007_5025.cpp . . Temp6100_5025.cpp i want to replace 5025 to 5026 in all files and the result should be Temp6001_5026.cpp . .... (3 Replies)
Discussion started by: lathigara
3 Replies

3. Shell Programming and Scripting

command to copy original files from links in HP-UX

I have folder ABC and files in ABC are links. I want to create the same ABC folder in different path and copy the actual files from source ABC dir. Can anyone provide command for this? Thanks in advance. (2 Replies)
Discussion started by: venkatababu
2 Replies

4. Shell Programming and Scripting

AWK print and retain original format

I have a file with very specific column spacing formatting, I wish to do the following: awk '{print $1, $2, $3, $4, $5, $6, $19-$7, $20-$8, $21-$9, $10, $11, $12}' merge.pdb > vector.pdb but the format gets ruined. I have tried with print -f but to no avail.... (7 Replies)
Discussion started by: chrisjorg
7 Replies

5. Shell Programming and Scripting

Rename portion of file based on another file

Hello, I've been searching and reading, but I can't figure out how to solve this problem with my newbie skills. In my directory, I have a list of files (see dirlist.txt attachment) that I need to merge and rename. I have part of the code of the code figured out (see below). However, I... (3 Replies)
Discussion started by: anjulka
3 Replies

6. UNIX for Dummies Questions & Answers

For Loop To Rename Multiple Files Finds One Non-existant File

Okay so here's something that's confusing me: I have a script that's designed to remove the words "new_" from the front of any file except two exceptions and it looks something like this... for i in new_* do if ] && ]; then j=`echo "$i"|cut -c5-` mv $i $j fi done ... (5 Replies)
Discussion started by: Korn0474
5 Replies

7. Shell Programming and Scripting

Multiple file rename (change in filename in unix with single command

Dear All, Please help ! i ham having 300 file with E.G. PMC1_4567.arc in seq. like PMC1_4568.arc,PMC1_4569.arc ...n and so on.. i want all those file to be rename like PMC_4567.arc ,PMC_4568.arc .. mean i want to remove 1 from first file name .. pls help.. (6 Replies)
Discussion started by: moon_22
6 Replies

8. Shell Programming and Scripting

how to rename multiple files with a single command

Hi I have following list of files at a path: 01.AR.asset 01.AR.index 01.AR.asset.vf 01.AR.asset.xv I want to rename all these files as follows: 73.AR.asset.Z 73.AR.index.Z 73.AR.asset.vf.Z 73.AR.asset.xv.Z Can any body give me a single command to acheive the above results. ... (5 Replies)
Discussion started by: tayyabq8
5 Replies

9. UNIX for Advanced & Expert Users

command to copy files with original ownership

Hi, I need a command that to copy files from others and to keep files' ownership. Example: I copy file.txt from users "abc" to my local, and file.txt is own by user "abc" in local. Thanks in advance! (3 Replies)
Discussion started by: need_help
3 Replies

10. Shell Programming and Scripting

Rename a file if I don't know its exact original name?

Hi everyone, I will be downloading a file every day with the name in this format SCA20060303_17514_IN.TXT And I need to rename it to SCA20060303_IN.TXT Where "20060303" is the current date, and the "_17514" part will always be a 5-digit number that I will NOT know beforehand. I... (9 Replies)
Discussion started by: propel
9 Replies

Featured Tech Videos