LinuxQuestions.org
Visit Jeremy's Blog.
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 12-01-2012, 12:56 PM   #1
Calgarian
Member
 
Registered: Feb 2005
Location: Calgary, AB Canada
Distribution: Kubuntu 6.10
Posts: 44

Rep: Reputation: 15
cp propblem in BASH


I am trying to write a script to move all occurences of a file type from a directory structure including all sub-directories, into a single directory. For example: from a structure music/different artists/different album names/different songs.mp3 collect all the mp3 files into a directory /songs. The names contain spaces, except for the destination directory, and I'm not concerned about duplicate names in the source.

I have all the basics of getting the base directory name, file type, and source directory name settled in the script. I'm trying the following find/cp command which I think should walk down from the base directory. I get the target directory created, but no files are copied into it and no error messages created. Here is my find/cp command. What silly mistake have I made? I'm suspicious that i"ve messed up the quotes or double quotes - or I really don't understand how the {} works in the cp command.


find "$BASE_DIR" -type f -name '*.$FILE_TYPE' -exec cp '{}' $NEW_DIR/'{}' \;
 
Old 12-01-2012, 01:22 PM   #2
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
This should work:

Code:
find /source -iname '*.mp3' -print0 | xargs -0 mv -t /destination
Replace source and destination directories.

If you didn't want to clobber files you would add '-n' to the mv arguments. Note that '-iname' means ignore case, if you don't want to ignore case use '-name' instead, but you usually want to ignore case. I mean the extension can be '.MP3' instead of '.mp3'.

Last edited by H_TeXMeX_H; 12-01-2012 at 01:25 PM.
 
Old 12-01-2012, 01:32 PM   #3
Calgarian
Member
 
Registered: Feb 2005
Location: Calgary, AB Canada
Distribution: Kubuntu 6.10
Posts: 44

Original Poster
Rep: Reputation: 15
I am actually trying to just copy them, not move them. I want to use the resulting directory for some other work without touching the originals. Also, I can't seem to find out what the -t option actually does?
 
Old 12-01-2012, 02:30 PM   #4
Calgarian
Member
 
Registered: Feb 2005
Location: Calgary, AB Canada
Distribution: Kubuntu 6.10
Posts: 44

Original Poster
Rep: Reputation: 15
A little more digging and reading and moving the target directory out of the base directory and I got it to work.

find "$BASE_DIR" -type f -name *.$FILE_TYPE -exec cp {} $NEW_DIR \;
 
Old 12-02-2012, 02:35 AM   #5
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Just replace 'mv' with 'cp'. Read the man pages and you will see that '-t' means destination directory.

Code:
find /source -iname '*.mp3' -print0 | xargs -0 cp -t /destination
Make sure to use single quotes around the '*' for the iname parameter or bash may expand that before find can use it.
 
Old 12-02-2012, 02:35 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

Your find command can be made more efficient this way:

Code:
find "$BASE_DIR" -type f -name "*.$FILE_TYPE" -exec cp -t "$NEW_DIR" '{}' \+
Changing ";" to "+" at the end of the -exec option makes it operate in batch mode, similar to how xargs works. But this requires the use of the -t option in cp and mv; since the '{}' substitution now represents all of the files found and can only appear at the end of the command.

Notice also how I quoted all the variables, and, as Tex-Mex mentioned, especially the -name argument. Otherwise the shell will attempt to expand globbing characters like the '*' and word-split all expanded strings before executing the command itself.

Proper quoting is a vitally important concept in shell scripting, so train yourself to do it correctly now.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes
 
1 members found this post helpful.
  


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
Bash problem : -bash: [: /bin/bash: unary operator expected J.A.X Linux - Software 1 09-22-2011 05:52 AM
Propblem with harddisk lhrt Linux - Hardware 4 09-13-2008 02:04 AM
propblem with gentoo network ( /etc/conf.d/net ) Tigra Linux - Networking 5 06-04-2005 09:57 PM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 06:42 PM
font propblem with linux Nik0s Linux - General 1 04-02-2004 12:28 PM

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

All times are GMT -5. The time now is 07: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
Open Source Consulting | Domain Registration