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

发布于 2024-07-07 21:22:18 字数 448 浏览 13 评论 0原文

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

ls -LR | grep .txt

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

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

上面的代码将返回:

file.txt
file1.txt
file2.txt

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

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:

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?

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

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

发布评论

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

评论(14

静赏你的温柔 2024-07-14 21:22:18

使用 find:

find . -name \*.txt -print

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

Use find:

find . -name \*.txt -print

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

岁月如刀 2024-07-14 21:22:18

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

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

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


如果没有找到该命令,您可以安装它:

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

# yum install tree -y

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

$ sudo apt-get install tree -y

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

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

You can then use grep to filter out the ones you want.


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

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

# yum install tree -y

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

$ sudo apt-get install tree -y
假装爱人 2024-07-14 21:22:18

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

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

所以对于您的示例

find 。 -name "*.txt"

应该给你你想要的。

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

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

so for your example

find . -name "*.txt"

should give you what you want.

北城挽邺 2024-07-14 21:22:18

您可以使用 find 代替:

find . -name '*.txt'

You could use find instead:

find . -name '*.txt'
风筝在阴天搁浅。 2024-07-14 21:22:18

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

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

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

find $(pwd) -name \*.txt -print
予囚 2024-07-14 21:22:18

就可以了:

ls -R1 $PWD | 当读l时; 在 *:) d=${l%:}; 中执行 case $l "") d=;; *)回显“$d/$l”;; 经济及社会事务委员会; 完成 | grep -i ".txt"

但它通过解析 ls 来“犯罪”,这被 GNU 和 Ghostscript 社区认为是不好的形式。

That does the trick:

ls -R1 $PWD | while read l; do case $l in *:) d=${l%:};; "") d=;; *) echo "$d/$l";; esac; done | grep -i ".txt"

But it does that by "sinning" with the parsing of ls, though, which is considered bad form by the GNU and Ghostscript communities.

浅浅 2024-07-14 21:22:18
DIR=your_path
find $DIR | sed 's:""$DIR""::'

“sed”将从所有“查找”结果中删除“your_path”。 您会收到相对于“DIR”路径的信息。

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

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

少年亿悲伤 2024-07-14 21:22:18

这是一个 Perl 脚本:

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

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

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

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

main();

用法:

ls -LR | perl format_ls-LR.pl

Here is a Perl script:

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

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

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

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

main();

usage:

ls -LR | perl format_ls-LR.pl
养猫人 2024-07-14 21:22:18

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

filepath() {
    echo $PWD/$1
}

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

显然,第一个函数仅适用于单个文件。

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

filepath() {
    echo $PWD/$1
}

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

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

情仇皆在手 2024-07-14 21:22:18

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

find / -name "filename" 

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

find / -name "filename" 
在梵高的星空下 2024-07-14 21:22:18

如果您想在输出中保留 ls 附带的详细信息(例如文件大小等),那么这应该可行。

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

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

sed "s|<OLDPATH>|<NEWPATH>|g" input_file > output_file
梦屿孤独相伴 2024-07-14 21:22:18

fish shell 中,您可以执行此操作以递归方式列出所有 pdf,包括当前目录中的 pdf:

$ ls **pdf

只需删除如果您想要任何类型的文件,请输入“pdf”。

In the fish shell, you can do this to list all pdfs recursively, including the ones in the current directory:

$ ls **pdf

Just remove 'pdf' if you want files of any type.

月下伊人醉 2024-07-14 21:22:18

您可以像这样实现此功能
首先,使用 ls 命令指向目标目录。 稍后使用 find 命令过滤它的结果。
从你的情况来看,听起来像 - 文件名总是以单词开头
文件***.txt

ls /some/path/here | find . -name 'file*.txt'   (* represents some wild card search)

You can implement this functionality like this
Firstly, using the ls command pointed to the targeted directory. Later using find command filter the result from it.
From your case, it sounds like - always the filename starts with a word
file***.txt

ls /some/path/here | find . -name 'file*.txt'   (* represents some wild card search)
傾城如夢未必闌珊 2024-07-14 21:22:18

在我的例子中,使用树命令

相对路径

tree -ifF ./dir | grep -v '^./dir

绝对路径

tree -ifF ./dir | grep -v '^./dir
 | grep -v '.*/

绝对路径


 | grep '\./.*' | while read file; do
  echo $file
done

绝对路径


 | grep -v '.*/
 | grep -v '.*/

绝对路径


 | grep '\./.*' | while read file; do
  echo $file
done

绝对路径

| grep '\./.*' | while read file; do echo $file | sed -e "s|^.|$PWD|g" done | grep -v '.*/

绝对路径

| grep '\./.*' | while read file; do echo $file done

绝对路径

In mycase, with tree command

Relative path

tree -ifF ./dir | grep -v '^./dir

Absolute path

tree -ifF ./dir | grep -v '^./dir
 | grep -v '.*/

Absolute path


 | grep '\./.*' | while read file; do
  echo $file
done

Absolute path


 | grep -v '.*/
 | grep -v '.*/

Absolute path


 | grep '\./.*' | while read file; do
  echo $file
done

Absolute path

| grep '\./.*' | while read file; do echo $file | sed -e "s|^.|$PWD|g" done | grep -v '.*/

Absolute path

| grep '\./.*' | while read file; do echo $file done

Absolute path

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文