[英]Simple bash Question: combine results of 2 bash commands to one text file
I wish to put the results of "pwd" and "ls" into a text file. 我希望将“ pwd”和“ ls”的结果放入文本文件中。 I have a directory with .jpg files and it and as opposed to "ls > filename.txt" I want the path of the files included in filename.txt, so that the result would be a list of lines, in a text file, of the form "/home/path-to-files/file.jpg". 我有一个包含.jpg文件的目录,而不是“ ls> filename.txt”,我希望filename.txt中包含文件的路径,这样结果将是文本文件中的行列表,格式为“ /home/path-to-files/file.jpg”。
I know that it should be a combination of the "ls" command and the "pwd" command combined with a "/" in between. 我知道它应该是“ ls”命令和“ pwd”命令的组合,中间是“ /”。
This should be pretty easy to do but I am having a bit if trouble doing it. 这应该很容易做到,但是在执行时遇到麻烦。
ls
will print full paths if you pass it full paths. 如果您传递完整路径, ls
将打印完整路径。 *.jpg
expands to bare file names; *.jpg
扩展为裸文件名; "$PWD"/*.jpg
expands to those same file names with the current directory prepended to each. "$PWD"/*.jpg
扩展为那些相同的文件名,并以当前目录为前缀。 ls
then dutifully spits back the same paths you gave it. 然后, ls
忠实地返回您给它的相同路径。
ls "$PWD"/*.jpg > filename.txt
Alternatively, you could use the same trick with find
if you want to search recursively through subdirectories: 另外,如果要递归搜索子目录,可以对find
使用相同的技巧:
find "$PWD" -name '*.jpg' > filename.txt
You can use readlink
or find
: 您可以使用readlink
或find
:
readlink -f *.jpg > filename.txt
OR 要么
find $(pwd) -name "*.jpg" -type f > filename.txt
You can use find
command to get the abolute path of the file by telling find
to search into $(pwd)
or $PWD
, as these two will yield full path of the current working dir. 您可以使用find
命令告诉find
搜索$(pwd)
或$PWD
来获取文件的路径,因为这两个将产生当前工作目录的完整路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.