rustianimal

Q: recursively removing symlinks

Hi,

 

I have a number of hard drives on my server that during part of a dedupe process, had many of the original files replaced with symlinks of the type shown here:

 

lrwxr-xr-x@ 1 russtaylor staff 223 12 Mar 09:26 Cycle-R ICT kit & s:w.xlsx -> /Groups/Cycle-R/Old Data (to sort through)/CJ/Library/Mail/V3/6F89F311-CBFD-462B-92CB-336154CCBE2F/INBOX.mbox/Fin ance and Grants.mbox/55347D5B-6FFF-4324-ADD4-C538EA101DE3/Data/2/Attachments/2893/2/Cycl e-R ICT kit & s:w.xlsx

 

I now find myself in the dilemma with some 500,000 or more of these things scattered across the server disks, which makes it nigh impossible to find where the real file is buried on the disk due to each file having maybe upwards of 100 alias references to it. Unfortunately the standard Finder tools and similar file management Apps cannot actually identify an Alias file within the Finder, or my task would be easy, thus I must in instance resort to command line, but am not a terminal guru and don't do much coding work these days, so am rather rusty on relevant command syntax and shell scripting, etc.

 

Ideally I need a simple command line/scripting option along the lines of:

 

Start

for <directory>

if <filename> is type 'folder'

     then open 'folder' and return to Start

else if <filename> is type 'alias/symlink'

     then delete 'filename'

else if <filename> is type 'file'

     then next <filename>

End

 

Thus I want to be able to give the function/script the directory /Groups, and have in recursively drill down through all subdirectories, deleting all alias files as it goes, but leaving everything else intact. The end result would be a directory with no aliases or symlinks in it at any level. `I will then be able to recursively remove orphaned empty folders, leaving me with just those directories and subdirectories that have real files in them, which I can then recover and place somewhere useful!

 

All suggestions gratefully received.

MacBook Pro (15-inch 2.4/2.2 GHz), Mac OS X (10.6.8)

Posted on May 26, 2016 8:12 AM

Close

Q: recursively removing symlinks

  • All replies
  • Helpful answers

  • by Mark Jalbert,

    Mark Jalbert Mark Jalbert May 26, 2016 10:22 AM in response to rustianimal
    Level 5 (4,649 points)
    May 26, 2016 10:22 AM in response to rustianimal

    I'm always hesitant posting solutions that batch remove files but I'll show you how to list them and you can figure out  how to change the commands. To list all symbolic links in the directory Groups:

     

    cd /path/to/Groups

    find . -type l -print0 | xargs -0 -IX echo X

     

    To list all the Finder aliases in the directory Groups

     

    cd /path/to/Groups

    find . -type f -exec ksh -c 'for file

    do

        if    [ "$(file -b "$file")" = "MacOS Alias file" ]

        then  echo "$file"

        fi

    done

    ' sh {} +

  • by Camelot,

    Camelot Camelot May 26, 2016 6:26 PM in response to rustianimal
    Level 8 (47,233 points)
    Mac OS X
    May 26, 2016 6:26 PM in response to rustianimal

    Unfortunately the standard Finder tools and similar file management Apps cannot actually identify an Alias file within the Finder

     

    You mean other than the little alias arrow image on the file's icon?

     

    I guess it's not as easy to look at the icon as it is to look at a list of files (especially if sorted by type), but it is incorrect to say that the Finder can't identify aliases.

     

    Ultimately, the shell may be your friend here, as Mark points out.

  • by Hiroto,

    Hiroto Hiroto May 27, 2016 1:16 AM in response to rustianimal
    Level 5 (7,276 points)
    May 27, 2016 1:16 AM in response to rustianimal

    Hello

     

    If you're actually using OS X 10.6.8 as your current profile states, the second script provided by Mark Jalbert wouldn't work as intended because file(1) under OS X 10.6.8 does not identify alias file as "MacOS Alias file" but generic "data".

     

    But after all I'd presume you're dealing with symlinks and not alias files.

     

    (If you're indeed dealing with alias files under OS X 10.6.8, post back and I'll show you some rubycocoa or pyobjc script to scan alias files in a directory tree.)

     

    H

  • by BobHarris,

    BobHarris BobHarris May 27, 2016 6:26 AM in response to rustianimal
    Level 6 (19,272 points)
    Mac OS X
    May 27, 2016 6:26 AM in response to rustianimal

    Along the lines established by , you can use the 'find' command to find the empty directories

     

    WARNING - the 'find' command can totally destroy your file system if you are not very careful where you point it.  Just like any firearm, you must assume it is always loaded and dangerous.

    find /path/to/starting/directory -type l

    will find and show you all the symbolic links in the specified directory tree.  The -type l is a lowercase L

     

    As Mark shows you can pipe that into xargs which Mark is showing using the 'echo' command (the vertical bar is the pipe symbol and on an U.S. keyboard is typically located on the key just above the <return/enter> key).  But once you are sure you want to start deleting symbolic links after reviewing the output, you replace the 'echo' with 'rm' and you will start deleting things.  NOTE: there is no, zero, nada recovery from the 'rm' command, so be very VERY careful when you decide to use it.

     

    Now if there are any empty directories, you can use

    find /path/to/starting/directory -type d -empty

    to find all the empty directories in the specified directory tree.

     

    Again, using Mark's example, you can pipe the 'find' output to xargs, and instead of 'echo' you can use 'rmdir' to delete the empty directories.

  • by Mark Jalbert,

    Mark Jalbert Mark Jalbert May 27, 2016 7:01 AM in response to Hiroto
    Level 5 (4,649 points)
    May 27, 2016 7:01 AM in response to Hiroto

    Oops, custom magic file. I was attempting to stay close to POSIX standards. Assuming the volume is indexed by spotlight then

     

    mdfind -0 "kMDItemKind == 'Alias'" -onlyin /path/to/Groups | xargs  ..........