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? |
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. |
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. |
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 \; 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. |
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. |