ls shows a directory but it is inaccessible
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?
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
source_folder – is symlink that refer to the
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
val that resulted from the copy the first time round is a broken symbolic link.
valbecause it exists: there is a directory entry called
cd valcomplains “No such file or directory” because
valis a broken symbolic link.
cdneeds to access the target of the link, but the target doesn’t exist (that’s the definition of a broken symlink).
du valshows 0 because a symbolic link doesn’t use any storage space. (The space for the name and metadata is not counted.)
Falsefor broken symbolic links.
rmdir valrightfully complains that
valis not a directory, since it’s a symbolic link.
valis a file that isn’t a directory.
lrwxrwxrwx 1 user1 dinfk 4 Jun 20 12:05 source_folder -> test
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 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.
In case someone also appears to have this particular problem where the proposed solution does not work: Make sure that you really spelled the directory name correctly and pay special attention to special (non-ASCII) characters in its name.
In my case I had the following situation:
$ ll total 68 -rw-r--r-- 1 st_ac131646 st_us-031110 1387 29. Jun 10:02 cu2_o2_nh3_6-1_60.xyz -rw-r--r-- 1 st_ac131646 st_us-031110 1387 29. Jun 10:03 cu2_o2_nh3_6-2_20.xyz drwxr-xr-x 2 st_ac131646 st_us-031110 4096 29. Jun 09:17 rḱs drwxr-xr-x 2 st_ac131646 st_us-031110 4096 29. Jun 13:20 tests
But when I tried e.g.
cd rks or
rmdir rks, the system would always tell me that no such file or directory existed. I could even do a
mkdir rks and now all of a sudden there appeared a second directory with this name in the
ls output (which was accessible as one would expect).
In any case, when you look closer, I must have accidentally created the first directory with a
ḱ instead of a regular
k which is why when typing the name as
rks (instead of
rḱs) the system obviously told me that this does not exist.
So long story short: Double-check your spelling and potentially try copy-pasting the name from the