Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 04-19-2010, 10:18 AM   #1
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
Old 04-19-2010, 10:27 AM   #2
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

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

Rep: Reputation: 825Reputation: 825Reputation: 825Reputation: 825Reputation: 825Reputation: 825Reputation: 825
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, 10:47 AM   #4
LQ Addict
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,891

Rep: Reputation: 351Reputation: 351Reputation: 351Reputation: 351
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.


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

All times are GMT -5. The time now is 10:38 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration