简体   繁体   中英

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. 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".

I know that it should be a combination of the "ls" command and the "pwd" command combined with a "/" in between.

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. *.jpg expands to bare file names; "$PWD"/*.jpg expands to those same file names with the current directory prepended to each. ls then dutifully spits back the same paths you gave it.

ls "$PWD"/*.jpg > filename.txt

Alternatively, you could use the same trick with find if you want to search recursively through subdirectories:

find "$PWD" -name '*.jpg' > filename.txt

You can use readlink or 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM