在Linux CLI中以相对于当前目录的pathrecursion列出文件
这和这个问题很相似,但是我想在unix中包含相对于当前目录的path。 如果我做到以下几点:
ls -LR | grep .txt 它不包括完整的path。 例如,我有以下目录结构:
 test1/file.txt test2/file1.txt test2/file2.txt 
上面的代码将返回:
 file.txt file1.txt file2.txt 
我怎样才能使用标准的Unix命令来包含相对于当前目录的path?
使用find:
 find . -name \*.txt -print 
在使用GNU find的系统上,像大多数GNU / Linux发行版一样,可以省略-print。
 使用tree ,使用-f (完整path)和-i (没有缩进行): 
 tree -if --noreport . tree -if --noreport directory/ 
 然后你可以使用grep来过滤掉你想要的。 
如果找不到命令,则可以安装它:
键入以下命令在RHEL / CentOS和Fedora linux上安装树命令:
 # yum install tree -y 
如果你正在使用Debian / Ubuntu,Mint Linux在你的terminalinput以下命令:
 $ sudo apt-get install tree -y 
 试试find 。 您可以在手册页中查看它,但它是这样的: 
 find [start directory] -name [what to find] 
所以为你的例子
 find . -name "*.txt" 
应该给你你想要的。
你可以用find来代替:
 find . -name '*.txt' 
这就是诀窍:
 ls -R1 $PWD | while read l; do case $l in *:) d=${l%:};; "") d=;; *) echo "$d/$l";; esac; done | grep -i ".txt" 
 但是这样做是通过对ls的parsing来“犯罪”,而GNU和Ghostscript社区认为这是不好的forms。 
 DIR=your_path find $DIR | sed 's:""$DIR""::' 
'sed'会从所有的“查找”结果中清除“your_path”。 而你收到相对'DIR'的path。
要使用find命令获取所需文件的实际完整path文件名,请使用pwd命令:
 find $(pwd) -name \*.txt -print 
这是一个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 
 你可以创build一个shell函数,例如在.zshrc或.bashrc : 
 filepath() { echo $PWD/$1 } filepath2() { for i in $@; do echo $PWD/$i done } 
第一个只能在单个文件上工作,显然。
在文件系统上find名为“filename”的文件,从根目录“/”开始search。 “文件名”
 find / -name "filename" 
如果你想保留在你的输出文件大小等ls的细节,那么这应该工作。
 sed "s|<OLDPATH>|<NEWPATH>|g" input_file > output_file 
 你可以像这样实现这个function 
 首先,使用ls命令指向目标目录。 之后使用find命令过滤它的结果。 从你的情况,这听起来像 – 总是文件名以字file***.txt开头 
 ls /some/path/here | find . -name 'file*.txt' (* represents some wild card search)