LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-01-2014, 04:13 AM   #1
rustymonkey
LQ Newbie
 
Registered: Jul 2014
Posts: 4

Rep: Reputation: Disabled
Copy One File To Many With 4-digit Index On Filename Indexes Copies


Long ago...
Quote:
Originally Posted by Tinkster View Post
Code:
for FILE in {1..10}; do cp file file$FILE; done
;}

I like this and it works well.

Could some kind person please tell me how I could do the same while keeping the leading zeros on the file index numbers?

I tried this:
Code:
for FILE in {0001..0010}; do cp filename filename$FILE; done
The files were copied, but the leading zeros were not included, so the file names look like this:

filename1, filename2.. filename10

I want the file names to look like this:

filename0001, filename0002.. filename0010

Is there a way to include the leading zeros in the filenames?

Please keep in mind that I want all index numbers to have 4 digits, so filename000 will only work from 1~9. I need to make thousands of copies of the file.

Thanks heaps!
 
Old 07-01-2014, 04:33 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,832

Rep: Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820
Use printf to (re-)format the variable as %04d - that will maintain the leading zeroes.
And no, I'm not going to give you the complete solution.
 
Old 07-03-2014, 11:12 PM   #3
rustymonkey
LQ Newbie
 
Registered: Jul 2014
Posts: 4

Original Poster
Rep: Reputation: Disabled
Smile

Quote:
Originally Posted by syg00 View Post
Use printf to (re-)format the variable as %04d - that will maintain the leading zeroes.
And no, I'm not going to give you the complete solution.
Thank you so much for the hint. I'll post up with the answer when/if I find the answer to the puzzle.
 
Old 07-04-2014, 12:56 AM   #4
rustymonkey
LQ Newbie
 
Registered: Jul 2014
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thumbs up

I did not figure out how to do this in one step, but this worked with two commands.

First this:
Code:
for FILE in {1..100}; do cp filename filename$FILE; done
The files were copied, but again, the leading zeros were not included, so the file names look like this:

filename1, filename2.. filename100

So then I did this:
Code:
rename 's/\d+/sprintf("%04d", $&)/e' filename*
Now the file names have the desired numeric padding:

filename0001, filename0002.. filename0100

Now the only thing left is for me to understand out why and how this worked...

Thanks for helping me keep the family fed, Syg00!
 
Old 07-04-2014, 01:17 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,832

Rep: Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820
Have a look at this and see if it would be useful
Code:
for i in {90..100} ; do i=$(printf "%04d" $i) ; echo "FILE"$i ; done
 
Old 07-04-2014, 07:04 AM   #6
rustymonkey
LQ Newbie
 
Registered: Jul 2014
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by syg00 View Post
Have a look at this and see if it would be useful
Code:
for i in {90..100} ; do i=$(printf "%04d" $i) ; echo "FILE"$i ; done
Thanks syg00. This is much better, and I think I even understand what's going on now.

The following is the single line I used following your suggestion:

Code:
for i in {1..100}; do i=$(printf "%04d" $i); cp file file$i; done
This part was a huge help: i=&(printf "%04d" $i)

Would it be appropriate for me to study bash scripting to build a better understanding of what is happening here?

Thanks again.

Cheers

Last edited by rustymonkey; 07-04-2014 at 07:06 AM.
 
Old 07-04-2014, 07:15 AM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,832

Rep: Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820
Yes - go here and download the "Advanced Bash-Scripting Guide" in format of choice.
It says "Advanced" but doesn't require any pre-reqs.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
For Loop passing the index to filename g_paschoal Programming 8 11-01-2013 12:14 PM
Substitue single-digit, two-digit, and 3-digit numbers with text using sed dmason165 Programming 13 08-07-2009 11:38 AM
Change name of backup file in ext3 from filename~ to .filename~ Libu Linux - General 2 07-21-2008 10:29 PM
Bash script to change a filename associated with an inode index number. Ziv Programming 22 06-19-2004 09:41 AM
can not see file indexes from web myke_w Linux - Networking 1 11-19-2002 11:48 PM


All times are GMT -5. The time now is 04:31 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration