1

I'm looking for an easy way to build a list in a txt file of the filenames inside a given directory. Filenames only preferred, though I could trim other fields if necessary.

A.B.
  • 89,123
  • 21
  • 245
  • 323
Gary Bollenbach
  • 13
  • 1
  • 1
  • 4
  • 2
    Please get rid of the `version` else your question might be closed! – George Udosen Jul 13 '18 at 16:20
  • similar to the question https://askubuntu.com/q/1028197/283843 – αғsнιη Jul 13 '18 at 16:52
  • 1
    The post named in comment above wanted subfolders, path, size, and creation (or last modified) date . The original question for this post wanted none of that, just names. – K7AAY Jul 13 '18 at 16:57
  • 2
    Possible duplicate of [File list command line (hidden and subfolders)](/q/1028197) combined with [How do I save terminal output to a file?](/q/420981/175814) – David Foerster Jul 14 '18 at 10:46

3 Answers3

5

This command should be helpful:

find -maxdepth 1 -type f ! -name flist.txt -printf  "%P\n" > flist.txt

Command information:

  • -maxdepth: Don't search for files below folders of one level down.
  • type f: Search for only files
  • -printf "%P\n": Print the names only and on separate lines
  • > flist.txt: Store those names (using output redirection) in a file to be created on the fly called `flist.txt
  • ! -name flist.txt: Skips the name of the output file from the generated list
George Udosen
  • 35,970
  • 13
  • 99
  • 121
-1

The ls (list) command executed in a terminal window is the key, and its man page lists options.

Let's say you are in your home directory, and you want a list of all the files in directory /x/y . The command would be

ls -b1A /x/y > listoffiles

for the -b option lists all files even with spaces, -1 (number one, not letter l) option of ls will list with one line per file name, and -A will make sure hidden files are shown, but not directories.

K7AAY
  • 16,864
  • 9
  • 45
  • 77
-3

In terminal, change directory to the required folder, then use the command:

ls > files.txt

This will redirect a list of the contents of the folder to the text file files.txt. You didn't indicate what form the contents of the folder takes. If there are sub-folders present, in addition to a group of files, the folder names will also be included. But, once you have the text version you can sort/edit it any way you desire.

David Foerster
  • 35,754
  • 55
  • 92
  • 145
CentaurusA
  • 2,652
  • 1
  • 21
  • 27
  • 1
    It will also list "files.txt" which is probably undesirable behavior. – K7AAY Jul 13 '18 at 16:54
  • `ls` omits “hidden” dot files (unless instructed otherwise) and mangles file names with some unusual characters. Don't use it to list file names for anything but display to a human reader. -1 – David Foerster Jul 14 '18 at 10:48
  • Many people don't like this solution and downvote the answer - which is their prerogative. However, I would point out that the OP asked for an "easy way" to list files. Some of us have simple folder structures. We don't have concerns with hidden files. And, I did realize that the simple command would include files.txt, but I did say that the result could be edited. Anyway, for what it's worth, that's the simple-minded way I do things. – CentaurusA Aug 17 '18 at 02:09