LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-14-2013, 06:59 PM   #1
jbruyet
Member
 
Registered: Feb 2011
Location: North Central Washington
Distribution: Debian, OpenSUSE, Kali, Ubuntu
Posts: 178

Rep: Reputation: 7
How can I bulk copy files to a new extension in the same directory?


Ok, I'm at a loss on this one. What I frequently need to do is take a group of files in a directory, copy them so they have a different extension and have the new files remain in the original directory. For example, if I have file1.cfg, file2.cfg and file3.cfg in /dir I want to be able to copy them so I have file1.txt, file2.txt and file3.txt in /dir in addition to file1.cfg, etc... When I'm done I want to have all six files in /dir. This is very simple in Windows:

Code:
copy *.cfg *.txt
What would be the equivalent command in Linux?

Thanks,

Joe B
 
Old 01-14-2013, 08:15 PM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
Code:
for i in *.cfg;
do
  cp $i ${i/%.cfg/.txt}
done
See Bash parameter substitution: http://www.tldp.org/LDP/abs/html/par...stitution.html
In this specific case example 10.13 is very helpful.
 
Old 01-15-2013, 03:54 AM   #3
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Don't forget about quoting though (also this can be done without bash extensions):

Code:
for i in *.cfg; do
  cp "$i" "${i%.cfg}.txt"
done
 
Old 01-15-2013, 06:19 AM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
Quote:
Originally Posted by mina86 View Post
Don't forget about quoting though (also this can be done without bash extensions):

Code:
for i in *.cfg; do
  cp "$i" "${i%.cfg}.txt"
done
Of course you can use basename instead of Bash extensions, like we did, but Bash is pretty much a common denominator nowadays, I would think, so why not take advantage of it (and I tested this also on zsh, works there, too).
 
Old 01-15-2013, 07:24 AM   #5
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by TobiSGD View Post
Of course you can use basename instead of Bash extensions, like we did, but Bash is pretty much a common denominator nowadays, I would think, so why not take advantage of it (and I tested this also on zsh, works there, too).
Sure, that's why this was just a minor point of my post – my main issue was with quoting.

Nonetheless I prefer sticking to POSIX shell (which may be especially useful when writing scripts as programs like dash are reported to be faster than full bash) and “${i%.cfg}.txt” is in no way less convenient than “${i/%.cfg/.txt}”.

By the way, I think it's also worth noting “rename” here. Potential solution to OP problem could also be:
Code:
cp -- source-directory/*.cfg target-directory/
cd target-directory/
rename .cfg .txt *.cfg
# or on systems with perl-based rename:
# rename 's/\.cfg$/.txt/' *.cfg
even though it has it's own limitations.
 
1 members found this post helpful.
Old 01-15-2013, 07:36 AM   #6
jefro
Moderator
 
Registered: Mar 2008
Posts: 21,976

Rep: Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623
I have never tried this but it might work. See if you have mmv command.
 
Old 09-15-2014, 01:54 PM   #7
jbruyet
Member
 
Registered: Feb 2011
Location: North Central Washington
Distribution: Debian, OpenSUSE, Kali, Ubuntu
Posts: 178

Original Poster
Rep: Reputation: 7
Thanks everyone, and sorry about the late "Solved" notification.

Thanks,

Joe B
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy a directory into another directory while ignoring specific directories or files wh33t Linux - Newbie 14 05-16-2012 08:13 PM
Bash script to copy specified extension file from a directory? sokha Programming 11 03-15-2010 02:51 AM
[SOLVED] Need to add an extension to all files in a directory kmkocot Linux - Newbie 3 09-27-2009 10:19 AM
command tofind files by extension through a directory tree, and copy them all to the budword Linux - Newbie 2 06-27-2009 12:48 PM
Copy all files with .htm extension dickb Linux - Newbie 3 06-28-2005 12:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:51 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