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

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?


Solution 1:

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

Solution 2:

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,