【问题标题】:List files recursively in Linux CLI with path relative to the current directory在 Linux CLI 中以相对于当前目录的路径递归列出文件
【发布时间】:2010-09-19 17:31:08
【问题描述】:

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

ls -LR | grep .txt

它不包括完整路径。比如我的目录结构如下:

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

上面的代码会返回:

file.txt
file1.txt
file2.txt

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

【问题讨论】:

  • 这只是说明 ls 缺少这个功能。
  • 很遗憾所有这些解决方案都需要findtree。我正在 ssh'ing 到一个我似乎只有 ls 的 android 设备,并且没有这些其他工具:/

标签: linux unix recursion ls


【解决方案1】:

使用查找:

find . -name \*.txt -print

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

【讨论】:

  • ...就此而言,您可以省略“。”
  • 对于绝对路径,使用find $(pwd) -name \*.txt
  • 如果模式有部分目录名应该使用-path "*docs/*.txt"而不是-name-wholename-path 相同
  • 使用-type f只返回文件而不返回目录、符号链接等
  • -name "*.txt"NOT 匹配 file.TXT — 始终使用 -iname "*.txt" 而不是不区分大小写。
【解决方案2】:

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

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

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


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

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

# yum install tree -y

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

$ sudo apt-get install tree -y

【讨论】:

  • 任何设置只列出文件,排除文件夹?
【解决方案3】:

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

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

你的例子

find . -name "*.txt"

应该给你你想要的。

【讨论】:

    【解决方案4】:

    你可以用 find 代替:

    find . -name '*.txt'
    

    【讨论】:

      【解决方案5】:

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

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

      【讨论】:

        【解决方案6】:

        这就是诀窍:

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

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

        【讨论】:

        • @CAFEBABE:解析ls的结果被认为是禁忌,因为它通常会导致错误。有问题的句子试图指出这一点。它还有一个笑话,我已经删除了。
        【解决方案7】:
        DIR=your_path
        find $DIR | sed 's:""$DIR""::'
        

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

        【讨论】:

        • 这正是我需要的,但它仍然为我吐出了完整的路径?
        • 不要使用撇号 - find /tmp | sed s:"/tmp":: |更多
        【解决方案8】:

        这是一个 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
        

        【讨论】:

        • 这是一个糟糕的 Perl 程序。它可能比实际需要的时间长 2 到 3 倍,也很复杂。
        【解决方案9】:

        你可以创建一个 shell 函数,例如在你的.zshrc.bashrc

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

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

        【讨论】:

          【解决方案10】:

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

          find / -name "filename" 
          

          【讨论】:

            【解决方案11】:

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

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

            【讨论】:

              【解决方案12】:

              fish shell 中,您可以这样做以递归方式列出所有 pdf,包括当前目录中的那些:

              $ ls **pdf
              

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

              【讨论】:

                【解决方案13】:

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

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

                【讨论】:

                  【解决方案14】:

                  在我的情况下,使用树命令

                  相对路径

                  tree -ifF ./dir | grep -v '^./dir$' | grep -v '.*/$' | grep '\./.*' | while read file; do
                    echo $file
                  done
                  

                  绝对路径

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

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 2011-03-21
                    • 2014-04-14
                    • 2014-05-24
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2017-09-30
                    相关资源
                    最近更新 更多