在Linux CLI中以递归方式列出文件,其中包含相对于当前目录的路径

StackOverflow https://stackoverflow.com/questions/245698

  •  05-07-2019
  •  | 
  •  

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

ls -LR | grep .txt

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

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

上面的代码将返回:

file.txt
file1.txt
file2.txt

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

有帮助吗?

解决方案

使用find:

find . -name \*.txt -print

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

其他提示

使用 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

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

找到[开始目录] -name [找到什么]

所以你的例子

找到。 -name" * .txt"

应该给你你想要的东西。

您可以使用find代替:

find . -name '*.txt'

这就是诀窍:

ls -R1 $ PWD |读l;做案例$ l in * :) d = $ {l%:} ;; “")d = ;; *)echo“$ d / $ l”;; ESAC;完成| grep -i" .txt"

但它是通过“犯罪”而做到的。但是,解析 ls ,这被GNU和Ghostscript社区视为不良形式。

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

'sed'将从所有'find'结果中删除'your_path'。并且你接受了相对于'DIR'的道路。

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

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

这是一个Perl脚本:

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

    foreach (@lines)
    {
        next if (

这是一个Perl脚本:

ls -LR | perl format_ls-LR.pl

用法:

<*> =~ /^\s+/); if (

这是一个Perl脚本:

<*>

用法:

<*> =~ /(^\w+(\/\w*)*):/) { $tmppath = $1 if defined $1; next; } print "$tmppath/

这是一个Perl脚本:

<*>

用法:

<*>"; } } sub main() { my @lines = (); while (<>) { push (@lines,

这是一个Perl脚本:

<*>

用法:

<*>); } format_lines(\@lines); } main();

用法:

<*>

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

filepath() {
    echo $PWD/$1
}

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

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

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

find / -name "filename" 

如果你想在输出中保留详细信息,例如文件大小等,那么这应该可行。

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

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

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

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

$ ls **pdf

如果您想要任何类型的文件,请删除'pdf'。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top