ls shows a directory but it is inaccessible

15,760

Solution 1

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.

Solution 2

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.

Share:
15,760

Related videos on Youtube

Ant
Author by

Ant

“But this long run is a misleading guide to current affairs. In the long run we are all dead. Economists set themselves too easy, too useless a task if in tempestuous seasons they can only tell us that when the storm is long past the ocean is flat again." (Keynes)

Updated on September 18, 2022

Comments

  • Ant
    Ant almost 2 years

    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
    
    • Hauke Laging
      Hauke Laging almost 7 years
      What is the output of ls -ld ource_folder level1/val?
    • Ant
      Ant almost 7 years
      @HaukeLaging Added :)
    • Hauke Laging
      Hauke Laging almost 7 years
      That is very strange. You should run fsck on that volume.
    • B Layer
      B Layer almost 7 years
      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.
    • Egor Vasilyev
      Egor Vasilyev almost 7 years
      df -h shows different sizes before and after copying? 6,4Gb is sufficiently large to detect this difference.
    • Eric Towers
      Eric Towers almost 7 years
      What user was attempting the cp? I would expect that user1 would fail since only the owner, user2, has write permission to level1/val.
    • Ant
      Ant almost 7 years
      @EgorVasilyev Do you mean the size of source_folder after performing the command? It is still 6.4 Gb
    • Ant
      Ant almost 7 years
      @EricTowers It was user2, the owner of level1/val
    • Eric Towers
      Eric Towers almost 7 years
      Does user2 have read permission on /test and all of its contents? (The lrwxrwxrwx permissions on the link tell us nothing about the permissions at the target of the link.)
  • Ant
    Ant almost 7 years
    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 ?
  • Egor Vasilyev
    Egor Vasilyev almost 7 years
    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
    Egor Vasilyev almost 7 years
    path to source_folder is not the same thing that path in symlink
  • Ant
    Ant almost 7 years
    Thank you very much! Very clear and informative answer. I used your command and it worked perfectly. Much appreciated! :D