LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Find files linked to file (https://www.linuxquestions.org/questions/linux-software-2/find-files-linked-to-file-568698/)

x_terminat_or_3 07-12-2007 05:50 AM

Find files linked to file
 
Hi there

I would like to do a search to find all files that link to a certain file. Either soft-linked or hard-linked.

Basically we have folders that contain previous versions. The version currently in use is softlinked.

Example

myfile-1.0.1.ext
myfile-1.1.9.ext

myfile.ext -> myfile-1.1.9.ext


Now I want to go through all the folders and move the previous versions to a different folder.

Any ideas?

Simon Bridge 07-12-2007 06:14 AM

The current version (softlinked) is not the one you want to find. You want to find previous versions.... these are not linked to.

There is no way of looking at a file and knowing what links to it. Tough. You need to write a script that looks (grep?) through all likely directories for the filename-glob you want to move (myfile-*.ext) and then do the bulk move.

Better management is to keep the previous versions in one place, then the cleanup script is simpler.

bigrigdriver 07-12-2007 06:18 AM

Ls can show you existing links, if the correct options are used. Here's an article: http://www.linuxclues.com/articles/17.htm.

Previous versions though, you would have to search for the basename (filename without version number) of each file that turns up in the ls listing.

ls [options] the contents of a directory;
pipe through an awk script to strip the basename;
pipe through find [some top level directory] to recursively search down through the tree;
pipe through grep to select the basename;
pipe though tee to write the list to a file.

Wiser heads probably know a better way.

stress_junkie 07-12-2007 06:56 AM

For reasons listed by SimonBridge it is easier to find the current version and move them. Something like this might work.
Code:

find  / -mount -type l -printf "%h/%f %l\n" -exec cp {} /destination/dir \;
This needs some work. If more than one link points to one target file then the target of the links will be written more than once. Also, this solution could be a problem if a link points to a directory.

The format string for the printf action is a little bit funny because I had actually misunderstood your question so I was answering a question that you didn't ask. When I realized my mistake I didn't change the format string when I reworked my solution. You probably don't need the newline at the end of the format string.

x_terminat_or_3 07-12-2007 07:18 AM

Thanks all for your input. Looks like I'll probably be better of writing something in php that compares inode numbers to find hard links and readlink for soft links.


All times are GMT -5. The time now is 08:32 PM.