Change all filenames in a directory

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change all filenames in a directory
# 8  
Old 09-17-2009
Hi Crumb,

As far as i know /[A-Za-z0-9]/ works well in most cases instead of /[[:alnum:]]/ ,sou can try /[A-Za-z0-9]+/ instead of repeating alnum too. But below it is explained better. i got this part from a web page that was recommended by one of our moderators here, so check it:

For example, before the POSIX standard, you had to write /[A-Za-z0-9]/ to match
alphanumeric characters. If your character set had other alphabetic characters in it, this
would not match them, and if your character set collated differently from ASCII, this might not even match the ASCII alphanumeric characters. With the POSIX character classes, you can write /[[:alnum:]]/ to match the alphabetic and numeric characters in your character set.
# 9  
Old 09-17-2009
Originally Posted by crumb
Cool. That did it. Here is my pseudo-final solution: -also not pretty but does the job.
#! /bin/bash

for OLD_FILE in $( ls | egrep '^[[:alnum:]][[:alnum:]][[:alnum:]][[:alnum:]][[:alnum:]]' )
  NEW_FILE=$( echo ${OLD_FILE} | sed 's/^[[:alnum:]][[:alnum:]][[:alnum:]][[:alnum:]][[:alnum:]]//' )
  if [ -f $NEW_FILE ]
    echo $NEW_FILE'-duplicate'


exit 0

I think you need [:digit:] instead of [:alnum:], no?

Here is an alternative way of doing it:
for old_file in [0-9][0-9][0-9][0-9][0-9]*; do
  if [ -f $new_file ]; then
    echo $new_file'-duplicate'
  echo $old_file $new_file

Last edited by Scrutinizer; 09-17-2009 at 08:50 PM..
# 10  
Old 09-17-2009
Try this one liner:
ls -1 [0-9][0-9][0-9][0-9][0-9]* | sed 's/\(^[0-9]\{5\}\)\(.*\)/mv & \2/' | ksh

# 11  
Old 09-18-2009
Originally Posted by crumb
The code I think is not working is:
( echo ${OLD_FILE} | sed 's/^[0-9]{5}//' )

Obviously, I missed something - correct would have been:

[house@leonov] echo 12345test.file | sed 's/^[0-9]\{5\}//'

# 12  
Old 09-18-2009
I just tried it on my actual files and realized there are spaces involved. This seems to throw a wrench into what has been already used.
Any more ideas?
# 13  
Old 09-18-2009
Originally Posted by crumb
there are spaces involved. Any more ideas
Depending on where those spaces are located (and assumed that eliminating them in advance is not an option), using double quotes may help you out:

[house@leonov] echo "12345te   st.file" | sed 's/^[0-9]\{5\}//'
te   st.file

# 14  
Old 09-18-2009
I think the egrep is also separating everything at the space too.

---------- Post updated at 12:38 AM ---------- Previous update was at 12:35 AM ----------

I would also be ok removing all the spaces
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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. #!/bin/bash FILELIST=$(ls -f -R *) for FILE in $FILELIST do # create new filename ... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

2. 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

3. 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

4. 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

5. 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

6. Shell Programming and Scripting

Use filenames to create directory.

I have many files similar to this one: AC41_AC85_86_AC128_129_MC171_173_SF_207_FMV.pdf. I want a directory named AC41 and to put the file AC41_AC85_86_AC128_129_MC171_173_SF_207_FMV.pdf into the directory. Next, a directory named AC85 and put the file into it. Also, continue to cycle through... (1 Reply)
Discussion started by: ndnkyd
1 Replies

7. Shell Programming and Scripting

concatenating the filenames in a directory

hi all, I have a requirement where in i have to read all the filenames based on a pattern from a directory and concatenate all these file names and write it to another file. i am using the following code to do this var1='' for filename in $_DIR/${FILE_NAME}* do if if then... (7 Replies)
Discussion started by: nvuradi
7 Replies

8. Shell Programming and Scripting

looping thru filenames in a directory

Hi, i am very new to UNIX, i am trying to loop thru the files in a directory. I got the filenames into a variable using $files=`ls` Here $files will contain <filename1> <filename2> <filename3> I want to get one filename at a time and append it to some some text. forexample, ... (1 Reply)
Discussion started by: silas.john
1 Replies

9. UNIX for Dummies Questions & Answers

Need to change filenames in a particular directory from lowercase to UPPERCASE

Hi, I need a shell script which changes a bunch of files in a particular directory from lowercase to UPPERCASE. I am not very familiar with shell scripts so a detailed explanation would be greatly appreciated!!!! Thanks ini advance! :) (7 Replies)
Discussion started by: Duke_Lukem
7 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