在 Linux CLI 中以相对于当前目录的路径递归列出文件
这类似于 这个问题,但我想包含相对于unix中当前目录的路径。 如果我执行以下操作:
ls -LR | grep .txt
它不包括完整路径。 例如,我有以下目录结构:
test1/file.txt
test2/file1.txt
test2/file2.txt
上面的代码将返回:
file.txt
file1.txt
file2.txt
如何使用标准 Unix 命令让它包含相对于当前目录的路径?
This is similar to this question, but I want to include the path relative to the current directory in unix. If I do the following:
ls -LR | grep .txt
It doesn't include the full paths. For example, I have the following directory structure:
test1/file.txt
test2/file1.txt
test2/file2.txt
The code above will return:
file.txt
file1.txt
file2.txt
How can I get it to include the paths relative to the current directory using standard Unix commands?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
使用 find:
在使用 GNU find 的系统上,像大多数 GNU/Linux 发行版一样,您可以省略 -print。
Use find:
On systems that use GNU find, like most GNU/Linux distributions, you can leave out the -print.
使用
tree
,以及-f
(完整路径)和-i
(无缩进线):然后您可以使用
grep 过滤掉你想要的。
如果没有找到该命令,您可以安装它:
输入以下命令在 RHEL/CentOS 和 Fedora linux 上安装 tree 命令:
如果您使用的是 Debian/Ubuntu、Mint Linux,请在终端中输入以下命令:
Use
tree
, with-f
(full path) and-i
(no indentation lines):You can then use
grep
to filter out the ones you want.If the command is not found, you can install it:
Type following command to install tree command on RHEL/CentOS and Fedora linux:
If you are using Debian/Ubuntu, Mint Linux type following command in your terminal:
尝试
查找
。 您可以在手册页中准确查找它,但它有点像这样:find [start directory] -name [what to find]
所以对于您的示例
find 。 -name "*.txt"
应该给你你想要的。
Try
find
. You can look it up exactly in the man page, but it's sorta like this:find [start directory] -name [what to find]
so for your example
find . -name "*.txt"
should give you what you want.
您可以使用 find 代替:
You could use find instead:
要使用 find 命令获取所需文件的实际完整路径文件名,请将其与 pwd 命令一起使用:
To get the actual full path file names of the desired files using the find command, use it with the pwd command:
就可以了:
ls -R1 $PWD | 当读l时; 在 *:) d=${l%:}; 中执行 case $l "") d=;; *)回显“$d/$l”;; 经济及社会事务委员会; 完成 | grep -i ".txt"
但它通过解析
ls
来“犯罪”,这被 GNU 和 Ghostscript 社区认为是不好的形式。That does the trick:
ls -R1 $PWD | while read l; do case $l in *:) d=${l%:};; "") d=;; *) echo "$d/$l";; esac; done | grep -i ".txt"
But it does that by "sinning" with the parsing of
ls
, though, which is considered bad form by the GNU and Ghostscript communities.“sed”将从所有“查找”结果中删除“your_path”。 您会收到相对于“DIR”路径的信息。
'sed' will erase 'your_path' from all 'find' results. And you recieve relative to 'DIR' path.
这是一个 Perl 脚本:
用法:
Here is a Perl script:
usage:
您可以创建一个 shell 函数,例如在
.zshrc
或.bashrc
中:显然,第一个函数仅适用于单个文件。
You could create a shell function, e.g. in your
.zshrc
or.bashrc
:The first one would work on single files only, obviously.
从根目录“/”开始搜索,在文件系统上找到名为“filename”的文件。 “文件名”
Find the file called "filename" on your filesystem starting the search from the root directory "/". The "filename"
如果您想在输出中保留 ls 附带的详细信息(例如文件大小等),那么这应该可行。
If you want to preserve the details come with ls like file size etc in your output then this should work.
在 fish shell 中,您可以执行此操作以递归方式列出所有 pdf,包括当前目录中的 pdf:
只需删除如果您想要任何类型的文件,请输入“pdf”。
In the fish shell, you can do this to list all pdfs recursively, including the ones in the current directory:
Just remove 'pdf' if you want files of any type.
您可以像这样实现此功能
首先,使用 ls 命令指向目标目录。 稍后使用 find 命令过滤它的结果。
从你的情况来看,听起来像 - 文件名总是以单词开头
文件***.txt
You can implement this functionality like this
Firstly, using the ls command pointed to the targeted directory. Later using find command filter the result from it.
From your case, it sounds like - always the filename starts with a word
file***.txt
在我的例子中,使用树命令
相对路径
绝对路径
| grep '\./.*' | while read file; do echo $file | sed -e "s|^.|$PWD|g" done | grep -v '.*/绝对路径
| grep '\./.*' | while read file; do echo $file done绝对路径
In mycase, with tree command
Relative path
Absolute path
| grep '\./.*' | while read file; do echo $file | sed -e "s|^.|$PWD|g" done | grep -v '.*/Absolute path
| grep '\./.*' | while read file; do echo $file doneAbsolute path