Change filenames recursively


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change filenames recursively
# 1  
Old 05-28-2014
Change filenames recursively

Hello,

I made a mistake in a script and now need to go back and change allot of filenames. I need to change "v4" in filenames to "v3". I was thinking of something like this.

Code:
#!/bin/bash

FILELIST=$(ls -f -R *)

for FILE in $FILELIST
do

   # create new filename
   NEW_FILE_NAME=$(echo $FILE | sed 's/v4/v3/g')

   # if a substitution was made
   if [ "$NEW_FILE_NAME" -ne "$FILE" ]; then

      # copy file to new name
      cp $FILE $NEW_FILE_NAME

      # remove original file
      rm $FILE
   fi

done

There are a rew issues with this approach. I'm not sure that ls -R -f is the best way to retrieve the list of files and since this will run recursively, I need to do some testing to make sure I don't do more harm the good. I'm also not sure about the ls -f option as far as filenames and paths go since I'm not changing directories.

Any suggestions?

LMHmedchem

Last edited by LMHmedchem; 05-28-2014 at 06:55 PM..
# 2  
Old 05-28-2014
You can do something like this:
Code:
#!/bin/bash

for file in *v4*
do
        echo mv "$file" "${file//v4/v3}"
done

Remove echo and rerun if output looks OK.
This User Gave Thanks to Yoda For This Post:
# 3  
Old 05-28-2014
Quote:
Originally Posted by Yoda
Code:
#!/bin/bash

for file in *v4*
do
        echo mv "$file" "${file//v4/v3}"
done

This does not appear to work recursively, at least the echo version only gave output for files in pwd. This will need to work down through a fairly deep file tree to be practical.

LMHmedchem
# 4  
Old 05-28-2014
This should work as long as you don't have and directory names with v4:

Code:
find . -iname "*v4*" -type f -print | while read file
do
    echo mv "$file" "${file//v4/v3}"
done

Otherwise this is less efficient but safer:

Code:
find . -iname "*v4*" -type f -print | while read file
do
   pth=$(dirname "$file")
   fl=$(basename "$file")
   echo mv "$file" "${pth}/${fl//v4/v3}"
done

This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 05-28-2014
Quote:
Originally Posted by Chubler_XL
Otherwise this is less efficient but safer:

Code:
find . -iname "*v4*" -type f -print | while read file
do
   pth=$(dirname "$file")
   fl=$(basename "$file")
   # remove echo to make this functional 
   echo mv "$file" "${pth}/${fl//v4/v3}"
done

That worked nicely. It took a few minutes to run, but that was still the quickest way to correct the gaff.

LMHmedchem
# 6  
Old 05-29-2014
You could speed it up a little (eliminating two utility invocations per file processed) by changing:
Code:
   pth=$(dirname "$file")
   fl=$(basename "$file")

to:
Code:
   pth="${file%/*}"
   fl="${x##*/}"

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change permission on a file recursively

Hi, this is the structure of the directory /local/home/app/cases under cases directory, below are the sub directories and each directory has files. /local/home/app/cases/1 /local/home/app/cases/2 /local/home/app/cases/3 /local/home/app/cases/4 File types are .txt .sh and so... (5 Replies)
Discussion started by: lookinginfo
5 Replies

2. Shell Programming and Scripting

How best to remove certain characters from filenames and folders recursively

hello, I'm trying to figure out which tool is best for recursively renaming and files or folders using the characters \/*?”<>| in their name. I've tried many examples that use Bash, Python and Perl, but I'm not much of a programmer I seem to have hit a roadblock. Does anyone have any... (15 Replies)
Discussion started by: prometheon123
15 Replies

3. Shell Programming and Scripting

change filenames but not extension

I have a filename with a bunch of periods that I want to replace with underscores, but I don't want to change the extension. Ex: I want file.test1.f-1.fig.eps to be file_test1_f-1_fig.eps Using awk, the following line will replace ALL periods with underscores, but I want to leave the... (2 Replies)
Discussion started by: erinbot
2 Replies

4. Shell Programming and Scripting

Change all filenames under different folders...

Hi, all: I'd love to use shell script to change all filenames under different folders once for all: I've got over 100 folders, in each of them, there is a file named "a.ppm". I wanna change all these "a.ppm" to "b.ppm", and still . Visually, the directory structure looks like: and hope... (1 Reply)
Discussion started by: jiapei100
1 Replies

5. Shell Programming and Scripting

Change part of filenames in a bulk way

Hallo! I have generated lots of data file which all having this format: sp*t1overt2*.txt Now I want to change them in this way: sp*t2overt1*.txt The rest of the file names stay unchanged. I know this is kind of routine action in sed or awk, but dont know how! I tried this command: ... (6 Replies)
Discussion started by: forgi
6 Replies

6. UNIX for Dummies Questions & Answers

Filenames change in a directory

Hi I have abc_ahb_one.v abc_ahb_two.v abc_ahb_three.v ........l like this -----upto abc_ahb_ninety.v in some directory. I need to change those file names to like below. ... (5 Replies)
Discussion started by: praneethk
5 Replies

7. UNIX for Advanced & Expert Users

script to recursively change permissions on file and dirs differently?

Hi there, I need to change all files/dirs 1. all files with 744 2. all dirs with 755 is there a script for that ? thanks, thegunman (13 Replies)
Discussion started by: TheGunMan
13 Replies

8. Shell Programming and Scripting

Change all filenames in a directory

I have a directory of files and each file has a random 5 digit string at the beginning that needs to be removed. Plus, there are some files that will be identically named after the 5 digit string is removed and I want those eliminated or moved. any ideas? (17 Replies)
Discussion started by: crumb
17 Replies

9. Cybersecurity

Recursively find and change Permissions on Man pages

Just joined after using the site as a guest.. (Very Good Stuff in here.. thanks folks.) I am in the process of hardening a Solaris 10 server using JASS. I also must use DISA Security Checklists (SRR) scripts to test for things that did not get hardened to DISA standards. One of the things... (5 Replies)
Discussion started by: altamaha
5 Replies

10. Shell Programming and Scripting

change filenames to Proper case

Hi, I have files with all its characters in lower cases. I need to change them to "proper case" (starting char to be come Upper case). How can I? Pls suggest. for e.g. xyz.txt should become Xyz.txt TIA Prvn (7 Replies)
Discussion started by: prvnrk
7 Replies
Login or Register to Ask a Question