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

时间:2016-05-06 17:00:46

标签: terminal find applescript posix

我有两个问题:

第1部分(终端) - 我在Mac环境中工作,该环境存档到基于Windows的系统,因此我需要检测255的路径长度,以便我可以在归档之前更改这些文件。我在StackOverflow上看到了很多关于文件名长度的解决方案,但我需要绝对路径长度。我碰巧写了这个剧本并且它已经结束了:

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

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

第2部分(Applescript) - 一旦第一部分在终端I中正常工作,我希望稍微自动化一下这个过程。我最好创建一个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($1), \" $1\\n\" if (length($1)>254)/e''" in shell       
end if
activate
end tell

问题在于,当我运行这个AppleScript时,命令开始通过动作然后卡住了,我所看到的只是

>

我必须按ctrl + c out继续使用终端窗口。

有没有人知道我在终端命令中丢失了什么标志?我还完全采用不同的方法来检索和列出突出显示的目录中255个字符或更多的文件的字符数和绝对路径。

2 个答案:

答案 0 :(得分:0)

我不确定在这种情况下您使用终端的价值是多少,所以这是我在没有它的情况下添加的解决方案。

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

答案 1 :(得分:0)

只需将文件夹的路径放入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命令的末尾添加了两个简单的引号字符。