列出终端中文件的绝对路径 w/more 超过 255 个字符

Listing Absolute Paths of Files in Terminal w/more than 255 Characters

我有一个两部分问题:

第 1 部分(终端)- 我在存档到基于 Windows 的系统的 Mac 环境中工作,因此我需要检测 255 的路径长度,以便我可以更改这些文件存档前。我在 Whosebug 上看到了很多关于文件名长度的解决方案,但我需要绝对路径长度。我偶然发现了这个脚本,它很接近:

sudo find . -name \* -type f | perl -ne 's/(.*)/print length(), " \n" if (length()>254)/e' | sort -n

问题是我没有看到绝对路径长度,我只看到了当前目录的路径长度。有没有一种方法可以在当前目录中递归搜索文件,同时仍然显示所有列出文件的绝对路径?

第 2 部分 (Applescript) - 一旦第 1 部分在终端中正常工作,我希望稍微自动化该过程。最好我想创建一个 Applescript,当文件夹在 Finder 中突出显示时,我可以 运行 命令,终端将弹出,运行 命令和列表。这是我目前所拥有的:

tell application "Finder" to set theSel to selection
tell application "Terminal" 
set theFol to POSIX path of ((item 1 of theSel) as text)    
if (count of windows) is not 0 then 
    set shell to do script "cd " & quoted form of theFol in window 1        
do script "find . -name \* -type f | perl -ne 's/(.*)/print length(), \" \n\" if (length()>254)/e''" in shell       
end if
activate
end tell

问题是,当我 运行 这个 applescript 时,命令开始执行动作但随后卡住了,我只看到

>

我必须按 ctrl+c 退出才能继续使用终端 window。

有谁知道我在终端命令中缺少什么标志?我也愿意使用完全不同的方法来检索和列出突出显示目录的字符数和 255 个字符或更多字符的文件的绝对路径。

我不确定您在这种情况下使用终端能获得什么价值,所以这是我在没有使用终端的情况下添加的解决方案。

on run
    set longPaths to {}
    tell application "Finder" to set theSel to selection
    repeat with aFile in theSel
        set aFile to aFile as string
        set pathLength to count of characters in aFile
        if pathLength > 255 then
            set end of longPaths to aFile
        end if
    end repeat

    if longPaths is not equal to {} then
        -- do something with your list of long paths, write them to a text file or whatever you want
        set pathToYourTextFile to (path to desktop folder as string)&"SampleTextFile.txt"
        set tFile to open for access file (pathToYourTextFile as string) with write permission
        repeat with filePath in longPaths
            write (filePath & return as string) to tFile starting at eof
        end repeat
        close access tFile
    end if
end run

只需将文件夹的路径放入 find 命令,而不是使用 cd command

像这样:

tell application "Finder"
    set theFol to item 1 of (get selection)
    if class of theFol is not folder then return -- not a folder
end tell
set f to quoted form of POSIX path of (theFol as text)

tell application "Terminal"
    do script "find " & f & " -name \* -type f | perl -ne 'print length($_), \" $_\",  if length($_)>255'" -- (>255 characters), because the $_ variable contains a newline
    activate
end tell

AppleScript 中 do script 命令的信息:您的命令卡在终端中,因为您在 perl 命令的末尾放置了两个简单的引号字符。