0

Is it possible to use tree to create several output files without being cd ~/ into the directory I want to create a tree file?

For example, I have a directory called parent-dir.

Inside of parent-dir are subdirectories titled a, b, c to z and one also titled 0-9.

Inside of those subdirectories are more subdirectories that are titled at random using lower/upper case, numbers and some special characters. (It is at this level I want to create a tree .txt output file)


Here is the directory structure:

parent-dir ( <-- I will be cd ~/ here and run cmd from here)
|_a
| |_ahgfVFCJC6.h78 ( <-- cmd should create a tree titled ahgfVFCJC6.h78.txt)
| | |_file.jpg
| | |_file.mp4
| |  
| |_a34grBVFHEwerv ( <-- cmd should create a tree titled a34grBVFHEwerv.txt)
|   |_file.txt
|   |_file.mp3
|
|_b
| |_bhlHKH.7tbh ( <-- cmd should create a tree titled bhlHKH.7tbh.txt)
|   |_file.png
|   |_file.txt

...and so on...

The .txt files created using `tree`:

ahgfVFCJC6.h78.txt
a34grBVFHEwerv.txt
bhlHKH.7tbh.txt

should be outputted in a specified directory (example: ~/Desktop/Tree)

I am hoping to run a command while in parent-dir because in a alone there are thousands of directories like ahgfVFCJC6.h78 (but named differently). Having to cd into each of those sub-subdirectories would take far too much time.

5
  • That's a bit odd question, seeing that you have accepted an answer here, where your goal was accomplished, allegedly with my advice. As that wasn't the answer that resolved your issue, you actually should have accepted your own answer - that's perfectly fine :-) If you get advice, you test it and it doesn't help, you should edit the question and add the result. That way your question is up-to-date, so everyone can see we're not there yet. Feb 28 at 16:47
  • As for doing something wrong - I wouldn't say so. If you're used to discussion forum format, Q&A site format can feel a bit odd. You can think of this as a remote support service - the only diff is that nobody's getting paid for this :-D I'd really recommend taking the Tour and checking the Help, at least the Asking- and Answering -sections. That does tell you just about everything there is to know about how these sites work. Feb 28 at 16:51
  • will do. thank you. Feb 28 at 16:54
  • @Panki I realized what I was doing wrong by not accepting answers. It's been taken care of now. I also updated this post to provide a better example of what it is I am trying to accomplish. Any suggestions? Feb 28 at 19:36
  • @Peregrino69 I updated the original post to provide more details. Any suggestions on how to do this? Feb 28 at 19:36

2 Answers 2

2

In zsh:

for d (parent-dir/*/*(N/)) (cd -- $d && tree) > output-dir/$d:t.txt
9
  • im getting a syntax error on the parenthesis. Feb 28 at 18:08
  • @linuxuser24569, from zsh? Feb 28 at 18:25
  • sorry, i am not familiar with zsh. I just installed it but not sure how to access it. Feb 28 at 18:33
  • @linuxuser24569, the same way you access bash or any other shell, by running it in a terminal emulator. From within another shell, run zsh. To make it your login shell instead of bash, look at chsh or passwd -s depending on the system. Feb 28 at 18:40
  • ok, i got zsh to work. this command makes 1 .txt file though and has all of the directories merged. i need a .txt file for each of the directories. if you look at my original post, i will be cd ~ into parent-dir, i will run a command there that will need to go into a/ and then tree a .txt file for ahgfVFCJC6.h78 (called ahgfVFCJC6.h78.txt), then go to the next one in a/ and so on and on, once alll those directories are tree'd into .txt files then it should go into b/ and do the same. Feb 28 at 18:47
1

You don't need to cd to directory to use tree command on it, and you can specify multiple directories to use tree on them at the same time.

tree /dir1 /dir2 /dir3 -o /some/other/path/outputfile -n

also if you want to use stdout redirection like > or >> you can specify a path there also

tree /dir1 /dir2 /dir3 -n > /some/other/path/outputfile

If you have some file that has paths you want to check you can do something like following to create outputfile in each dir.

for i in $(cat filewithdirs); do tree "$i" -o "$i/outputfile"; done

If you want to get paths of all subdirectories in the parentdir you can use something like this

tree parentdir/ -L 1 -fid

This will print all first level subdirectories in parentdir with no indentation and with their path

  • -L option specifies how many levels deep do you want to go

  • -f options specified to print path prefix

  • -i specifies to not use indentation

  • -d specifies to only list directories

  • -o specifies to use outputfile instead of stdout

7
  • i would have to specify all the directories i want to create a tree file with then wouldnt i? that would be a massively long command. theres thousands of these directories I want to create their own tree.txt file with. Feb 28 at 16:50
  • how are you going through directories you want to run tree command against, you are asking ambiguous questions without ever providing any example of the commands or scripts you are running
    – ralz
    Feb 28 at 16:51
  • 1
    then use a for loop or something similar and output the file in the same dir you are running tree command against, nobody knows what you are doing and how are you feeding data into the commands. you need to give some examples if you want people to give you something more precise, not just in this question, but all the questions you asked.
    – ralz
    Feb 28 at 16:55
  • let me edit my original post. Feb 28 at 16:57
  • It does, yes, and you would have to, yes. You need to construct some kind of loop if you want each directory tree to be output in a separate file. There are many roads to Rome, no single one is the "correct" one - only what best suits your needs and capabilities. Posting your scripts here, with explanations what you expect to happen and what actually happens, is a good way to learn. Practically every time I post one, I get valuable advice on what can I do to make them work more efficiently, pointers to what pitfalls my approach has etc. Feb 28 at 16:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .