LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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-25-2012, 05:26 AM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Rep: Reputation: 76
Using find.


Hi:
I use the find command and get the following output:
Code:
semoi@darkstar:/mnt/cd0/Bach/Partitas_violin$ find . -name "*flac"
./PARTITA_1/Bach_Partita_I_Kuijken_24-bit.flac
./PARTITA_1/Bach_Sonata_I_Kuijken_24-bit.flac
./PARTITA_2/Bach_Kuijken_Partita_IIa_24-bit-cr.flac
./PARTITA_2/Bach_Kuijken_Partita_IIb_24-bit-cr.flac
./PARTITA_2/Bach_Kuijken_Sonata_II_24-bit-cr.flac
./PARTITA_3/Bach_Kuijken_Partita_III_24-bit.flac
./PARTITA_3/Bach_Kuijken_Sonata_III_24-bit.flac
semoi@darkstar:/mnt/cd0/Bach/Partitas_violin$
Now I'd like to copy all the listed files into a single directory. Could somebody give me a command or series of commands to do it?
 
Old 10-25-2012, 05:44 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Code:
find . -name "*flac" -exec cp "{}" dirname \;
replace "dirname" with the path to the desired destination directory
 
1 members found this post helpful.
Old 10-25-2012, 05:48 AM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Quote:
Originally Posted by pixellany View Post
Code:
find . -name "*flac" -exec cp "{}" dirname \;
replace "dirname" with the path to the desired destination directory
Thanks pixellany. I'll add it to my 'find' examples list.
 
Old 10-25-2012, 05:48 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
find . -name \*.flac -exec cp {} /path/to/destination \;
In general this has a downside effect: since find searches recursively into subdirectories, it may find files with the same name. In this case the files are overwritten into the destination directory. To avoid that you can use the --backup option of cp in order to keep the different files with a suffix added to their name. Example:
Code:
find . -name \*.flac -exec cp --backup=numbered {} /path/to/destination \;
See man cp for details.
 
1 members found this post helpful.
Old 10-25-2012, 05:51 AM   #5
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Very considerate of you. I had been trying some syntaxis but I do not think I would have guess the answer(s).
 
Old 10-25-2012, 06:03 AM   #6
rambolinuxbuddy
LQ Newbie
 
Registered: Sep 2012
Posts: 16

Rep: Reputation: Disabled
you can use this command:-

Code:
ubuntu$ cp $(find . -name "*flac") direcory/you/want/to/copy/
hope you got your answer.
 
Old 10-25-2012, 05:00 PM   #7
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
Quote:
Originally Posted by rambolinuxbuddy View Post
you can use this command:-

Code:
ubuntu$ cp $(find . -name "*flac") direcory/you/want/to/copy/
No, do not use that. Never use a substitution pattern to insert lines of text from a file or command into another command. Shell word-splitting will break up any entries with whitespace in their names, and globbing characters might possibly expand, and you'll end up with errors and possibly worse.

It's basically the same problem as this. Unless you can tell your command to operate on filenames directly, you really need to use a while+read loop or similar.

How can I read a file (data stream, variable) line-by-line (and/or field-by-field)?
http://mywiki.wooledge.org/BashFAQ/001

How can I find and deal with file names containing newlines, spaces or both?
http://mywiki.wooledge.org/BashFAQ/020

Last edited by David the H.; 10-25-2012 at 05:02 PM.
 
Old 10-25-2012, 07:03 PM   #8
linux_BSD
Member
 
Registered: Sep 2012
Posts: 47

Rep: Reputation: 4
Code:
find . -name "*flac" -exec cp -t target_dir '{}' \;
The -t is a switch for target directory.
 
  


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
[SOLVED] Find URL in Debian package index via awk/sed (=find a line, then search from there) maddes.b Linux - Software 11 06-28-2013 07:37 AM
find hidden files from wild card path using 'find' nagendrar Linux - Newbie 5 10-13-2011 10:48 PM
[GNU find version 4.2.27] find: paths must precede expression mechagojira Linux - Newbie 3 07-06-2011 05:34 AM
Find with -exec argument - not giving proper output..how to find... hinetvenkat Linux - Server 4 01-25-2010 06:19 AM
Find/grep/wc command to find matching files, print filename and word count dbasch Linux - Newbie 10 09-14-2009 05:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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