Rename and add chars to multiple files at once


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename and add chars to multiple files at once
# 1  
Old 07-29-2014
Rename and add chars to multiple files at once

i have a little over 100 files named page1.jpg through page106.jpg. How can I rename them all to remove the page word and add 00 in front of the remaining numbers? I realize this might be multiple commands but cp page*.jpg *.jpg didn't work like I thought it would.

Last edited by rbatte1; 07-29-2014 at 09:41 AM.. Reason: Added ICODE tags
# 2  
Old 07-29-2014
Welcome to the forum.

First of all cp will not rename the file, it will just create a copy of the original file. If you want to rename a file you need to use mv.

If you are using bash, try:
Code:
for file in page*.jpg
do
    mv ${file} ${file/page/00}
done

# 3  
Old 07-29-2014
Did you consider searching these fora first? There's umpteen solutions to your problem!

Try
Code:
for FN in *.jpg; do echo mv "$FN" "00${FN#????}"; done
mv page106.jpg 00106.jpg
mv page14.jpg 0014.jpg
mv page1.jpg 001.jpg

# 4  
Old 07-29-2014
Welcome zimmerru,

You will have to work out the process for doing one, then call do that in a loop for all. I have a few to questions pose in response first:-
  • Is this homework/assignment? There are specific forums for these.
  • What have you tried so far, other than a your cp?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)
Most importantly, What have you tried so far?

The suggestion from chacko193 may or may not work depending on your OS & shell, hence the questions above.

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.

By way of explanation, cp page*.jpg *.jpg will be expanded to actually be:-
Code:
cp page1.jpg page2.jpg page3.jpg .... pagennn.jpg  abc.jpg page1.jpg page2.jpg page3.jpg .... pagennn.jpg

The process will copy all files that match either page*.jpg or *.jpg over the last file alphabetically that matches *.jpg so if you have page999.jpg, that will have been overwritten, unless your OS (unspecified) refuses to multiple copy unless the last item in the list is a directory.


We're all here to learn and getting the relevant information will help us all.
# 5  
Old 07-29-2014
Thank you all for your replies. I did do some initial searching but didn't find one that pertained to my situation... then again i wasn't searching this site specifically, but was using Google instead. Thank you for not flaming me too much!

I'll see what I can come up with for a single file and then use your loop example.. will post when I have it worked out.

---------- Post updated at 05:53 PM ---------- Previous update was at 09:13 AM ----------

Quote:
Originally Posted by rbatte1
Welcome zimmerru,

You will have to work out the process for doing one, then call do that in a loop for all. I have a few to questions pose in response first:-
  • Is this homework/assignment? There are specific forums for these.
  • What have you tried so far, other than a your cp?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)
Most importantly, What have you tried so far?

The suggestion from chacko193 may or may not work depending on your OS & shell, hence the questions above.

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.

By way of explanation, cp page*.jpg *.jpg will be expanded to actually be:-
Code:
cp page1.jpg page2.jpg page3.jpg .... pagennn.jpg  abc.jpg page1.jpg page2.jpg page3.jpg .... pagennn.jpg

The process will copy all files that match either page*.jpg or *.jpg over the last file alphabetically that matches *.jpg so if you have page999.jpg, that will have been overwritten, unless your OS (unspecified) refuses to multiple copy unless the last item in the list is a directory.


We're all here to learn and getting the relevant information will help us all.
  • Is this homework/assignment? There are specific forums for these. - No, just me trying to do something I have not had to do before.
  • What have you tried so far, other than a your cp? Tried both cp and mv as these seemed the most logical
  • What output/errors do you get? - target page9.jpg is not a directory
  • What OS and version are you using? - Mint Linux 17
  • What are your preferred tools? (C, shell, perl, awk, etc.) - bash mostly
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve) logically mv page*. jpg *.jpg seemed like it should strip page from the names but it does not. Since that didn't work I did not make it to the next step of adding the 0's
---------- Post updated at 05:57 PM ---------- Previous update was at 05:55 PM ----------

Quote:
Originally Posted by chacko193
Welcome to the forum.

First of all cp will not rename the file, it will just create a copy of the original file. If you want to rename a file you need to use mv.

If you are using bash, try:
Code:
for file in page*.jpg
do
    mv ${file} ${file/page/00}
done

This worked perfectly. Dropped it in a file, saved it, chmod +x and executed it and it worked great!
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

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

4. UNIX for Dummies Questions & Answers

Rename Multiple Files

Hey guys, I am the definition of a newbie. I am in the process of trying to rip all my dvds onto a new HTPC I setup. While doing this, I am also trying to organize a bunch of other files I already have to proper naming conventions. So far I have just been naming each file separately (I am on a... (4 Replies)
Discussion started by: Ralze34
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 the multiple files

Hi I need to reanme the multiple file using unix script I have multiple file like: sample_YYYYMMDD.xls test new_YYYYMMDD.xls simple_YYYYMMDD.xls I need to rename this file sample.xls testnew.xls SIMPLE.xls thanks (8 Replies)
Discussion started by: murari83.ds
8 Replies

7. UNIX for Dummies Questions & Answers

help with multiple files rename...

Hi everyone, I'm very green in Linux. Please help me to solve my problem. I have thousands of files and I want to change their names. They have naming convection: prefix_date_date+1_suffix.nc prefix: ext-GLORY date_date+1: 20020101_20020102 and two types of suffix: gridV_R20020130 and... (3 Replies)
Discussion started by: makikicindy
3 Replies

8. Shell Programming and Scripting

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 ... (3 Replies)
Discussion started by: a_dor8
3 Replies

9. Shell Programming and Scripting

removing non-printable chars from multiple files

How do I remove non-printable characters from all txt files and output the results to one file? I've tried the following: tr -cd '\n' < *.txt > out.txt and it gives ambiguous redirect error. How can I get it to operate on all txt files in the current directory and append the output to... (1 Reply)
Discussion started by: revax
1 Replies

10. 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
Login or Register to Ask a Question