Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need a file (preferably a .list file) which contains the absolute path of every file in a directory.

Example dir1: file1.txt file2.txt file3.txt

listOfFiles.list:

/Users/haddad/dir1/file1.txt
/Users/haddad/dir1/file2.txt
/Users/haddad/dir1/file3.txt

How can I accomplish this in linux/mac?

share|improve this question
up vote 13 down vote accepted
ls -d "$PWD"/* > listOfFiles.list
share|improve this answer
    
would this work in red hat linux? – arabian_albert Mar 8 at 19:07
3  
That command works in any Linux or UNIX operating system. If you want to get one file per line, you need to use ls -d -1 $PWD/* – MelBurslan Mar 8 at 19:11
1  
if your file names are long or terminal width is narrow,, yes, that will be the case, but say you maximized the terminal window to occupy the whole screen or your file names (including the path) are really short, that will not hold true. -1 option guarantees you get one filename per line – MelBurslan Mar 8 at 19:18
6  
@MelBurslan 's addition is only needed if output is to a termiinal. ls detects if output is is to a file or terminal. – Runium Mar 8 at 19:58
6  
this will fail if there are many thousands of files in the directory, i.e. enough to exceed the maximum command line size (made more likely by the fact that the shell is expanding the filenames with full path). @Andy Dalton's find answer is a better solution, as it won't fail no matter how many files are to be listed. – cas Mar 8 at 22:39

You can use find. Assuming that you want only regular files, you can do:

find /path/to/dir -type f > listOfFiles.list

You can adjust the type parameter as appropriate if you want other types of files.

share|improve this answer
7  
+1 for pointing a more future-proof solution that ls. This find does recurse the subdirectories, for non-recursive you need to add -maxdepth 1 before -type argument. – kubanczyk Mar 8 at 21:19

Note that in:

ls -d "$PWD"/* > listOfFiles.list

It's the shell that computes the list of (non-hidden) files in the directory and passes the list to ls. ls just prints that list here, so you could as well do:

printf '%s\n' "$PWD"/*
share|improve this answer
2  
Using printf has the added bonus that you won't get a 'command line too long' error if you have thousands of files as printf is not run as a separate process. – Adrian Pronk Mar 10 at 9:07
1  
@AdrianPronk, yes, except in shells where printf is not built-in like pdksh and some of its derivatives or most versions of the Bourne shell. One drawback compared to ls -d is that if there's no non-hidden file in there, it will print /path/to/* while ls will give you an error about that file not existing instead. – Stéphane Chazelas Mar 10 at 9:44

To see just regular files --

find "$PWD" -type f  > output
share|improve this answer

In a past Linux environment, I had resolve command that would standardize paths, including making a relative path into an absolute path. I can't find it now, so maybe it was written by someone in that organization.

You can make your own script using functions in the Python or Perl standard libraries (and probably other languages too).

resolve.py :

#!/bin/env python

import sys
import os.path

for path in sys.argv:
    print os.path.abspath(path)

resolve.pl :

#!/bin/env perl

use warnings;
use Cwd qw ( abs_path );

foreach (@ARGV) {
    print abs_path($_), "\n";
}

Then, you would solve you problem with:

resolve.py * > listOfFiles.list

With this command, you can also do things like this:

cd /root/dir1/dir2/dir3
resolve.py ../../dir4/foo.txt
# prints /root/dir1/dir4/foo.txt
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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