miner_tom
miner_tom

Reputation: 187

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.

Upvotes: 0

Views: 147

Answers (2)

P....
P....

Reputation: 18371

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 findto search into $(pwd) or $PWD , as these two will yield full path of the current working dir.

Upvotes: 2

John Kugelman
John Kugelman

Reputation: 361625

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

Upvotes: 3

Related Questions