LinuxQuestions.org
Register a domain and help support LQ
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 04-19-2010, 11:18 AM   #1
mq15
Member
 
Registered: Apr 2009
Location: Pakistan
Distribution: Fedora 9
Posts: 192

Rep: Reputation: 30
Question doubt in cp command for copying recursively directories and files


Hi there.
There are many files in my source directory and its subdirectories. Keeping the directory tree same as that of my source directory, I want to copy only specific type of files (say only .txt files or say all those files with specific string pattern in their names) and leave the others. Note that I want copy not only the files but also the directory hierarchy as well. I don't know how to construct cp command for this. What I only know is that -r or -R is used for recursion wth cp command
Thanks
 
Old 04-19-2010, 11:27 AM   #2
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 96
You can use rsync.
 
Old 04-19-2010, 11:36 AM   #3
business_kid
LQ Guru
 
Registered: Jan 2006
Location: Ireland
Distribution: Slackware & Android
Posts: 7,572

Rep: Reputation: 697Reputation: 697Reputation: 697Reputation: 697Reputation: 697Reputation: 697
cp -a
cp can fall over copying empty directories.

You could simply use lndir, which makes symlinks, and is a bit more space efficient
/tries getting too clever
You could even get clever and try find [OPTIONS] |xargs install -D
 
Old 04-19-2010, 11:47 AM   #4
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian Jessie 8.4
Posts: 5,873

Rep: Reputation: 348Reputation: 348Reputation: 348Reputation: 348
Depending on what you want to achieve, you could use rsync to keep directories in sync. Or, you could use three commands piped together.

Something like: ls -r SOURCE | grep .txt | cp --parents *.txt

Note that I have not tried this command, so it may need to be tweaked a bit.
 
  


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
List all files in a directory recursively, without the directories? b10m3ch4 Linux - General 4 11-12-2009 06:07 PM
Using chmod to recursively change directories / files [GOD]Anck Linux - Software 6 11-10-2008 07:16 PM
copying directories recursively with find command nanda22 Linux - Newbie 1 09-01-2008 08:39 AM
deleting files recursively, but not directories tantan Linux - General 2 03-07-2007 07:01 AM
Copying hidden files recursively? mrd Linux - General 1 09-30-2005 08:51 AM


All times are GMT -5. The time now is 11:03 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration