在 Linux CLI 中使用相对于当前目录的路径递归列出文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/245698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:39:59  来源:igfitidea点击:

List files recursively in Linux CLI with path relative to the current directory

linuxunixrecursionls

提问by Darryl Hein

This is similar to this question, but I want to include the path relative to the current directory in unix. If I do the following:

这类似于这个问题,但我想包括相对于 unix 中当前目录的路径。如果我执行以下操作:

ls -LR | grep .txt

It doesn't include the full paths. For example, I have the following directory structure:

它不包括完整路径。例如,我有以下目录结构:

test1/file.txt
test2/file1.txt
test2/file2.txt

The code above will return:

上面的代码将返回:

file.txt
file1.txt
file2.txt

How can I get it to include the paths relative to the current directory using standard Unix commands?

如何使用标准 Unix 命令让它包含相对于当前目录的路径?

采纳答案by Andru Luvisi

Use find:

使用查找:

find . -name \*.txt -print

On systems that use GNU find, like most GNU/Linux distributions, you can leave out the -print.

在使用 GNU find 的系统上,像大多数 GNU/Linux 发行版一样,您可以省略 -print。

回答by Jonathan Adelson

Try find. You can look it up exactly in the man page, but it's sorta like this:

试试find。您可以在手册页中准确查找它,但它有点像这样:

find [start directory] -name [what to find]

find [start directory] -name [what to find]

so for your example

所以对于你的例子

find . -name "*.txt"

find . -name "*.txt"

should give you what you want.

应该给你你想要的。

回答by Sherm Pendley

You could use find instead:

您可以使用 find 代替:

find . -name '*.txt'

回答by h-dima

DIR=your_path
find $DIR | sed 's:""$DIR""::'

'sed' will erase 'your_path' from all 'find' results. And you recieve relative to 'DIR' path.

'sed' 将从所有 'find' 结果中删除 'your_path'。并且您收到相对于“DIR”路径。

回答by Eric Keller

Here is a Perl script:

这是一个 Perl 脚本:

sub format_lines($)
{
    my $refonlines = shift;
    my @lines = @{$refonlines};
    my $tmppath = "-";

    foreach (@lines)
    {
        next if ($_ =~ /^\s+/);
        if ($_ =~ /(^\w+(\/\w*)*):/)
        {
            $tmppath =  if defined ;    
            next;
        }
        print "$tmppath/$_";
    }
}

sub main()
{
        my @lines = ();

    while (<>) 
    {
        push (@lines, $_);
    }
    format_lines(\@lines);
}

main();

usage:

用法:

ls -LR | perl format_ls-LR.pl

回答by Stephen Irons

Use tree, with -f(full path) and -i(no indentation lines):

使用tree, with -f(完整路径)和-i(无缩进线):

tree -if --noreport .
tree -if --noreport directory/

You can then use grepto filter out the ones you want.

然后您可以使用grep过滤掉您想要的那些。



If the command is not found, you can install it:

如果找不到命令,可以安装:

Type following command to install tree command on RHEL/CentOS and Fedora linux:

键入以下命令在 RHEL/CentOS 和 Fedora linux 上安装 tree 命令:

# yum install tree -y

If you are using Debian/Ubuntu, Mint Linux type following command in your terminal:

如果您使用的是 Debian/Ubuntu,Mint Linux 在您的终端中输入以下命令:

$ sudo apt-get install tree -y

回答by rxw

You could create a shell function, e.g. in your .zshrcor .bashrc:

您可以创建一个 shell 函数,例如在您的.zshrcor 中.bashrc

filepath() {
    echo $PWD/
}

filepath2() {
    for i in $@; do
        echo $PWD/$i
    done
}

The first one would work on single files only, obviously.

显然,第一个只适用于单个文件。

回答by ZaSter

To get the actual full path file names of the desired files using the find command, use it with the pwd command:

要使用 find 命令获取所需文件的实际完整路径文件名,请将其与 pwd 命令一起使用:

find $(pwd) -name \*.txt -print

回答by user2101432

Find the file called "filename" on your filesystem starting the search from the root directory "/". The "filename"

从根目录“/”开始搜索,在文件系统上找到名为“filename”的文件。“文件名”

find / -name "filename" 

回答by rajeshk

If you want to preserve the details come with ls like file size etc in your output then this should work.

如果你想在你的输出中保留 ls 的细节,比如文件大小等,那么这应该可以工作。

sed "s|<OLDPATH>|<NEWPATH>|g" input_file > output_file