LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 04-19-2010, 10:18 AM   #1
mq15
Member
 
Registered: Apr 2009
Location: Pakistan
Distribution: Linux Mint Cinnamon
Posts: 224

Rep: Reputation: 31
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, 10:27 AM   #2
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 97
You can use rsync.
 
Old 04-19-2010, 10:36 AM   #3
business_kid
LQ Guru
 
Registered: Jan 2006
Location: Ireland
Distribution: Slackware, Slarm64 & Android
Posts: 16,363

Rep: Reputation: 2335Reputation: 2335Reputation: 2335Reputation: 2335Reputation: 2335Reputation: 2335Reputation: 2335Reputation: 2335Reputation: 2335Reputation: 2335Reputation: 2335
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
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
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



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

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

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