dsyleixa123
Posts: 1025
Joined: Mon Jun 11, 2018 11:22 am

retrieve a file location by recognizing only the link of the file

Thu Oct 01, 2020 10:46 am

hello,
I have a file link on my Stretch Desktop, related to a file stored somewhere in any a nested subdir.

Reading the link's property informations, it shows the path of the link (home/pi/Desktop) but I can't recognize the path of the origin (the whole path is reduced/truncated and so unreadable corrupted)...
what am I missing?

User avatar
jahboater
Posts: 6274
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: retrieve a file location by recognizing only the link of the file

Thu Oct 01, 2020 11:26 am

Does the "file" command help?

Code: Select all

$ file joe
joe: symbolic link to fred
Pi4 8GB running PIOS64 Lite

dsyleixa123
Posts: 1025
Joined: Mon Jun 11, 2018 11:22 am

Re: retrieve a file location by recognizing only the link of the file

Thu Oct 01, 2020 1:30 pm

yes, thank you, a little cumbersome (as usual for Linux), but it works! :mrgreen:

User avatar
jahboater
Posts: 6274
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: retrieve a file location by recognizing only the link of the file

Thu Oct 01, 2020 2:22 pm

"ls" of course can do it too, I just use "file" out of habit:

Code: Select all

$ ls -l joe
lrwxrwxrwx 1 jah jah 4 Oct  1 15:18 joe -> fred
or the standard alias (which is better because it shows its a directory):

Code: Select all

$ ll joe
lrwxrwxrwx 1 jah jah 4 Oct  1 15:18 joe -> fred/
If you think "ll" is too cumbersome, then it would be trivial to write a small C program, giving you a command with the name or syntax you prefer:
See "man 2 open" and look at the options O_NOFOLLOW and O_PATH
or just use an alias:

Code: Select all

alias follow_soft_link file
Pi4 8GB running PIOS64 Lite

User avatar
dickon
Posts: 1802
Joined: Sun Dec 09, 2012 3:54 pm
Location: Home, just outside Reading

Re: retrieve a file location by recognizing only the link of the file

Thu Oct 01, 2020 2:40 pm

realpath is more useful, as it will follow all the symlinks in the path and return the canonical name:

Code: Select all

newdesktop:/tmp/ (1) 42 $ touch a
newdesktop:/tmp/ (1) 43 $ ln -s a b
newdesktop:/tmp/ (1) 44 $ ln -s b c
newdesktop:/tmp/ (1) 45 $ realpath c
/tmp/a
newdesktop:/tmp/ (1) 46 $ 

User avatar
jahboater
Posts: 6274
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: retrieve a file location by recognizing only the link of the file

Thu Oct 01, 2020 2:48 pm

Cool!

There is readlink too, but it only opens the first file.
Pi4 8GB running PIOS64 Lite

dsyleixa123
Posts: 1025
Joined: Mon Jun 11, 2018 11:22 am

Re: retrieve a file location by recognizing only the link of the file

Thu Oct 01, 2020 3:19 pm

thanks for the friendly advices!
All terminal commands work, but what I meant what would be more convenient is if one could read all path information just out of the link-file properties, e.g. by enlarging the properties window or scroll through the explicite uncorrupted pathname . :geek: :ugeek: :? 8-)

User avatar
jahboater
Posts: 6274
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: retrieve a file location by recognizing only the link of the file

Thu Oct 01, 2020 5:10 pm

dsyleixa123 wrote:
Thu Oct 01, 2020 3:19 pm
thanks for the friendly advices!
All terminal commands work, but what I meant what would be more convenient is if one could read all path information just out of the link-file properties, e.g. by enlarging the properties window or scroll through the explicite uncorrupted pathname . :geek: :ugeek: :? 8-)
It is the contents of the soft link file, not its properties.

Soft links were invented to allow a link to cross filesystem boundaries.
Pi4 8GB running PIOS64 Lite

dsyleixa123
Posts: 1025
Joined: Mon Jun 11, 2018 11:22 am

Re: retrieve a file location by recognizing only the link of the file

Thu Oct 01, 2020 5:32 pm

the target file+path is supposed to be shown in the linkfile-properties, and in fact, actually it's there - but totally corrupted and unreadable.

User avatar
jahboater
Posts: 6274
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: retrieve a file location by recognizing only the link of the file

Thu Oct 01, 2020 5:45 pm

Hard links work by having two entries in a directory (which is a file containing filename/inode number pairs) with the same inode number. Thus the file has two names. They are indistinguishable from each other.
The trouble with that is file system specific, the inodes are local to the filesystem.

Soft links work by having the filename of the target file contained within another file. Nothing to do with inodes, and so the filename or path name of the target file may easily point to another filesystem. Its as simple as that.
There is a mode bit set in the soft link file to say what it is. The system will automatically and transparently open the soft link file, read the new path from the file, and use that.
Pi4 8GB running PIOS64 Lite

User avatar
dickon
Posts: 1802
Joined: Sun Dec 09, 2012 3:54 pm
Location: Home, just outside Reading

Re: retrieve a file location by recognizing only the link of the file

Thu Oct 01, 2020 5:54 pm

Nit pick: the directories with the two (or more) entries do not have to be the same.

Code: Select all

newdesktop:/tmp/ (1) 78 $ mkdir d
newdesktop:/tmp/ (1) 79 $ ln a d/a
newdesktop:/tmp/ (1) 80 $ ls -li a d/a
4295316 -rw------- 2 dickon dickon 0 Oct  1 18:51 a
4295316 -rw------- 2 dickon dickon 0 Oct  1 18:51 d/a
newdesktop:/tmp/ (1) 81 $ 
OP appears to be using some sort of file manager, which displays the link contents when requested. But it mangles them. I have no answer to that as I simply don't use one and wouldn't know where to begin.

Code: Select all

newdesktop:/tmp/ (1) 81 $ pgrep xterm | wc -l
104
newdesktop:/tmp/ (1) 82 $ 

User avatar
jahboater
Posts: 6274
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: retrieve a file location by recognizing only the link of the file

Thu Oct 01, 2020 6:02 pm

dickon wrote:
Thu Oct 01, 2020 5:54 pm
Nit pick: the directories with the two (or more) entries do not have to be the same.
Yes. Of course.
I was trying to keep the explanation reasonably simple to illustrate the difference between hard and soft links!
Like not mentioning reference counts and deletion.
Pi4 8GB running PIOS64 Lite

dsyleixa123
Posts: 1025
Joined: Mon Jun 11, 2018 11:22 am

Re: retrieve a file location by recognizing only the link of the file

Thu Oct 01, 2020 7:56 pm

jahboater wrote:
Thu Oct 01, 2020 5:45 pm
Hard links work by having two entries in a directory (which is a file containing filename/inode number pairs) with the same inode number. Thus the file has two names. They are indistinguishable from each other.
The trouble with that is file system specific, the inodes are local to the filesystem.

Soft links work by having the filename of the target file contained within another file. Nothing to do with inodes, and so the filename or path name of the target file may easily point to another filesystem. Its as simple as that.
There is a mode bit set in the soft link file to say what it is. The system will automatically and transparently open the soft link file, read the new path from the file, and use that.
no, you do not understand: the linked original file+path (target) is visible in the desktop linkfile properties, but totally truncated, mutilated, corrupted, and thus messed up and unreadable. But the target name is expected to be written out completely.
Like e.g., when opening the link's properties (context menu):

Code: Select all

file name: logfile_pi07.dat

Code: Select all

location: /home/pi/Desktop

exploited pathname to origin, visible:

Code: Select all

target: /home/pi/s...pi07.dat
explicitely it must be instead
/home/pi/settings/backup/logfile_pi07.dat

Return to “General discussion”