Rename the multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename the multiple files
# 1  
Old 07-19-2011
Rename the multiple files

Hi

I need to reanme the multiple file using unix script

I have multiple file

like:
Code:
sample_YYYYMMDD.xls
test new_YYYYMMDD.xls
simple_YYYYMMDD.xls

I need to rename this file
Code:
sample.xls
testnew.xls
SIMPLE.xls


thanks

Last edited by Franklin52; 07-21-2011 at 03:51 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 07-19-2011
does this help?
Code:
 echo "sample_YYYYMMDD.xls
test new_YYYYMMDD.xls
simple_YYYYMMDD.xls"|awk '{a="mv "$0" "; gsub(/_.*xls/,".xls",$0);a=a$0; print a}'|sh

you can change the echo part with your own find/ls command
# 3  
Old 07-19-2011
Please use code tags when pasting code or sample input/output.

On most linux distros, there is a utility, called 'rename' that does the job.
However, I am aware of two different implementations, so check 'man rename' and make a test on copies of the files first, before you actually rename them all.
On redhat/centos systems the syntax would be
Code:
rename "_[0-9]*" "" *.xls

on debian/Ubuntu, the syntax is perl-like
Code:
rename 's/_[0-9]*//' *.xls

The second variant is sometimes called 'prename' (perl rename) on some systems. Again, check the man pages.
# 4  
Old 07-19-2011

rename "_[0-9]*" "" *.xls

can you please explace above post where i need to use this
# 5  
Old 07-19-2011
just type it in the terminal in the directory with the files you want to rename.

Type
Code:
rename --help

If you see something like
Code:
Usage: rename [-v] [-n] [-f] perlexpr [filenames]

, then you have the second mentioned version of rename.
Type
Code:
rename -n  's/_[0-9]*//' *.xls

Which will not rename anything, just do a dry-run and tell you what it would do. If you are happy, just run it wihtout the -n switch.
# 6  
Old 07-20-2011
modification source fole format

I have receive the file below format

2010_simple_test_20100912.xls
2010_information_new_20100912.xls

I need to rename the above two file in below format

simple.xls
information.xls

can you please guide me on this
# 7  
Old 07-20-2011
rename(1) is your best bet. I still don't know what version of rename you have, though.

If the above worked for you, why don't you just use it twice?

Code:
rename 's/^[0-9]*_//' *.xls   #will strip the leading numbers and underscore
rename 's/_[0-9]*//' *.xls    #strip the underscore followed by number

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename multiple files in one go

OS : Oracle Linux 6.8 shell : bash As shown below, I have multiple files like below (query1-extract_aa, query1-extract_ab, query1-extract_ac, ....) $ ls -l total 235680 -rw-rw-r-- 1 reportusr reportusr 30M May 3 11:25 query1-extract_aa -rw-rw-r-- 1 reportusr reportusr 30M May 3 11:25... (5 Replies)
Discussion started by: kraljic
5 Replies

2. Shell Programming and Scripting

SBATCH trinity for multiple files and rename/move the output files

Hey guys, I have wrote the following script to apply a module named "trinity" on my files. (it takes two input files and spit a trinity.fasta as output) #!/bin/bash -l #SBATCH -p node #SBATCH -A <projectID> #SBATCH -n 16 #SBATCH -t 7-00:00:00 #SBATCH --mem=128GB #SBATCH --mail-type=ALL... (1 Reply)
Discussion started by: @man
1 Replies

3. Shell Programming and Scripting

Rename a multiple files

I have multiple files in folder which i want to rename. hence I am using the below command in my script by I get an error: export XXX_LOG_DIR="${LOG_DIR}/${XXX_HOST}/xxx/${REPORT_DATE}" mv $XXX_LOG_DIR/*.audit.gz $XXX_LOG_DIR/*.audit.log.gz But I get the below error: mv: target... (5 Replies)
Discussion started by: karan8810
5 Replies

4. Shell Programming and Scripting

How to rename multiple files at one go?

Hi, I have hundreds of files with XXX in their file name and I want to rename all of them with YYY in place of XXX. for ex: $ ls -1 123XXX789 345XXX678 Output $ ls -1 123YYY789 345YYY678 I know we can loop in each file and sed to replace and rename each file but ren *XXX* *YYY*... (4 Replies)
Discussion started by: reddyr
4 Replies

5. Shell Programming and Scripting

Rename multiple files

Hi, In my directory I have many files, for e.g. file_123 file_124 file_125 file_126 file_127 Instead of renaming these files one by one, I would like to rename them at a same time using same command... they should appear like 123 124 125 126 127 What command(awk or ls or... (3 Replies)
Discussion started by: juzz4fun
3 Replies

6. Shell Programming and Scripting

Rename multiple files

hello: I have multiple files with names like: somestring_y2010m01d01 somestring_y2010m01d02 .......... somestring_y2010m12d31 How... (4 Replies)
Discussion started by: sylcam
4 Replies

7. UNIX for Dummies Questions & Answers

How to rename multiple files

Hi all, I have some files like: pickup.0000043200.t001.t001.data pickup.0000043200.t001.t002.data pickup.0000043200.t002.t001.data pickup.0000043200.t002.t002.data pickup.0000043200.t003.t001.data pickup.0000043200.t003.t002.data I need to rename these files to ... (4 Replies)
Discussion started by: a_dor8
4 Replies

8. Shell Programming and Scripting

now to rename multiple files

I have several hundred files in one directory which I need to move to another directory with the new extension, for example: /bb/data/rptmgr* are in the source directory need to be moved to /bb/data55/rptmgr*.new Is there an efficient way to do it? Thanks -A (4 Replies)
Discussion started by: aoussenko
4 Replies

9. Shell Programming and Scripting

rename multiple files

Hi, can anyone have a ksh script to rename multiple files (ie to remove .Z extension of the files) can someone correct this? for i in *.Z do var1 = substr($i, 1,at(".Z",$i)-1) mv $i $var1 done Thanks.. Antony (13 Replies)
Discussion started by: antointoronto
13 Replies

10. UNIX for Dummies Questions & Answers

Rename multiple files

Hello, I want to rename multiple files at a time and I don't know how to do it. I have various ".mp3" files, like "band name - music name.mp3" and I want to remove the "band name" from all files. Anybody knows how to do it using shell script or sed or even perl? Thanks (7 Replies)
Discussion started by: luiz_fer10
7 Replies
Login or Register to Ask a Question