这与这个问题类似,但是我想包含相对于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命令让它包含相对于当前目录的路径?
当前回答
在我的情况下,使用树命令
相对路径
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
其他回答
DIR=your_path
find $DIR | sed 's:""$DIR""::'
'sed'将从所有'find'结果中删除'your_path'。你接收相对于DIR路径的。
你可以用find代替:
find . -name '*.txt'
下面是一个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
从根目录“/”开始搜索,在文件系统中找到名为“filename”的文件。“文件名”
find / -name "filename"
使用找到:
find . -name \*.txt -print
在使用GNU find的系统上,比如大多数GNU/Linux发行版,您可以省略-print。