Linux 'cp' copies symlink instead of target file

Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
In my directory I have 2 files: a text file and a symbolic link to that text file.
base_v21.txt -> base_v001.txt
base_v001.txt
When I try to copy base_v001.txt using the following command:
# cp base_v001.txt /targetdir
Instead of base_v001.txt getting copied, base_v21.txt is copied.
# ls /targetdir
base_v21.txt
So even though I specify the file I want to copy by the full filename the symbolic link gets copied instead.
I've seen posts about using -L or -H to dereference symlinks, but that doesn't really apply to my situation, because I'm trying to copy the target file itself; not the symlink.
As for why the symlink and the target file are in the same directory, I have no control over that.
linux symlink cp
add a comment |Â
up vote
1
down vote
favorite
In my directory I have 2 files: a text file and a symbolic link to that text file.
base_v21.txt -> base_v001.txt
base_v001.txt
When I try to copy base_v001.txt using the following command:
# cp base_v001.txt /targetdir
Instead of base_v001.txt getting copied, base_v21.txt is copied.
# ls /targetdir
base_v21.txt
So even though I specify the file I want to copy by the full filename the symbolic link gets copied instead.
I've seen posts about using -L or -H to dereference symlinks, but that doesn't really apply to my situation, because I'm trying to copy the target file itself; not the symlink.
As for why the symlink and the target file are in the same directory, I have no control over that.
linux symlink cp
3
You shuold edit your question to make it understandable. Check the "base" names....
â schily
Aug 9 at 6:21
4
giving the output ofls -linstead of onlylswould make things more clear for all the answerers
â A.B
Aug 9 at 6:32
Show the command that you used to create the symbolic link and also correct the filenames (you reference a filename that isbase_v21.txtin the text, but not in the commands).
â Kusalananda
Aug 9 at 7:00
cp base_v001.txt /targetdirproduces/targetdir/base_v21.txt? Are you absolutely sure? There's no way for the system to know what symlinks are pointing to that file. I would understand this if you used the symlink as the copy source and got the name from the symlink destination instead, or something like that.
â ilkkachu
Aug 14 at 18:48
@ilkkachu Yes, I understand it's a odd result. I've searched the web for a similar occurrence, but so far have not seen any. It forced me to make an account and post this question.
â ETD
Aug 14 at 21:57
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
In my directory I have 2 files: a text file and a symbolic link to that text file.
base_v21.txt -> base_v001.txt
base_v001.txt
When I try to copy base_v001.txt using the following command:
# cp base_v001.txt /targetdir
Instead of base_v001.txt getting copied, base_v21.txt is copied.
# ls /targetdir
base_v21.txt
So even though I specify the file I want to copy by the full filename the symbolic link gets copied instead.
I've seen posts about using -L or -H to dereference symlinks, but that doesn't really apply to my situation, because I'm trying to copy the target file itself; not the symlink.
As for why the symlink and the target file are in the same directory, I have no control over that.
linux symlink cp
In my directory I have 2 files: a text file and a symbolic link to that text file.
base_v21.txt -> base_v001.txt
base_v001.txt
When I try to copy base_v001.txt using the following command:
# cp base_v001.txt /targetdir
Instead of base_v001.txt getting copied, base_v21.txt is copied.
# ls /targetdir
base_v21.txt
So even though I specify the file I want to copy by the full filename the symbolic link gets copied instead.
I've seen posts about using -L or -H to dereference symlinks, but that doesn't really apply to my situation, because I'm trying to copy the target file itself; not the symlink.
As for why the symlink and the target file are in the same directory, I have no control over that.
linux symlink cp
linux symlink cp
edited Aug 14 at 19:01
ilkkachu
50.9k678140
50.9k678140
asked Aug 9 at 0:08
ETD
142
142
3
You shuold edit your question to make it understandable. Check the "base" names....
â schily
Aug 9 at 6:21
4
giving the output ofls -linstead of onlylswould make things more clear for all the answerers
â A.B
Aug 9 at 6:32
Show the command that you used to create the symbolic link and also correct the filenames (you reference a filename that isbase_v21.txtin the text, but not in the commands).
â Kusalananda
Aug 9 at 7:00
cp base_v001.txt /targetdirproduces/targetdir/base_v21.txt? Are you absolutely sure? There's no way for the system to know what symlinks are pointing to that file. I would understand this if you used the symlink as the copy source and got the name from the symlink destination instead, or something like that.
â ilkkachu
Aug 14 at 18:48
@ilkkachu Yes, I understand it's a odd result. I've searched the web for a similar occurrence, but so far have not seen any. It forced me to make an account and post this question.
â ETD
Aug 14 at 21:57
add a comment |Â
3
You shuold edit your question to make it understandable. Check the "base" names....
â schily
Aug 9 at 6:21
4
giving the output ofls -linstead of onlylswould make things more clear for all the answerers
â A.B
Aug 9 at 6:32
Show the command that you used to create the symbolic link and also correct the filenames (you reference a filename that isbase_v21.txtin the text, but not in the commands).
â Kusalananda
Aug 9 at 7:00
cp base_v001.txt /targetdirproduces/targetdir/base_v21.txt? Are you absolutely sure? There's no way for the system to know what symlinks are pointing to that file. I would understand this if you used the symlink as the copy source and got the name from the symlink destination instead, or something like that.
â ilkkachu
Aug 14 at 18:48
@ilkkachu Yes, I understand it's a odd result. I've searched the web for a similar occurrence, but so far have not seen any. It forced me to make an account and post this question.
â ETD
Aug 14 at 21:57
3
3
You shuold edit your question to make it understandable. Check the "base" names....
â schily
Aug 9 at 6:21
You shuold edit your question to make it understandable. Check the "base" names....
â schily
Aug 9 at 6:21
4
4
giving the output of
ls -l instead of only ls would make things more clear for all the answerersâ A.B
Aug 9 at 6:32
giving the output of
ls -l instead of only ls would make things more clear for all the answerersâ A.B
Aug 9 at 6:32
Show the command that you used to create the symbolic link and also correct the filenames (you reference a filename that is
base_v21.txt in the text, but not in the commands).â Kusalananda
Aug 9 at 7:00
Show the command that you used to create the symbolic link and also correct the filenames (you reference a filename that is
base_v21.txt in the text, but not in the commands).â Kusalananda
Aug 9 at 7:00
cp base_v001.txt /targetdir produces /targetdir/base_v21.txt? Are you absolutely sure? There's no way for the system to know what symlinks are pointing to that file. I would understand this if you used the symlink as the copy source and got the name from the symlink destination instead, or something like that.â ilkkachu
Aug 14 at 18:48
cp base_v001.txt /targetdir produces /targetdir/base_v21.txt? Are you absolutely sure? There's no way for the system to know what symlinks are pointing to that file. I would understand this if you used the symlink as the copy source and got the name from the symlink destination instead, or something like that.â ilkkachu
Aug 14 at 18:48
@ilkkachu Yes, I understand it's a odd result. I've searched the web for a similar occurrence, but so far have not seen any. It forced me to make an account and post this question.
â ETD
Aug 14 at 21:57
@ilkkachu Yes, I understand it's a odd result. I've searched the web for a similar occurrence, but so far have not seen any. It forced me to make an account and post this question.
â ETD
Aug 14 at 21:57
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
2
down vote
Copying to a directory creates a new file with the same name as the source file in the target directory, if a new file name is not given. So the following commands will give the same output:
> cp base_v21.txt /targetdir
> cp base_v21.txt /targetdir/base_v21.txt
These create a new file called "base_v21.txt" with the contents of the file in the source directory. In this case, these are the contents of "base_v001.txt" because of the symbolic link. Essentially, you are doing the following:
> cat base_v21.txt >> /targetdir/base_v21.txt
Thus it is to be expected that the following commands give the same result after copying:
> cat base_v21.txt
> cat base_v001.txt
> cat /targetdir/base_v21.txt
You can see that this occurs with either a hard link or symbolic link in the following examples:
> touch base_v001.txt
> ln -s base_v001.txt base_v21.txt #create soft link
> cat "test" >> base_v001.txt
> mkdir dir
> cd dir
> cp ../base_v21.txt .
> ls
base_v21.txt
>cat base_v21.txt
test
> ln base_v001.txt base_v21.txt #create hard link
> cat "test" >> base_v001.txt
> mkdir dir
> cd dir
> cp ../base_v21.txt .
> ls
base_v21.txt
>cat base_v21.txt
test
add a comment |Â
up vote
0
down vote
Try this,
let the destination be /tmp/
If you want to copy the target file but not the symlink, :
readlink -ne base.txt | xargs -0 cp -t /tmp/
add a comment |Â
up vote
0
down vote
Indeed, that is a very strange behavior.
Check and see what conflicting arguments (if any) are aliased to the cp command.
To do this, run the type command. (i.e. type cp)
Other than that, you can try to perform your operation with an alternative utility such as rsync which also has support for handling symlinks (with the arguments --links --copy-links and --hard-links)
It seems the OP describes a situation in which running the commandcp base_v001.txt /targetdirreturns with the file (symbolic link) named/targetdir/base_v21.txt. You don't find that strange?
â Eminent
Aug 14 at 18:39
Wait, I read the file names wrong. The way it's actually written is so weird I must have mentally blocked the idea, or something... Sorry.
â ilkkachu
Aug 14 at 18:45
That's alright. It is weird to imaginecpbehaving as described, but that is how the question is worded.
â Eminent
Aug 14 at 20:12
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Copying to a directory creates a new file with the same name as the source file in the target directory, if a new file name is not given. So the following commands will give the same output:
> cp base_v21.txt /targetdir
> cp base_v21.txt /targetdir/base_v21.txt
These create a new file called "base_v21.txt" with the contents of the file in the source directory. In this case, these are the contents of "base_v001.txt" because of the symbolic link. Essentially, you are doing the following:
> cat base_v21.txt >> /targetdir/base_v21.txt
Thus it is to be expected that the following commands give the same result after copying:
> cat base_v21.txt
> cat base_v001.txt
> cat /targetdir/base_v21.txt
You can see that this occurs with either a hard link or symbolic link in the following examples:
> touch base_v001.txt
> ln -s base_v001.txt base_v21.txt #create soft link
> cat "test" >> base_v001.txt
> mkdir dir
> cd dir
> cp ../base_v21.txt .
> ls
base_v21.txt
>cat base_v21.txt
test
> ln base_v001.txt base_v21.txt #create hard link
> cat "test" >> base_v001.txt
> mkdir dir
> cd dir
> cp ../base_v21.txt .
> ls
base_v21.txt
>cat base_v21.txt
test
add a comment |Â
up vote
2
down vote
Copying to a directory creates a new file with the same name as the source file in the target directory, if a new file name is not given. So the following commands will give the same output:
> cp base_v21.txt /targetdir
> cp base_v21.txt /targetdir/base_v21.txt
These create a new file called "base_v21.txt" with the contents of the file in the source directory. In this case, these are the contents of "base_v001.txt" because of the symbolic link. Essentially, you are doing the following:
> cat base_v21.txt >> /targetdir/base_v21.txt
Thus it is to be expected that the following commands give the same result after copying:
> cat base_v21.txt
> cat base_v001.txt
> cat /targetdir/base_v21.txt
You can see that this occurs with either a hard link or symbolic link in the following examples:
> touch base_v001.txt
> ln -s base_v001.txt base_v21.txt #create soft link
> cat "test" >> base_v001.txt
> mkdir dir
> cd dir
> cp ../base_v21.txt .
> ls
base_v21.txt
>cat base_v21.txt
test
> ln base_v001.txt base_v21.txt #create hard link
> cat "test" >> base_v001.txt
> mkdir dir
> cd dir
> cp ../base_v21.txt .
> ls
base_v21.txt
>cat base_v21.txt
test
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Copying to a directory creates a new file with the same name as the source file in the target directory, if a new file name is not given. So the following commands will give the same output:
> cp base_v21.txt /targetdir
> cp base_v21.txt /targetdir/base_v21.txt
These create a new file called "base_v21.txt" with the contents of the file in the source directory. In this case, these are the contents of "base_v001.txt" because of the symbolic link. Essentially, you are doing the following:
> cat base_v21.txt >> /targetdir/base_v21.txt
Thus it is to be expected that the following commands give the same result after copying:
> cat base_v21.txt
> cat base_v001.txt
> cat /targetdir/base_v21.txt
You can see that this occurs with either a hard link or symbolic link in the following examples:
> touch base_v001.txt
> ln -s base_v001.txt base_v21.txt #create soft link
> cat "test" >> base_v001.txt
> mkdir dir
> cd dir
> cp ../base_v21.txt .
> ls
base_v21.txt
>cat base_v21.txt
test
> ln base_v001.txt base_v21.txt #create hard link
> cat "test" >> base_v001.txt
> mkdir dir
> cd dir
> cp ../base_v21.txt .
> ls
base_v21.txt
>cat base_v21.txt
test
Copying to a directory creates a new file with the same name as the source file in the target directory, if a new file name is not given. So the following commands will give the same output:
> cp base_v21.txt /targetdir
> cp base_v21.txt /targetdir/base_v21.txt
These create a new file called "base_v21.txt" with the contents of the file in the source directory. In this case, these are the contents of "base_v001.txt" because of the symbolic link. Essentially, you are doing the following:
> cat base_v21.txt >> /targetdir/base_v21.txt
Thus it is to be expected that the following commands give the same result after copying:
> cat base_v21.txt
> cat base_v001.txt
> cat /targetdir/base_v21.txt
You can see that this occurs with either a hard link or symbolic link in the following examples:
> touch base_v001.txt
> ln -s base_v001.txt base_v21.txt #create soft link
> cat "test" >> base_v001.txt
> mkdir dir
> cd dir
> cp ../base_v21.txt .
> ls
base_v21.txt
>cat base_v21.txt
test
> ln base_v001.txt base_v21.txt #create hard link
> cat "test" >> base_v001.txt
> mkdir dir
> cd dir
> cp ../base_v21.txt .
> ls
base_v21.txt
>cat base_v21.txt
test
edited Aug 14 at 17:41
ilkkachu
50.9k678140
50.9k678140
answered Aug 9 at 5:02
Tom Kelly
31726
31726
add a comment |Â
add a comment |Â
up vote
0
down vote
Try this,
let the destination be /tmp/
If you want to copy the target file but not the symlink, :
readlink -ne base.txt | xargs -0 cp -t /tmp/
add a comment |Â
up vote
0
down vote
Try this,
let the destination be /tmp/
If you want to copy the target file but not the symlink, :
readlink -ne base.txt | xargs -0 cp -t /tmp/
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Try this,
let the destination be /tmp/
If you want to copy the target file but not the symlink, :
readlink -ne base.txt | xargs -0 cp -t /tmp/
Try this,
let the destination be /tmp/
If you want to copy the target file but not the symlink, :
readlink -ne base.txt | xargs -0 cp -t /tmp/
edited Aug 9 at 6:33
answered Aug 9 at 6:27
sai sasanka
54019
54019
add a comment |Â
add a comment |Â
up vote
0
down vote
Indeed, that is a very strange behavior.
Check and see what conflicting arguments (if any) are aliased to the cp command.
To do this, run the type command. (i.e. type cp)
Other than that, you can try to perform your operation with an alternative utility such as rsync which also has support for handling symlinks (with the arguments --links --copy-links and --hard-links)
It seems the OP describes a situation in which running the commandcp base_v001.txt /targetdirreturns with the file (symbolic link) named/targetdir/base_v21.txt. You don't find that strange?
â Eminent
Aug 14 at 18:39
Wait, I read the file names wrong. The way it's actually written is so weird I must have mentally blocked the idea, or something... Sorry.
â ilkkachu
Aug 14 at 18:45
That's alright. It is weird to imaginecpbehaving as described, but that is how the question is worded.
â Eminent
Aug 14 at 20:12
add a comment |Â
up vote
0
down vote
Indeed, that is a very strange behavior.
Check and see what conflicting arguments (if any) are aliased to the cp command.
To do this, run the type command. (i.e. type cp)
Other than that, you can try to perform your operation with an alternative utility such as rsync which also has support for handling symlinks (with the arguments --links --copy-links and --hard-links)
It seems the OP describes a situation in which running the commandcp base_v001.txt /targetdirreturns with the file (symbolic link) named/targetdir/base_v21.txt. You don't find that strange?
â Eminent
Aug 14 at 18:39
Wait, I read the file names wrong. The way it's actually written is so weird I must have mentally blocked the idea, or something... Sorry.
â ilkkachu
Aug 14 at 18:45
That's alright. It is weird to imaginecpbehaving as described, but that is how the question is worded.
â Eminent
Aug 14 at 20:12
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Indeed, that is a very strange behavior.
Check and see what conflicting arguments (if any) are aliased to the cp command.
To do this, run the type command. (i.e. type cp)
Other than that, you can try to perform your operation with an alternative utility such as rsync which also has support for handling symlinks (with the arguments --links --copy-links and --hard-links)
Indeed, that is a very strange behavior.
Check and see what conflicting arguments (if any) are aliased to the cp command.
To do this, run the type command. (i.e. type cp)
Other than that, you can try to perform your operation with an alternative utility such as rsync which also has support for handling symlinks (with the arguments --links --copy-links and --hard-links)
edited Aug 14 at 18:46
ilkkachu
50.9k678140
50.9k678140
answered Aug 9 at 1:31
Eminent
112
112
It seems the OP describes a situation in which running the commandcp base_v001.txt /targetdirreturns with the file (symbolic link) named/targetdir/base_v21.txt. You don't find that strange?
â Eminent
Aug 14 at 18:39
Wait, I read the file names wrong. The way it's actually written is so weird I must have mentally blocked the idea, or something... Sorry.
â ilkkachu
Aug 14 at 18:45
That's alright. It is weird to imaginecpbehaving as described, but that is how the question is worded.
â Eminent
Aug 14 at 20:12
add a comment |Â
It seems the OP describes a situation in which running the commandcp base_v001.txt /targetdirreturns with the file (symbolic link) named/targetdir/base_v21.txt. You don't find that strange?
â Eminent
Aug 14 at 18:39
Wait, I read the file names wrong. The way it's actually written is so weird I must have mentally blocked the idea, or something... Sorry.
â ilkkachu
Aug 14 at 18:45
That's alright. It is weird to imaginecpbehaving as described, but that is how the question is worded.
â Eminent
Aug 14 at 20:12
It seems the OP describes a situation in which running the command
cp base_v001.txt /targetdir returns with the file (symbolic link) named /targetdir/base_v21.txt. You don't find that strange?â Eminent
Aug 14 at 18:39
It seems the OP describes a situation in which running the command
cp base_v001.txt /targetdir returns with the file (symbolic link) named /targetdir/base_v21.txt. You don't find that strange?â Eminent
Aug 14 at 18:39
Wait, I read the file names wrong. The way it's actually written is so weird I must have mentally blocked the idea, or something... Sorry.
â ilkkachu
Aug 14 at 18:45
Wait, I read the file names wrong. The way it's actually written is so weird I must have mentally blocked the idea, or something... Sorry.
â ilkkachu
Aug 14 at 18:45
That's alright. It is weird to imagine
cp behaving as described, but that is how the question is worded.â Eminent
Aug 14 at 20:12
That's alright. It is weird to imagine
cp behaving as described, but that is how the question is worded.â Eminent
Aug 14 at 20:12
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f461405%2flinux-cp-copies-symlink-instead-of-target-file%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
3
You shuold edit your question to make it understandable. Check the "base" names....
â schily
Aug 9 at 6:21
4
giving the output of
ls -linstead of onlylswould make things more clear for all the answerersâ A.B
Aug 9 at 6:32
Show the command that you used to create the symbolic link and also correct the filenames (you reference a filename that is
base_v21.txtin the text, but not in the commands).â Kusalananda
Aug 9 at 7:00
cp base_v001.txt /targetdirproduces/targetdir/base_v21.txt? Are you absolutely sure? There's no way for the system to know what symlinks are pointing to that file. I would understand this if you used the symlink as the copy source and got the name from the symlink destination instead, or something like that.â ilkkachu
Aug 14 at 18:48
@ilkkachu Yes, I understand it's a odd result. I've searched the web for a similar occurrence, but so far have not seen any. It forced me to make an account and post this question.
â ETD
Aug 14 at 21:57