Reputation: 6857
I want to extract name of all files contained in a folder into a .txt file for ex: when we type ls command in terminal it shows list all the files and folder names. Can we store all these names in a .txt file.
Upvotes: 1
Views: 2940
Reputation: 12603
If you really care about files and want to skip directories, I'd use find.
find . -type f --mindepth 1 --maxdepth 1 >> list.txt
Note, that this list will also contain list.txt
, as it is created before spawning find
in the current directory.
The advantage of using find
instead of ls
is that it will include "hidden" files (files starting with a .
in their name).
Upvotes: 0
Reputation: 2400
By using the >
character, a command's output can be written to a named file. For example,
ls > list.txt
This will directly post the output of the ls
command into the file list.txt
. However, this will also overwrite anything that's in list.txt
. To append to the end of a file instead of overwriting, use >>
:
ls >> list.txt
This will leave the previous contents of list.txt
in tact, and add the output of ls
to the end of the file.
Upvotes: 1
Reputation: 3486
You can redirect the output of the ls
command to a file with >
like so:
ls > files.txt
Note this will overwrite any previous contents of files.txt
. To append, use >>
instead like so:
ls >> files.txt
Upvotes: 3