LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 10-08-2012, 03:46 AM   #1
dunstable
LQ Newbie
 
Registered: Oct 2012
Posts: 4

Rep: Reputation: Disabled
bash script to copy rename files


Hi I am trying to right a bash script that will scan folders recursivly for file types .tbn and copy that file and rename it to cover.jpg within same directory.
So far the following script finds files but copys and renames file to where i run the cript from not where the original file is.
Not understanding the the basic syntax is a big handicap have done extensive searches but cant quite find what i am looking for.


for file in **/*.tbn
do
cp "${file}" cover.jpg
done
 
Old 10-08-2012, 03:53 AM   #2
asimba
Member
 
Registered: Mar 2005
Location: 127.0.0.0
Distribution: Red Hat / Fedora
Posts: 349

Rep: Reputation: 42
I think something following should do the trick


find ./ -name <filename.extension> | xargs mv <suit your needs>
 
Old 10-08-2012, 04:05 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,503

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Code:
cp "${file}" cover.jpg
Since you didn't specify a path for cover.jpg, it will be copied/renamed in the current working directory. If you want to copy the file in the same directory where the original is, you have to extract the path from the matched file name: Example:
Code:
#!/bin/bash
#
shopt -s globstar

for file in **/*.tbn
do
  cp "$file" "$(dirname "$file")"/cover.jpg
done
Instead, using the find command, as suggested by asimba, the -execdir predicate accomplishes the task for you, that is it executes the command from inside the directory where the file is, so that
Code:
find . -name \*.tbn -execdir cp {} cover.jpg \;
should do the trick.

Last edited by colucix; 10-08-2012 at 04:09 AM. Reason: Added info.
 
3 members found this post helpful.
Old 10-08-2012, 04:29 AM   #4
dunstable
LQ Newbie
 
Registered: Oct 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Many thanks

Quote:
Originally Posted by colucix View Post
Code:
cp "${file}" cover.jpg
Since you didn't specify a path for cover.jpg, it will be copied/renamed in the current working directory. If you want to copy the file in the same directory where the original is, you have to extract the path from the matched file name: Example:
Code:

Code:
#!/bin/bash
#
shopt -s globstar

for file in **/*.tbn
do
  cp "$file" "$(dirname "$file")"/cover.jpg
done
Did the Trick
Thank you all for your fast replies this bugging me for 3 hours

Last edited by colucix; 10-08-2012 at 05:16 AM. Reason: Added proper QUOTE tags
 
Old 10-08-2012, 10:53 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
No need for dirname, an external command. Just use built-in parameter substitution.

Code:
cp "$file" "${file%/*}"/cover.jpg
You might also consider using the -t option instead with cp/mv in this kind of scripting situation.

I think the find version used above is a better choice though.
 
2 members found this post helpful.
  


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
Help with Bash Script - Rename Multiple Files embsupafly Programming 16 04-02-2010 03:50 AM
Trouble with making a bash script to read in different files and rename output files. rystke Linux - Software 1 05-07-2009 08:00 AM
copy and rename the existing directory using BASH script neo2k Linux - Software 3 08-20-2008 10:43 AM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-22-2008 11:25 PM


All times are GMT -5. The time now is 03:40 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration