Linux show all files in directory

How to list all files in a directory with absolute paths

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

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

11 Answers 11

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.

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

find «$(pwd)» -type f > listOfFiles.txt will list files w.r.t. working directory. Note: the file listOfFiles.txt will also be listed in this approach.

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

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/*

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 ‘s addition is only needed if output is to a termiinal. ls detects if output is is to a file or terminal.

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.

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:

Note that it doesn’t include hidden files, includes files of any type (including directories) and if there’s no non-hidden file in the directory, in POSIX/csh/rc shells, you’d get /current/wd/* as output. Also, since the newline character is as valid as any in a file path, if you separate the file paths with newline characters, you won’t be able to use that resulting file to get back to the list of file reliably.

With the zsh shell, you could do instead:

print -rNC1 $PWD/*(ND-.) > listOfFiles.list 
  • -rC1 prints r aw on 1 C olumn.
  • -N , output records are NUL-delimited instead of newline-delimited (lines) as NUL is the only character that can’t be found in a file name.
  • N : expands to nothing if there’s no matching file ( nullglob )
  • D : include hidden files ( dotglob ).
  • -. : include only regular files ( . ) after symlink resolution ( — ).

Then, you’d be able to do something like:

To remove those files for instance.

You could also use the 😛 modifier in the glob qualifiers to get the equivalent of realpath() on the files expanded from the globs (gets a full path exempt of any symlink component):

print -rNC1 -- *(ND-.:P) > listOfFiles.list 

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.

Читайте также:  How to extract files in linux

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

To see just regular files —

Another way with tree , not mentioned here, it goes recursively and unlike find or ls you don’t have any errors (like: Permission denied , Not a directory ) you also get the absolute path in case you want to feed the files to xargs or other command

tree -fai /pathYouWantToList >listOfFiles.list 
-a All files are printed. By default tree does not print hidden files (those beginning with a dot `.'). In no event does tree print the file system constructs `.' (current directory) and `..' (previous directory). -i Makes tree not print the indentation lines, useful when used in conjunction with the -f option. -f Prints the full path prefix for each file. 

sudo apt install tree on Ubuntu/Debian

sudo yum install tree on CentOS/Fedora

sudo zypper install tree on OpenSUSE

@rogerdpack sudo apt install tree on Ubuntu sudo yum install tree on CentOS sudo zypper install tree on OpenSUSE

You can just use realpath or readlink this naughty way:

When ls prints to a TTY it formats the file names in columns, but when it’s writing to a file, pipe, or other non-TTY it behaves like ls -1 and prints one file name per line. You can check this by running ls | cat in place of ls . [1]

  • xargs build and execute command lines from standard input.
  • realpath : return the canonicalized absolute pathname
  • readlink : read value of a symbolic link
  • Use realpath — to make it treat everything that follows as parameters instead of options, if files could have » -something «.
  • If some files have spaces you could:

@rogerdpack do you have coreutils package installed? I get that info with dpkg -S /usr/bin/realpath . Check out this.

In a past Linux environment, I had a 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).

#!/bin/env python import sys import os.path for path in sys.argv: print os.path.abspath(path) 
#!/bin/env perl use warnings; use Cwd qw ( abs_path ); foreach (@ARGV)

Then, you would solve your 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 

Recursive files can be listed by many ways in linus. Here i am sharing one liner script to clear all logs of files(only files) from /var/log/ directory and second check recently which logs file has made an entry.

find /var/log/ -type f #listing file recursively 
for i in $(find $PWD -type f) ; do cat /dev/null > "$i" ; done #empty files recursively 
ls -ltr $(find /var/log/ -type f ) # listing file used in recent 

note: for directory location you can also pass $PWD instead of /var/log.

I find bash find $(pwd) works well. Also, ls | xargs realpath works. You can add any other flags to the latter example as well as the first.

To list the full path of all commands (apps/programs) accessible to the user. (revised to address most, but not all limitations outlined in the comments)

eval ls -d $(echo $PATH | sed -e 's|^:|.:|' -e 's|:$|. |' -e 's|:|/[[:word:]]* |g') 2>/dev/null | sort 

NOTE
The PATH variable would normally have a colon ( : ) either at the beginning or at the end, but not both. A colon at the beginning or end signifies to search the current directory as well. Standard practice is for it to be at the end so as to never override standard utility programs. The sed substitutions here handle either case.

Explanation.

ls: cannot access ‘./[[:word:]]‘: No such file or directory
ls: cannot access ‘/home/alpha/bin/[[:word:]]
‘: No such file or directory
ls: cannot access ‘/usr/local/sbin/[[:word:]]‘: No such file or directory
ls: cannot access ‘/usr/local/bin/[[:word:]]
‘: No such file or directory
ls: cannot access ‘/usr/sbin/[[:word:]]‘: No such file or directory
ls: cannot access ‘/usr/bin/[[:word:]]
‘: No such file or directory
ls: cannot access ‘/sbin/[[:word:]]‘: No such file or directory
ls: cannot access ‘/bin/[[:word:]]
‘: No such file ordirectory

  • ls -d
    List directories. The -d option doesn’t really just list directories. By appending /* to each directory (see below), we will get the contents of the directories.
  • $( . ) Perform the commands inside parens and replace the $( . ) with the results for ls to use.
    • $(echo :$PATH | sed -e ‘s|^:|.:|’ -e ‘s|:$|. |’ -e ‘s|:|/[[:word:]]* |g’)
      • Produces a space-separated list of directory patterns like.
      ./[[:word:]]* /home/alpha/bin/[[:word:]]* /usr/local/sbin/[[:word:]]* /usr/local/bin/[[:word:]]* /usr/sbin/[[:word:]]* /usr/bin/[[:word:]]* /sbin/[[:word:]]* /bin/[[:word:]]* 

      Источник

      Get a list of all files in folder and sub-folder in a file

      How do I get a list of all files in a folder, including all the files within all the subfolders and put the output in a file?

      7 Answers 7

      You can do this on command line, using the -R switch (recursive) and then piping the output to a file thus:

      this will make a file called filename1 in the current directory, containing a full directory listing of the current directory and all of the sub-directories under it.

      You can list directories other than the current one by specifying the full path eg:

      will list everything in and under /var and put the results in a file in the current directory called filename2. This works on directories owned by another user including root as long as you have read access for the directories.

      You can also list directories you don’t have access to such as /root with the use of the sudo command. eg:

      sudo ls -R /root > filename3 

      Would list everything in /root, putting the results in a file called filename3 in the current directory. Since most Ubuntu systems have nothing in this directory filename3 will not contain anything, but it would work if it did.

      Maybe telling the person to cd into the directory first could be added to answer.Also this works fine if i own the directory but if trying in a directory say owned by root it didnt.I got the usual permission denied and sudo followed by your command also gave permission denied. IS there a work around without logging in as root?

      Well I did say «current» directory. The correct use of CD might the subject of another question, and I’m sure it has been. You can list directories owned by root as long as you have read access to them. Directories owned by root to which the user has read access can be listed with ls -R. It’s hard to imagine why you’d want to list directories owned by root to which you don’t have read access, but sudo does indeed work if you give the full path. I’m adding examples for both of these, but excluding the use of CD.

      Just use the find command with the directory name. For example to see the files and all files within folders in your home directory, use

      Also check find GNU info page by using info find command in a terminal.

      This is the most powerful approach. find has many parameters to customize output format and file selection.

      That’s the best approach in my opinion. Simple and practical. Could also do $ find . > output if there’s many directories.

      tree

      An alternative to recursive ls is the command line tool tree that comes with quite a lot of options to customize the format of the output diplayed. See the manpage for tree for all options.

      will give you the same as tree using other characters for the lines.

      to display hidden files too

      1. Go to the folder you want to get a content list from.
      2. Select the files you want in your list ( Ctrl + A if you want the entire folder).
      3. Copy the content with Ctrl + C .
      4. Open gedit and paste the content using Ctrl + V . It will be pasted as a list and you can then save the file.

      This method will not include subfolder, content though.

      You could also use the GUI counterpart to Takkat’s tree suggestion which is Baobab. It is used to view folders and subfolders, often for the purpose of analysing disk usage. You may have it installed already if you are using a GNOME desktop (it is often called disk usage analyser).

      sudo apt-get install baobab 

      You can select a folder and also view all its subfolders, while also getting the sizes of the folders and their contents as the screenshot below shows. You just click the small down arrow to view a subfolder within a folder. It is very useful for gaining a quick insight into what you’ve got in your folders and can produce viewable lists, but at the present moment it cannot export them to file. It has been requested as a feature, however, at Launchpad. You can even use it to view the root filesystem if you use gksudo baobab .

      (You can also get a list of files with their sizes by using ls -shR ~/myfolder and then export that to file.)

      Источник

Оцените статью
Adblock
detector