Make a list of filenames
11,115
Solution 1
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.
Solution 2
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
Related videos on Youtube
Author by
Gary Bollenbach
Updated on September 18, 2022Comments
-
Gary Bollenbach almost 2 years
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.-
George Udosen almost 6 yearsPlease get rid of the
version
else your question might be closed! -
αғsнιη almost 6 yearssimilar to the question askubuntu.com/q/1028197/283843
-
K7AAY almost 6 yearsThe 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.
-
David Foerster almost 6 yearsPossible duplicate of File list command line (hidden and subfolders) combined with How do I save terminal output to a file?
-
-
K7AAY almost 6 yearsIt will also list "files.txt" which is probably undesirable behavior.
-
A.B. almost 6 yearsDoesn't works for filenames with spaces.
-
K7AAY almost 6 yearsCorrected to solve special case above
-
PerlDuck almost 6 years... or use
ls -b
which will quote any special characters (including\n
) in the filenames. -
A.B. almost 6 yearsNow you have one line in the output file?
-
A.B. almost 6 yearsForget the
ls
approach -
PerlDuck almost 6 years@A.B. LOL, you mean because of this: mywiki.wooledge.org/ParsingLs ?
-
Gary Bollenbach almost 6 yearsCases I see will be unlikely to have spaces in filenames. Also this is the only response that worked out of the box. Thanks much for help. GB
-
David Foerster almost 6 years
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 -
CentaurusA almost 6 yearsMany 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.