Rename files/directories based on their name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename files/directories based on their name
# 1  
Old 06-24-2003
Rename files/directories based on their name

i have hundreds of directories that have to be renamed. the directory structure is fairly uniform which makes the scripting a little simpler.

suppose i have many directories like this */*/*/*abc* (in other words i have similar directory names 3 dirs deep that all contain the pattern abc in their dir name). i would like to rename any directory that contains the pattern *abc* to, say, xyz (or anything really, but not just change the abc part. i want to change the entire dir name with xyz).

there are several pattern variations that i would like to rename, so i'm trying to develop a script for which additions would be easy.

i'm using Unix (Solaris) with tcsh, and my work so far (i'm real green to scripting so there may be some mistakes) is:
.............................

foreach old ( abc )
set new=`echo $old | sed 's/abc/xyz'`
mv $old $new
end

.............................

this works for dirs with the exact name abc (and that's cool), but when i try:

.............................

foreach old ( *abc* )
set new=`echo $old | sed 's/*abc*/xyz'`
mv $old $new
end

.............................

it fails, saying mv: blahblah is a directory (or something to that effect)

i think that covers it all. if anyone wants to help but needs more info just ask.

thanks in advance for your help

quant
# 2  
Old 06-24-2003
I don't think I'm understanding completely what you want to do. If you have hundreds of directories all with "abc" somewhere in the name and you rename them all to "xyz", they're all going to overwrite each other.. or the first "mv" command will rename the first directory to xyz, and all subsequent "mv" commands will move the remaining directories inside of xyz..

For instance, here's some code that does what you're asking (it was done in ksh, so you may need to modify it - i.e. substituting $status for $?):
Code:
for i in `ls`; do
 if [ -d $i ]; then
  echo $i | grep -q *abc*
  if [ $? -eq 0 ]; then
   mv $i xyz
  fi
 fi
done

But if you have three directories called
dirabc1
dirabc2
dirabc3
then dirabc1 will be renamed to xyz and dirabc2 and dirabc3 will be moved inside xyz..
# 3  
Old 06-24-2003
all the dirs are "forked" off from each other. there are no directories that contain more than one directory with the same pattern (i hope that makes sense). i understand your point, and i hadn't thought of that. thanks, i'll try your suggestion and post back

thanks again
# 4  
Old 06-24-2003
i tried the following:

.............
#!/bin/ksh
for i in `ls`; do
if [ -d $i ]; then
echo $i | grep -q "*/*/*DUAL*
if [ $? -eq 0 ]; then
mv $i fse
fi
fi
done
..............

there were 10 directories containing one dir each. each 2nd level dir only contained one dir matching *DUAL*. i wanted to rename each of these "fse"

i got the following error repeated 10 times (i suppose once for each of the 10 dirs)

grep: illegal option --q
Usage: grep -hblcnsviw pattern file

have i missed something?

quant
# 5  
Old 06-24-2003
This should do the trick:
Code:
for i in `find . -name *somedir*`; do
 mv $i `dirname $i`'/xyz'
done

This could be a one-liner if you used the -exec option of the find command, but I couldn't get it to work right...
# 6  
Old 06-24-2003
You're only renaming directories, so the -type d option for find is a boon. Also, if you really have "hundreds of directories," it's just possible that there are so many that glob to "*abc*' that the shell can't handle the command line. When this is true, I try to use something like this untested code:
Code:
find . -type d -name '*abc*' | while read pn; do
    dn=`dirname "$pn"`
    mv "$pn" "$dn/xyz"
done

(Thanks to oombera for the `dirname "$dn"` bit. Somehow I keep forgetting about that ...)

Part of your problem might be filenames that contain spaces --- common on "modern" Windows boxes and not unheard of on Unices --- so I put in an extra statement and some more quotes. If this is guaranteed not to be part of your problem, all the "double quotes" can come out.

You should also really carefully consider the other posters' cautions about duplicate file names, too.
# 7  
Old 06-24-2003
oombera:
i couldn't get that to work. i played with it for a while with different results. i'm not sure of the use of:
mv $i `dirname $i`'/xyz'

i did this:
................
for i in `find . -name */*/*DUAL*`; do
mv $i `*/*/*DUAL* $i`'/fse'
done
................
i got error:
find bad option 1005/15sep01.images/20010915_AXIAL_DUAL_11134
find path-list predicate-list

so, i'm sure it's something i've done wrong. maybe i should clarify my task.

say i have dir "02"...
02 has dirs "1004" and "1005"

1004 has dirs "a" and "b" in it
a has dirs "123abc" and "456def"
b has dirs "124abc" and "457def"

1005 has dirs "c" and "d" in it
c has dirs "125abc" and "458def"
d has dirs "126abc" and "459def"

from the 02 directory i would like to rename all subdirs with dirnames containing the pattern *abc* to "xyz"; and all subdirs with dirnames containing the pattern *def* to "uvw"

in the end i wish to have:
02/1004/a/:
xyz uvw

02/1004/b/:
xyz uvw

02/1005/c/:
xyz uvw

02/1005/d/:
xyz uvw

_________
obviously from my error above i am using *DUAL* instead of *abc* and using fse instead of xyz. i should have explained this more completely from the beginning. me bad.

criglerj: i just saw your post after i posted this. i will try your ideas and post back. thanks

quant
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename files from multiple directories along with directory indicator

Hi, Friends, i have a requirement where i need to rename my files residing in multiple sub directories and move them to one different directory along with some kind of directory indicator. For eg: test--is my parent directory and it has many files such as a1.txt a2.txt a3.txt ... (5 Replies)
Discussion started by: gnnsprapa
5 Replies

2. UNIX for Beginners Questions & Answers

Rename files based on simple text file

Hello! New here although not completely new to Unix. I wonder how I could rename files based on the data found in a simple textfile. It goes like this: I have 4 files 1 ldfgkkfjslkdfjsldkfjsf.wav 2 nndsdflksdjf.wav 3 sdflksjdf jjsdflsdfl.wav 4 dkadsdddd.wav Textfile.txt looks like... (14 Replies)
Discussion started by: Oortone
14 Replies

3. Shell Programming and Scripting

Rename files based on name in text file

Hello, I have a text file "file.list" with the contents below. file1 filename1 file2 filename2 file3 filename3 file1, file2 and file3 are files existing in the same directory as the text file file.list. I want to rename file1 to filename1, file2 to filename2, as show in the text... (1 Reply)
Discussion started by: james2009
1 Replies

4. UNIX for Dummies Questions & Answers

Rename files based on a list

Hi, I have a directory with a lot of files like this: a.bam b.bam c.bam I like to rename these files based on a list where the name of the files in the first column will be replasced by the names in the second column. Here is my list which is a tab-delimited text file: a x b y c ... (4 Replies)
Discussion started by: a_bahreini
4 Replies

5. Shell Programming and Scripting

Rename the files in all the directories and sub-directories

Hi all, I have more than 12000 files in 46 different directories and each directory has 2 sub-directories named “dat” or “gridded”. Dat sub-directories have files with extension “jpg.dat” and gridded sub-directories have files with extension “.jpg”. I need to... (1 Reply)
Discussion started by: AshwaniSharma09
1 Replies

6. UNIX for Dummies Questions & Answers

rename files based on their respective directory name

I have a number of files in directories labeled like this: /Data/tr_gray/tr_DTI/dti_FA.nii.gz (the brackets here represent a range of number that the files are labeled with) I need to rename each dti_FA.nii.gz file according to the name of the folder it resides in. For example, the file ... (3 Replies)
Discussion started by: tk0034
3 Replies

7. Shell Programming and Scripting

Rename files in sub directories with sequential numbers

I can rename a file with sequential numbers from 1 to N with this script: num=1 for file in *.dat;do mv "$file" "$(printf "%u" $num).txt" let num=num+1 done The script begins with renaming a some.dat file to 1.dat.txt and goes on sequentially renaming other DAT files to... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

8. Shell Programming and Scripting

Rename files and directories with special characters

Hello guys, I was looking for a shell script that removes all the special characters from the files and the subdirectories recursively. I could not locate it any more. Dose any body have a similar script that dose that? Thanks for the help. AV (0 Replies)
Discussion started by: avatar_007
0 Replies

9. Shell Programming and Scripting

rename files Ax based on strings found in files Bx

Hi, I'm not very experienced in shell scripting and that's probably why I came across the following problem: I do have several hundred pairs of text files (PF00x.spl and PF00x.shd) where the first file (PF00x.spl) needs to be renamed according a string that is included in the second file... (12 Replies)
Discussion started by: inCH
12 Replies

10. Shell Programming and Scripting

Rename files recursivly in specified directories

Hi, This is what I would like to do. 1. Find all directories named "ByHost" in a specified directory 2. Rename all .plist files inside "ByHost" directories This is the way I have been able to do it so far. #!/bin/sh # # Rename ByHost files # # Thomas Berglund, 13.07.08 # Get the... (2 Replies)
Discussion started by: Thomas Berglund
2 Replies
Login or Register to Ask a Question