Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 06-13-2013, 07:27 AM   #1
Registered: Aug 2005
Location: Brazil
Distribution: Debian Jessie
Posts: 772

Rep: Reputation: 74
Copy recursively ignoring links

I need to copy an entire directory, recursively. And I need to ignore links. It's part of a script and I really need to do that.

'cp --no-dereference' doesn't work for me, because it copies the links. It doesn't follow them, but tries to copy them.

I've read 'man cp' a dozen times and if there is a way to ignore links, it's very well hidden.

Any suggestions?
Old 06-13-2013, 08:29 AM   #2
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,406

Rep: Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396
Have you considered using 'find' to select the files for you.
Use the '-type f' and -P' switches

Never follow symbolic links. This is the default behaviour. When find examines or prints information a file, and the file is a symbolic link, the information used shall be taken from the properties of the symbolic link itself.
Old 06-13-2013, 08:47 AM   #3
Registered: Mar 2006
Location: Delft NL
Distribution: Debian; Slackware; windows 7
Posts: 218

Rep: Reputation: 53
If you can stand to copy the links, then remove them you could use the find command.

cp -r --no-derefrence <src> <dest>
Then remove the links with the find command.
find <dest> -type l -delete
I have just seen the previous solution, and it's better. Having come up with my
solution I thought I ought to do at least a minimal test to make sure my solution

Last time I posted without testing I added an unneeded (that actually did not
exist) flag .

Last edited by archShade; 06-13-2013 at 08:54 AM.


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 On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
copy recursively across directories stefaandk *BSD 1 11-09-2008 10:01 PM
how to copy images recursively via bash mindfriction Programming 6 12-13-2007 10:25 PM
Using cp to copy directory permissions recursively? SirTristan Linux - Newbie 3 11-25-2007 08:36 AM
trying to recursively copy jpegs DJOtaku Linux - General 10 08-26-2005 01:09 PM
ripping all links of a site recursively rsingh Linux - Newbie 1 10-29-2003 02:03 PM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:18 PM.

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