0

I want to list all the files in a folder and their respective absolute paths line by line as below,

filename1  /home/.../file1  
filename2 /home/.../file2  
filename3 /home/.../file3  

I tried with find $(pwd) -type f, but it only gives the paths.

  • 1
    Your example seems to have conflicting filenames. e.g. filename1 and file1. What am I miss-understanding? – ctrl-alt-delor Apr 13 at 9:38
  • what kind of absolute path are those? why /home/../file1 and not /file1 or /home/some_user/../../file1? also, are filename1, filename2, etc symlinks? – mosvy Apr 13 at 9:57
  • (1) The way to show that you’re leaving out information is with three dots: ....  Two dots (..) means something else in a pathname, so, please, don’t use .. if you mean ....  But it would be better not to omit information at all, but just show what you mean, e.g., /home/user347009/file1. (2) And either make the filenames consistent or explain why they aren’t.  (For example, do you mean filename1   /home/user347009/dir1?) – G-Man Apr 13 at 16:07
  • yes.I just used an example and the dots were put to say that there is a middle part.what i want is as you mentioned, filename1   /home/user347009/dir1/filename1 – HirSK Apr 14 at 10:32
2

Using GNU find:

find "$PWD" -type f -printf '%f %h/%f\n'
  • %f File's name with any leading directories removed (only the last element).
  • %h Leading directories of file's name (all but the last element). If the file name contains no slashes (since it is in the current directory) the %h specifier expands to `.'.
  • 1
    Or %p instead of %h/%f – Stéphane Chazelas Apr 13 at 19:05
  • @StéphaneChazelas Exactly! I missed it when I was looking at the man page. – Freddy Apr 13 at 19:11
3

With standard find:

find "$PWD" -type f -exec sh -c '
    for pathname do
        printf "%s\t%s\n" "${pathname##*/}" "$pathname"
    done' sh {} +

This would print a tab-delimited list of all files in or below the current directory. The first column would contain the filename of the file, and the second column would contain the pathname of the file relative to the absolute path of the current directory (the $PWD in the search path).

This is done by feeding batches of found pathnames to a short in-line script which simply outputs the names in the preferred format. The parameter substitution ${pathname##*/} expands to the value of $pathname with everything up to the last / removed.

Note that filenames containing newlines would be presented on several lines, and that it would be difficult to distinguish filenames containing tab-characters properly. Assuming that the result would be for viewing rather than parsing, that might be okay.

0

The following bash oneliner will produce the output you want

for f in $(find $(pwd) -type f); do echo "$(basename $f) $(realpath $(dirname $f))"; done

Explanation:
for _ in _ ; do _ ; done iterates over what your command found
basename strips all but the last part from a path (aka. the filename)
dirname does the opposite
realpath returns the actual path (so you don't get . for file in cwd)

If you use this frequently, I suggest aliasing the oneliner to a shorter command.

0

Use this cmd:

find $PWD -type f -printf '%f %h/%f/n'

or

Looking for some files then go with this:

find $PWD -type f -iname "filenames" -printf '%f %h/%f/n'
  • Thank you very much.I used your first code. I think there should be a simple modification to as '%f %h/%f\n'. – HirSK Apr 14 at 10:38
  • how to display the filename without its extension? – HirSK Apr 14 at 11:58
-1

You can use the following cmd for searching the files with their absolute paths. E.g. I am trying to search .sh files using cmd as follows:

find . -type f -iname "*.sh"
  • 1
    This would not output two column with the filename in the first and the pathname in the second. It's also unclear why you restrict the search to files with a particular filename suffix when this was not part of the question. – Kusalananda Apr 13 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.