这与这个问题类似,但我想在Unix中包括相对于当前目录的路径。如果我执行以下操作:
ls -LR | grep .txt
它不包括完整路径。例如,我有以下目录结构:
test1/file.txt
test2/file1.txt
test2/file2.txt
上述代码将返回:
file.txt
file1.txt
file2.txt
如何使用标准Unix命令,使它包含相对于当前目录的路径?
这与这个问题类似,但我想在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。
find $(pwd) -name \*.txt
。 (说明:该命令是寻找当前目录下所有扩展名为txt的文件,$(pwd)表示当前所在目录的绝对路径。) - Zags-path "*docs/*.txt"
而不是 -name
。-wholename
与-path
相同。 - dashesy-name "*.txt"
不会 匹配 file.TXT
—— 请始终使用 -iname "*.txt"
,它是不区分大小写的。 - ccpizzafind -name "*.txt"
至少在Ubuntu系统上有效,我猜这也意味着它在Debian系统上也有效。 - Olof Bjarnason使用 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
试试find
命令。你可以在man手册中精确查找,但它有点像这样:
find [起始目录] -name [要查找的内容]
所以对于你的示例:
find . -name "*.txt"
应该会给你想要的结果。
find . -name '*.txt'
find $(pwd) -name \*.txt -print
ls -R1 $PWD | while read l; do case $l in *:) d=${l%:};; "") d=;; *) echo "$d/$l";; esac; done | grep -i ".txt"
但是,这种方法“犯规”了GNU和Ghostscript社区不赞成的解析方式。DIR=your_path
find $DIR | sed 's:""$DIR""::'
'sed' 将从所有 'find' 的结果中删除 'your_path'。您将收到相对于 'DIR' 路径的结果。
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
find / -name "filename"