如何从链接其垃圾箱的程序(例如 Homebrew)中找到最旧的使用文件?

问题描述 投票:0回答:1

如何从中找到最旧的使用过的文件?

MBP:~ user$ \ls -la /usr/local/bin/ | egrep -i "Cellar|Cask" | head
lrwxr-xr-x    1 user  admin       37 Sep 13 12:19 2to3 -> ../Cellar/[email protected]/3.11.5/bin/2to3
lrwxr-xr-x    1 user  admin       43 Sep 13 12:26 2to3-3.10 -> ../Cellar/[email protected]/3.10.13/bin/2to3-3.10
lrwxr-xr-x    1 user  admin       42 Sep 13 12:19 2to3-3.11 -> ../Cellar/[email protected]/3.11.5/bin/2to3-3.11
lrwxr-xr-x    1 user  admin       40 Sep 13 12:35 2to3-3.9 -> ../Cellar/[email protected]/3.9.18/bin/2to3-3.9
lrwxr-xr-x    1 user  admin       37 Aug 26 14:25 4channels -> ../Cellar/libraw/0.21.1/bin/4channels
lrwxr-xr-x    1 user  admin       52 Sep 13 12:34 Magick++-config -> ../Cellar/imagemagick/7.1.1-15_1/bin/Magick++-config
lrwxr-xr-x    1 user  admin       54 Sep 13 12:34 MagickCore-config -> ../Cellar/imagemagick/7.1.1-15_1/bin/MagickCore-config
lrwxr-xr-x    1 user  admin       54 Sep 13 12:34 MagickWand-config -> ../Cellar/imagemagick/7.1.1-15_1/bin/MagickWand-config
lrwxr-xr-x    1 user  admin       36 Jun  4  2023 acountry -> ../Cellar/c-ares/1.19.1/bin/acountry
lrwxr-xr-x    1 user  admin       32 Jun  4  2023 adig -> ../Cellar/c-ares/1.19.1/bin/adig
MBP:~ user$

如果有人在 Mac 上使用 Homebrew,我会看看我是否可以或应该清理任何东西。我安装了大量的应用程序,而且

brew update
需要很长时间。我需要找到我停止使用的应用程序。

python bash perl homebrew
1个回答
0
投票

我发现我可以获得 bash 访问时间,并制作了一个快速的 Perl 脚本来解释它们,向我展示了最旧的使用过的自制程序箱。

find /usr/local/bin -type l -exec sh -c 'readlink -f {}' \; | egrep -i "Cellar|Cask" > /tmp/brew-bins-targets
for i in $( cat /tmp/brew-bins-targets ); do echo -e "\n$i"; stat $i | grep Access; done > /tmp/brew-bins-access-times
cat /tmp/brew-bins-access-times | homebrew-get-dates.pl

详情

获取 bash 访问时间:

find /usr/local/bin -type l -exec sh -c 'readlink -f {}' \; | egrep -i "Cellar|Cask" > /tmp/brew-bins-targets

Homebrew 将所有 bin 文件链接到 /usr/local/Cellar/... 或 /usr/local/Cask/...

  • -type l
    显示链接
  • readlink...
    获取链接目标
  • egrep...
    仅显示 Homebrew 应用程序
for i in $( cat /tmp/brew-bins-targets ); do echo -e "\n$i"; stat $i | grep Access; done > /tmp/brew-bins-access-times

stat
列出了访问时间等内容。如果我执行了该应用程序,我认为该访问时间会更新。

解释它们的 Perl 脚本:

#!/usr/bin/perl
# homebrew-get-dates.pl

use strict;
use warnings;

my %data_res;
my $count = 0;
my $filename;

# Extra line for readability after PS1 prompt
print "\n";

while (<>) {
    chomp;

    # Process every 3 lines

    my $remain = $count % 3;     # which of the 3 lines is this

    if ($remain == 1) {        # filename line
        $filename = $_;
    } elsif ($remain == 2) {   # access time line
        my $access_line = $_;

        # Create a readable and sortable formatted date
        my ($label, $day, $month, $date, $time, $year) = split(" ", $access_line);
        my $formatted_date = "${year}_${month}_${date}_$time";

        # Store in hash, handling multiple entries for the same date
        if (exists $data_res{$formatted_date}) {
            push @{ $data_res{$formatted_date} }, $filename;  # Add filename to existing array
        } else {
            $data_res{$formatted_date} = [$filename];  # Create a new array for the key
        }
    };

    $count++;
}

# Sort dates, oldest last
my @sorted_dates = sort { $b cmp $a } keys %data_res;

# Print sorted output
foreach my $date (@sorted_dates) {
    my @p_files = @{ $data_res{$date} };
    foreach my $file ( @p_files ){
        printf "%s\t%s\n", $date, $file;
    }
}

# Extra line for readability before PS1 prompt
print "\n";

最后显示最旧使用的自制啤酒箱:

MBP:~ user$ cat /tmp/brew-bins-access-times | homebrew-get-dates.pl | tail
2022_Sep_17_16:55:16    /usr/local/Cellar/jemalloc/5.3.0/bin/jemalloc-config
2022_Sep_17_16:55:16    /usr/local/Cellar/jemalloc/5.3.0/bin/jeprof
2022_Sep_17_16:55:16    /usr/local/Cellar/jemalloc/5.3.0/bin/jemalloc.sh
2022_Sep_17_16:54:43    /usr/local/Cellar/lz4/1.9.4/bin/lz4
2022_Sep_17_16:54:43    /usr/local/Cellar/lz4/1.9.4/bin/lz4
2022_Sep_17_16:54:43    /usr/local/Cellar/lz4/1.9.4/bin/lz4
2022_Sep_17_16:54:43    /usr/local/Cellar/lz4/1.9.4/bin/lz4
2021_Oct_2_20:18:25 /usr/local/Cellar/npth/1.6/bin/npth-config
2021_Oct_17_20:47:13    /usr/local/Caskroom/vlc/3.0.16/vlc.wrapper.sh

MBP:~ user$
© www.soinside.com 2019 - 2024. All rights reserved.