4

Is it possible to create a file which contains the information (the contents) of a certain folder? For example:

cat /folder >> file

I also tried

touch /folder >> file

I should add that I need the folders and their folders and files in a recursive way

Anwar
  • 75,875
  • 31
  • 191
  • 309
Sam
  • 655
  • 1
  • 5
  • 9

2 Answers2

5

I would do it by

ls /path/to/folder > outputfile

If you want to see the recursive contents of folders within folders with file permissions, you can use

ls -lR /path/to/folder > outputfile
Pilot6
  • 88,764
  • 91
  • 205
  • 313
  • Thank you @Pilot6 , it lists the names of the folders and files in a vertical fashion. In other words, creates a file that lists the folders and files in a specific folder. – Sam Oct 10 '16 at 21:27
  • By the way, the command `ls -lR /path/to/folder > outputfile` does the something similar, also adding the the chain of subfolders that are inside the folder, and their respective content(-R). -l gives extensive information. Also thanks for your help @Owen Hines ! – Sam Oct 10 '16 at 21:39
  • @Pilot6 , may I add the command `ls -lR /path/to/folder > outputfile` in your answer as an additional option? – Sam Oct 10 '16 at 21:42
  • @Sam , Yeah, sorry that I didn't get it quite right. – TheOdd Oct 10 '16 at 22:11
  • @Sam Sure you can add it as an option with a description what it does. I will approve it. – Pilot6 Oct 11 '16 at 10:36
  • @Sam I rejected your edit because it did not explain the usage of the `-lR` parameters of `ls`. "Also works" is not an explanation. You need to write WHY you prefer to use `ls` with these options. – Pilot6 Oct 11 '16 at 21:13
  • Besides `-R` is recursive. You did not ask for a recursive list in your question. – Pilot6 Oct 11 '16 at 21:16
  • I thought that the explanation would appear on the summary. I forgot to add that I also wanted what is inside each folder, hence -R. – Sam Oct 11 '16 at 21:36
  • OK. I will add it myself. – Pilot6 Oct 11 '16 at 21:37
  • @Pilot6 , I see you add an explanation on the command line but ass a suggestion. Could you add it on your answer? – Sam Oct 11 '16 at 21:45
  • I did add it to my answer. – Pilot6 Oct 11 '16 at 21:46
  • Now I see it! And it's an easy and complete explanation. Thank you! – Sam Oct 11 '16 at 21:52
1

Do something like

ls >> outputFile

This will pass the output of the ls command to a specified file.

For example, if you are in a folder that contains:

  • File1
  • File2
  • File3

Then running ls >> outputFile will make a file called outputFile that contains

File1 File2 File3

TheOdd
  • 2,972
  • 1
  • 21
  • 41
  • I run that command a the result is a file called homedir which contains the names of all the folders and files located in pwd. I'm trying to do something similar to `cat file1 file2 > file3`. – Sam Oct 10 '16 at 20:15
  • Exactly. I did that. Maybe I should use -R, since that would give me all the content of a chain of folders. – Sam Oct 10 '16 at 20:22
  • I mark because it gave me a partial result. But I still need to figure it out. I also not certain of but the exercise asked me to do. I mean, is it similar to `cat file1 file2 > file3`? Thanks for you help! – Sam Oct 10 '16 at 20:30
  • That'd be nice @Owen Hines , but it seems that I don't have enough reputation to ask in the chat. – Sam Oct 10 '16 at 20:50
  • Why do append with `>>`? And `echo` is not needed. – Pilot6 Oct 10 '16 at 21:14
  • [Useless use of backticks/subshells](http://porkmail.org/era/unix/award.html#backticks). Why not simply `ls >> outputfile`? – David Foerster Oct 18 '16 at 12:02