LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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, 06:26 AM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,485

Rep: Reputation: 51
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, 06:44 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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, 06:48 AM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,485

Original Poster
Rep: Reputation: 51
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, 06:48 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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, 06:51 AM   #5
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,485

Original Poster
Rep: Reputation: 51
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, 07: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, 06:00 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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 06:02 PM.
 
Old 10-25-2012, 08: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


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
[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 08:37 AM
find hidden files from wild card path using 'find' nagendrar Linux - Newbie 5 10-13-2011 11:48 PM
[GNU find version 4.2.27] find: paths must precede expression mechagojira Linux - Newbie 3 07-06-2011 06:34 AM
Find with -exec argument - not giving proper output..how to find... hinetvenkat Linux - Server 4 01-25-2010 07:19 AM
Find/grep/wc command to find matching files, print filename and word count dbasch Linux - Newbie 10 09-14-2009 06:55 PM


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