LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-24-2009, 04:48 PM   #1
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
HOWTO convert a group of files in a directory to a set of sequential numbers?


example:

file_what_ever_name_here.extension
file_what_ever_again_next.extension

to 00001.extension and 00002.extension?

thanks. oh and FYI my scripting sucks. i can hardly do a simple hello bash#!
 
Old 12-24-2009, 05:55 PM   #2
slacker_et
Member
 
Registered: Dec 2009
Distribution: Slackware
Posts: 138

Rep: Reputation: 27
I assume by "convert" you mean rename.

Here is a for loop to help you get started.
Code:
N=1
for i in *
do
   echo $i
   echo $i |sed "s/.*\./000$N./"
   let N=N+1
   read
done
Keep in mind that this will not pad with leading zero's. Nor does it actually rename the files.
And the "read" is just a pause to help you see what it's doing.
Also; this assumes:
a) That every file in the directory will actually have an extension; and only one extension.
b) That you want the files numbered in the same order that they appear in a directory listing.

--ET
 
Old 12-24-2009, 06:39 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
i=1
for file in file_what_ever_name_*
do 
  ext=${file##*.}
  echo mv "$file" $(printf "%05d.%s\n" $i $ext)
  ((i++))
done

Last edited by ghostdog74; 12-24-2009 at 07:02 PM.
 
Old 12-24-2009, 06:54 PM   #4
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Quote:
Originally Posted by slacker_et View Post
I assume by "convert" you mean rename.

Here is a for loop to help you get started.
Code:
N=1
for i in *
do
   echo $i
   echo $i |sed "s/.*\./000$N./"
   let N=N+1
   read
done
Keep in mind that this will not pad with leading zero's. Nor does it actually rename the files.
And the "read" is just a pause to help you see what it's doing.
Also; this assumes:
a) That every file in the directory will actually have an extension; and only one extension.
b) That you want the files numbered in the same order that they appear in a directory listing.

--ET
Well this one displayed what the file name would look like, but failed to change the file names. first time i just hit enter, second time i hit y. That gave me the command not found bash error.
 
Old 12-24-2009, 06:55 PM   #5
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Quote:
Originally Posted by ghostdog74 View Post
Code:
i=1
for file in file_what_ever_name_*
do 
  ext=${file##*.}
  echo mv $file $(printf "%05d.%s\n" $i $ext)
done
i must of goofed when i did this one. i got an error in line 5 stating no such number. the only thing i didnt do when i copied over was the "file_what_ever_name_* i just put * as i was not sure what to put here for the directory or the file name.
 
Old 12-24-2009, 06:55 PM   #6
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Thank you both for replies btw.
 
Old 12-24-2009, 07:01 PM   #7
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
i removed the 'read' tag and this is the results:

Code:
sh sequence.sh 
Building Detail in Heidelberg.jpg
0001.jpg
Building is part of Heidelberg Castle.jpg
0002.jpg
Buildings across river from Heidelberg Castle.jpg
0003.jpg
City of Heidelberg with castle ruins on left.jpg
0004.jpg
Entering Heidelberg Castle.jpg
0005.jpg
Exterior Wall in Heidelberg Castle Courtyard.jpg
0006.jpg
German Building Style in Heidelberg.jpg
0007.jpg
Heidelberg Castle Walls and Gate.jpg
0008.jpg
Heidelberg Castle.jpg
0009.jpg
Heidelberg Garden Wall.jpg
00010.jpg
Heidelberg from Castle.jpg
00011.jpg
Inside Heidelberg Castle Grounds.jpg
00012.jpg
Interior Walls of Heidelberg Castle.jpg
00013.jpg
Looking up at Heidelberg Castle.jpg
00014.jpg
Model of Heidelberg Castle.jpg
00015.jpg
Model of Heidelberg Ruins.jpg
00016.jpg
Neptune Fountain in Heidelberg Castle Garden.jpg
00017.jpg
Path from River into Heidelberg.jpg
00018.jpg
Stone wall in Heidelberg.jpg
00019.jpg
Three Building Styles used in Heidelberg Castle.jpg
00020.jpg
Tower ruins Heidelberg Castle.jpg
00021.jpg
View of Heidelberg Castle Gardens.jpg
00022.jpg
View of Heidelberg Castle.jpg
00023.jpg
View of Heidelberg from Castle.jpg
00024.jpg
Walls around Heidelberg Castle Gardens I think.jpg
00025.jpg
sequence.sh
00026.sh
 
Old 12-24-2009, 07:02 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by lleb View Post
i must of goofed when i did this one. i got an error in line 5 stating no such number. the only thing i didnt do when i copied over was the "file_what_ever_name_* i just put * as i was not sure what to put here for the directory or the file name.
works fine for me.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
set default umask to a directory or a group luvlinux2009 Linux - Security 4 09-17-2009 08:54 PM
Always set group write bit in given directory? jnojr Linux - Newbie 3 05-07-2009 07:29 PM
Set group on files as they are created gatsby Linux - Newbie 1 02-25-2008 04:53 PM
Renaming group of files within one directory DIRdiver Linux - General 3 10-25-2006 09:57 AM
howto mv a set of subdirectories to an other directory? Lleb_KCir Linux - General 1 06-13-2004 11:33 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration