Score:1

How can i copy the content of a directory into a text file?

ng flag

Let me be clear, what I just want to do is to copy the content of the directory (as if it just were a normal text) into a text file. Is there a command I can use to do that? (a variant of cp maybe?) Or is ctrl+c the only solution?

guiverc avatar
cn flag
I'm not sure if you want what's in a directory (ie. pipe the directory into the file using `ls`), or contents of the file (if so pipe the contents using `cat`).. but you've not provied your OS & release either; thus no clue as to if the directory is *confined* etc.
vanadium avatar
cn flag
Can you expand on your solution using ctrl+c? I do not know this solution.
cn flag
@guiverc this "Or is ctrl+c the only solution?" means he is talking about `ls`
Score:3
zw flag

I would recommend to use the very useful tree package and command. Install it with sudo apt-get install tree . See man tree locally or online for usage instructions.

Example output is below:

$ tree /etc/apt/
/etc/apt/
├── apt.conf.d
│   ├── 01autoremove
│   ├── 01autoremove-kernels
│   ├── 01-vendor-ubuntu
│   ├── 10periodic
│   ├── 15update-stamp
│   ├── 20archive
│   ├── 20auto-upgrades
│   ├── 20dbus
│   ├── 20packagekit
│   ├── 20snapd.conf
│   ├── 50unattended-upgrades
│   ├── 70debconf
│   ├── 99proxy
│   ├── 99update-notifier
│   ├── docker-autoremove-suggests
│   ├── docker-clean
│   ├── docker-gzip-indexes
│   └── docker-no-languages
├── auth.conf.d
├── preferences.d
├── sources.list
├── sources.list.d
└── trusted.gpg.d
   ├── ubuntu-keyring-2012-archive.gpg
   ├── ubuntu-keyring-2012-cdimage.gpg
   └── ubuntu-keyring-2018-archive.gpg

5 directories, 22 files
Score:2
cn flag

If you pipe the results to a file what was shown in the terminal window will be inside that file. Example:

ls -ltr

shows a list of files in a directory then ...

ls -lrt > listing.log

then listing.log will have the same inside the file,

Score:0
vn flag

Your question is less detailed and it is kinda hard to understand what you're actually trying to say or help us you with.

In this case i'm assuming you're trying to get all of the file/contents/folder names into a txt file.

To do that you just have to list them and copy the list into a text file. Command for that is ls > output.txt using only ls will result in having some extra details about the file such as Permissions, Link count, Owner, Group, File size, Last modified date, File name.

To save only the filenames you just have to use -1a after the command ls. So if you want only the file names just use this command instead ls -1a > output.txt

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.