Search code examples
linuxpowershelldirectorydiff

diff compare directories by filename only


Is it possible to use any sort of diff utility to diff based on filename only, excluding the file extensions? I have multiple dirs that have various versions of a file, ie media.mov, media.mp4, media.jpg, etc. I want to make sure all versions were made for each file (1000s). So /dir1/media_99.mov and /dir2/media_99.mp4 would yield a TRUE condition. Man diff does not have "--ignore-extension" and I'm not sure how to possibly use "--exclude-from=FILE". I can use Linux (preferred) or PowerShell (if I must)


Solution

  • In PowerShell, if you want to know which file names are unique to /dir1, use a Compare-Object call, followed by reducing those file names to their base name (file name without extension), weeding out duplicates, and sorting via Sort-Object

    Compare-Object -PassThru -Property Name (Get-ChildItem -File /dir1) (Get-ChildItem -File /dir2) |
      Where-Object SideIndicator -eq '<=' |
        ForEach-Object BaseName |
          Sort-Object -Unique
    

    Note: The assumption is that both Get-ChildItem calls return at least one file-info object, otherwise the Compare-Object call will fail - guard against that with if statements, if necessary.