The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
find the length of file names in a directory? koti_rama Shell Programming and Scripting 5 06-04-2008 10:19 AM
Replace characters in all file names in a particular directory madhunk Shell Programming and Scripting 4 02-16-2006 07:10 PM
directory names in a flat file surjyap Shell Programming and Scripting 2 10-06-2005 07:51 AM
How to change automatically the file names MAKY Shell Programming and Scripting 6 08-10-2005 10:47 AM
File and Directory Names become hidden dbinsol1 UNIX for Advanced & Expert Users 10 05-29-2002 09:58 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-20-2008
andou andou is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 20
Change All File Names in a Directory

Hi,

If I have a directory full of say 100 random files, and I would like to organize them, for example: FILE001, FILE002, FILE003, FILE004, etc.

How would I do this from Terminal, instead of manually changing each file? I'm using Mac OS X, if that makes a difference.

Thank you in advance for any help
  #2 (permalink)  
Old 01-21-2008
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
Try...
Code:
c=0
for i in *
do 
   test -f $i &&\
   mv $i $(printf "FILE%03i" $c) &&\
   ((c+=1))
done
  #3 (permalink)  
Old 01-21-2008
thana thana is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 55
Try this

[code ]
#!/bin/bash
dir="path name"
i=0
for filename in `ls -1t $dir/`
do
i=`expr $i + 1`
mv $dir/$filename $dir/ex$i
done

[code ]
  #4 (permalink)  
Old 01-21-2008
kukretiabhi13 kukretiabhi13 is offline
Registered User
  
 

Join Date: Jan 2008
Location: India
Posts: 13
multiple file renaming

say you have files like below...
jordba.package1
jordba.package2
jordba.package3

use the below:
for f in jordba.*; do mv "$f" "${f#jordba.}"; done

the above for loop will make your list like...

package1
package2
package3

========================


but there is another issue similar to the before that i have the files

x1_p.sql
x2_p.sql
x3_p.sql

and so on

i need to add h before .sql to be as the following:

x1_ph.sql
x2_ph.sql
x3_ph.sql


====================================

This should work for the data given by you above...

for f in *_p.sql ; do mv "$f" "${f%_p.sql}_ph.sql"; done


I hope these r helpful
  #5 (permalink)  
Old 01-22-2008
andou andou is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 20
Quote:
Originally Posted by Ygor View Post
Try...
Code:
c=0
for i in *
do 
   test -f $i &&\
   mv $i $(printf "FILE%03i" $c) &&\
   ((c+=1))
done
I tried this with a test directory of 17 Randomly named files. The result was sh: test: too many arguments 13 times and sh: test: Photo: binary operator expected 2 times. However, 2 of the files came out as expected: FILE002 and FILE003.

Sidenote: This is with .jpg files. I changed "FILE%03i" to "FILE%03i.jpg", so the two files came out: FILE002.jpg and FILE003.jpg. None of the other file names changed.
  #6 (permalink)  
Old 01-22-2008
andou andou is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 20
Quote:
Originally Posted by thana View Post
Try this

[code ]
#!/bin/bash
dir="path name"
i=0
for filename in `ls -1t $dir/`
do
i=`expr $i + 1`
mv $dir/$filename $dir/ex$i
done

[code ]
I'm not sure what to do with filename. I tried *.jpg and "*.jpg" and end up with: `*.jpg': not a valid identifier and `"*.jpg"': not a valid identifier, respectively.
  #7 (permalink)  
Old 01-22-2008
andou andou is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 20
Quote:
Originally Posted by kukretiabhi13 View Post
say you have files like below...
jordba.package1
jordba.package2
jordba.package3

use the below:
for f in jordba.*; do mv "$f" "${f#jordba.}"; done

the above for loop will make your list like...

package1
package2
package3

========================


but there is another issue similar to the before that i have the files

x1_p.sql
x2_p.sql
x3_p.sql

and so on

i need to add h before .sql to be as the following:

x1_ph.sql
x2_ph.sql
x3_ph.sql


====================================

This should work for the data given by you above...

for f in *_p.sql ; do mv "$f" "${f%_p.sql}_ph.sql"; done


I hope these r helpful
This works for me for renaming files that are already ordered 1, 2, 3, etc.

However, I want to take randomly named files, such as qwerty.jpg, blah.jpg 123_lol.jpg, and rename them to be ordered sequentially, such as: 2007NOV03001.jpg, 2007NOV03002.jpg, 2007NOV03003.jpg, etc.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:51 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0