如何确定一个文件是否被另一个文件使用/调用?

发布于 2024-11-09 11:08:37 字数 359 浏览 0 评论 0 原文

我目前正在大学二年级,因此我的编程技能和知识并不像我希望的那么强。我暑假期间正在一家网络开发公司实习,分配给我的第一项任务让我完全不知所措。这就是我来这里寻求帮助的原因。

主文件夹中有许多子文件夹,每个子文件夹中有许多 .js、.cs 和 .php 文件 - 大约 1000 个文件。但大约有 300 个没有被使用。我需要打开每个子文件夹,看看这些文件是否被任何其他文件使用/调用。如果不是,我需要将未使用文件的位置存储在文本文件中。

我做了一些研究,发现命令 grep -r filename * 就是这样做的,但是在命令行上我无法弄清楚如何循环遍历文件夹并根据内容更改文件名文件夹内。我的工作站是 Windows 系统,安装了 Cygwin。

I am currently in my second year of college, therefore my programming skills and knowledge are not as strong as I like them to be. I am doing an internship for a web development company during my summer break and I am completely stomped on the first task that was assigned to me. That's why I'm here asking for some assistance.

In a main folder there are many sub-folders and within each sub-folder there are many .js .cs and .php files - about 1000 files. But about 300 are not being used. I need to open up each of the sub-folders and see if any of these files are used/called by any other files. If they are not, I need to store the location of the unused file in a text file.

I did some research and found out that the command grep -r filename * does just that, but on the command-line I cannot figure out how to loop through the folders and change the filename based on the content inside the folders. The workstation I have is in Windows with Cygwin installed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏日落 2024-11-16 11:08:37
echo file,count >results.csv
for f in $(find . -name *.js -o -name *.cs -o -name *.php)
do
    echo $f,$(grep -cr $(basename $f) *) >> results.csv
done

这将为您提供一个像这样的 csv 文件,其中包含每个文件被引用的次数。

file,count
file1,3
file2,1
file3,0

编辑以在 grep 之前删除文件路径

echo file,count >results.csv
for f in $(find . -name *.js -o -name *.cs -o -name *.php)
do
    echo $f,$(grep -cr $(basename $f) *) >> results.csv
done

this will give you a csv file like this with the number of times each file is referenced.

file,count
file1,3
file2,1
file3,0

edited to remove file path before grepping

一场春暖 2024-11-16 11:08:37

这不需要双循环吗? (大 O2)。您必须在每个文件中搜索该文件的每个实例。

我会使用 Perl 而不是 Awk 或 BASH(尽管可以在 BASH 中做到)。

#! /usr/bin/env perl

use warnings;
use strict;
use feature qw(say);

use File::Find;     #Not crazy about File::Find, but it's a standard module
use File::Basename;

my %fileHash;
my @dirs = qw(foo bar barfu fufu barbar);   #List of the directories you're searching

#Finds the name of all the files. Include ALL files and not just .php, etc.

find(\&wanted, @dirs);

sub wanted {
    next if (-d $File::Find::name); #Skip directories
    $fileHash{$File::Find::name} = 0;       #Number of times file is referenced
}

# Outer Loop: Foreach file you have to parse

foreach my $fileName (keys %fileHash) {

    # We don't have to grep anything except those below.
    (my $suffix = $fileName) =~ s/.*\.//;
    next unless ($suffix eq ".js" or $suffix eq ".cs" or $suffix eq ".php");

    #Slurp up file in an array. That way, we can use the grep command
    open (FILE, $fileName) or die qq(Can't open "$fileName" for reading\n);
    my @lines = <FILE>;
    close FILE;

    # Now, look for each and every file you've got in that directory tree
    # in this particular file. This is an inner loop

    foreach my $fileToFind (keys %fileHash) {
        my $basename = basename($fileToFind);

        # If any lines in the file contain the file name, increment the hash.
        if (grep /$basename/, @lines) {
            $fileHash{$fileToFind} += 1;
        }   
    }   
}   


#Now just print out those files who never got incremented (i.e. never referenced)
foreach my $fileName (keys %FileHash) {
    next if ($fileHash{$fileName} != 0);
    say "File: $fileHash{$fileName}"
}   

我采取的捷径是仅查找文件的基本名称而不是全名。理论上,我应该从根目录查找它的全名,以及它与文件本身的关系的名称。不过,我现在懒得这么做。最有可能的是,您不必担心这一点。

Doesn't this require a double loop? (Big O2). You have to search each file for every instance of the file in it.

I'd use Perl instead of Awk or BASH (although it is possible to do in BASH).

#! /usr/bin/env perl

use warnings;
use strict;
use feature qw(say);

use File::Find;     #Not crazy about File::Find, but it's a standard module
use File::Basename;

my %fileHash;
my @dirs = qw(foo bar barfu fufu barbar);   #List of the directories you're searching

#Finds the name of all the files. Include ALL files and not just .php, etc.

find(\&wanted, @dirs);

sub wanted {
    next if (-d $File::Find::name); #Skip directories
    $fileHash{$File::Find::name} = 0;       #Number of times file is referenced
}

# Outer Loop: Foreach file you have to parse

foreach my $fileName (keys %fileHash) {

    # We don't have to grep anything except those below.
    (my $suffix = $fileName) =~ s/.*\.//;
    next unless ($suffix eq ".js" or $suffix eq ".cs" or $suffix eq ".php");

    #Slurp up file in an array. That way, we can use the grep command
    open (FILE, $fileName) or die qq(Can't open "$fileName" for reading\n);
    my @lines = <FILE>;
    close FILE;

    # Now, look for each and every file you've got in that directory tree
    # in this particular file. This is an inner loop

    foreach my $fileToFind (keys %fileHash) {
        my $basename = basename($fileToFind);

        # If any lines in the file contain the file name, increment the hash.
        if (grep /$basename/, @lines) {
            $fileHash{$fileToFind} += 1;
        }   
    }   
}   


#Now just print out those files who never got incremented (i.e. never referenced)
foreach my $fileName (keys %FileHash) {
    next if ($fileHash{$fileName} != 0);
    say "File: $fileHash{$fileName}"
}   

I'm taking a shortcut of looking just for the file's basename and not the full name. In theory, I should be looking for both its full name from the root, and its name in relationship to the file itself. However, I'm too lazy to do that right now. Most likely, you don't have to worry about that.

水晶透心 2024-11-16 11:08:37

唷,棘手。至少如果你必须考虑“被使用”这一点。

对于 .cs,您可以使用导入语句,但这些语句不会轻易让您断定文件是否正在使用。导入可能在包级别上工作,除非我弄错了(更像是一个java人......)。

我认为 JavaScript 和 php 文件的情况会变得更糟。

也许您应该问,为什么该报告首先有价值?

phew, tricky. At least if you have to take into consideration the 'being used' bit.

In the case of .cs, you can have import statements that won't easily allow you to conclude whether a file is in use. The import might work on a package-level, unless I'm mistaken (being more of a java guy...).

And I assume it gets worse for JavaScript and php files.

Maybe you should ask, why that report is valuable in the first place?

路弥 2024-11-16 11:08:37

这只是一个草稿,你需要研究所有命令并做你自己的逻辑......

for file in $(find -type f -name \*.extension); do
    grep -Rl $file /in/path
done > /tmp/myfiles

This is only a draft, you need research about all commands and do your own logic...

for file in $(find -type f -name \*.extension); do
    grep -Rl $file /in/path
done > /tmp/myfiles
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文