1

I was trying to copy one folder from one location to another. The folder is about 6.4 Gb.

So I did

cp -r source_folder level1/val

after that, I went into the level1 folder and checked:

level1$ ls
val

But If I try to cd into val, an error is raised:

level1$ cd val
-bash: cd: val: No such file or directory

And it does not appear to be copying anything, either:

level1$ du -sh val
0   val

I also checked with python if the directory exists or not, but it also says that it does not exist

>>> import os
>>> os.path.exists('level1/val')
False

I can't even delete the folder that has been created:

level1$ rmdir val
rmdir: failed to remove 'val': Not a directory

On the other hand, I was able to delete it as if it was a file:

level1$ rm val
level1$ ls
level1$ 

What is going on? And how can I make sure to copy the folder correctly?

EDIT Added the output of ls -ld source_folder level1/val which returns

lrwxrwxrwx 1 user1 dinfk      4 Jun 20 12:05 source_folder -> test
drwxr-sr-x 2 user2  systems 4096 Aug 27 19:02 level1/val
  • What is the output of ls -ld ource_folder level1/val? – Hauke Laging Aug 27 '17 at 17:07
  • @HaukeLaging Added :) – Ant Aug 27 '17 at 17:11
  • That is very strange. You should run fsck on that volume. – Hauke Laging Aug 27 '17 at 17:29
  • The number 2 in the second column of level1/val listing indicates that there is a hard link to that file-dir thing. My understanding is you can't create hard links to dirs in Bash even though they're technically allowed by the OS. But is it a file or a directory? Except for the d indicator it seems like a file. ... The voodoo going on with that sucker is beyond my pay grade. – B Layer Aug 27 '17 at 18:19
  • 1
    What user was attempting the cp? I would expect that user1 would fail since only the owner, user2, has write permission to level1/val. – Eric Towers Aug 27 '17 at 23:34
2

Evidently, the val that resulted from the copy the first time round is a broken symbolic link.

  • ls lists val because it exists: there is a directory entry called val.
  • cd val complains “No such file or directory” because val is a broken symbolic link. cd needs to access the target of the link, but the target doesn't exist (that's the definition of a broken symlink).
  • du val shows 0 because a symbolic link doesn't use any storage space. (The space for the name and metadata is not counted.)
  • os.path.exists returns False for broken symbolic links.
  • rmdir val rightfully complains that val is not a directory, since it's a symbolic link.
  • rm val deletes val normally, since val is a file that isn't a directory.

You report:

lrwxrwxrwx 1 user1 dinfk      4 Jun 20 12:05 source_folder -> test

The command cp -r copies the symbolic link as a symbolic link. Since source_folder is a symbolic link whose target is test, this results in level1/val being a symbolic link whose target is test. The target of a symbolic link is a simple string, it doesn't “track” anything. Symbolic links that don't start with a / are relative. level1/val is a symbolic link whose target is test so it points to level1/test. Since level1/test doesn't exist, the symbolic link is broken.

Later you saw:

drwxr-sr-x 2 user2  systems 4096 Aug 27 19:02 level1/val

This time you did something different and copied a directory tree.

To copy the target of the link rather than the link itself, you can use

cp -r source_folder/ level1/val

The trailing slash tells the cp command to act on the directory that the link points to rather than on the symbolic link itself. If the argument is a directory, this doesn't make any difference.

  • Thank you very much! Very clear and informative answer. I used your command and it worked perfectly. Much appreciated! :D – Ant Aug 30 '17 at 8:29
3

source_folder - is symlink that refer to the test directory

But you directory path in symlink is relative. If you use absolute path (e.g. /home/user/test) in symlink copying will happen normally.

If you want to copy all files from the directory to which the symbolic link points, you could use -d option with cp command.

  • Thank you! I uses an absolute path to source_folder, but I need to use an absolute path to the folder it links to instead (i.e. test?) Or using -d, so the command would be cp -rd source_folder level1/val ? – Ant Aug 28 '17 at 7:28
  • When you create symlink you type path to the folder to which symlink will refer. Example 1: ln -s test source_folder. Example 2: ln -s /home/user/test source_folder. In first example you use relative path and in second example you use absolute path (which start from root directory - /). – Egor Vasilyev Aug 28 '17 at 7:46
  • path to source_folder is not the same thing that path in symlink – Egor Vasilyev Aug 28 '17 at 7:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.