I have external hard drives that I have put some media files on. I need to create a text list of the directories inside the hard drives. Is there a way to do this or a program in Linux that does it?
If I understand correctly, something like this should work:
find -type d /mnt/disk > directory_list.txt
This should work, just change the path and .txt file name:
find /Path/To/Disk/ -type d -printf '%f\n' > your_text_file.txt
It should print out only the folder names.
This command will not work because the path is in the wrong place.
Damn. I thought my mind-reading skills have improved
You misunderstood me, what i meant is instead of:
find -type d /mnt/disk > directory_list.txt
The Path should be specified before -type
or you will get a error message.
find /mnt/disk -type d > directory_list.txt
Oh. You are right. My bad.
I’m too used to fd
now
Thanks for the correction!
Ok so if its an external disk where do I look for the path?
Using your favourite file manager, you can most probably right-click and choose “Open in Terminal”.
Then, pwd
should give you the path.
OOoooooo hey nifty! Thanks
Is there a way to only get the top level directories?
Yes, you can set depth. See: find --help
or man find
Is there a limit on how many directories can be in the directory? I have a directory with 2,196 directories and I run this it takes about 45 seconds and there is no output text file.
Sorry but the manual does not make sense to me. I have a TBI and my comprehension is not what it used to be.
If manual doesn’t make sense to you, there are plenty of articles about find
, you just have to ask google exactly the same question.
From man find
:
-maxdepth levels
Descend at most levels (a non-negative integer) levels of directories below
the starting-points. Using -maxdepth 0 means only apply the tests and ac‐
tions to the starting-points themselves.
-mindepth levels
Do not apply any tests or actions at levels less than levels (a non-nega‐
tive integer). Using -mindepth 1 means process all files except the start‐
ing-points.
Yeah I have googled it and gotten quite a bit of info, but that info right there does not tell me where to put the “0” I’m guessing since I just want the top level directory.
If you didn’t specify a custom directory it will be saved to your $HOME
folder, just run ls
in the terminal and look for the file name.
find /DIR/TO/DATA/ -maxdepth 1 -type d -printf '%f\n' > /DIR/TO/FILE.txt
Awesome that worked, thanks. One more thing is there a command that can be added so that the output is in alphabetical order?
The sort
command should do the trick, try:
find /DIR/TO/DATA/ -maxdepth 1 -type d -printf '%f\n' | sort > /DIR/TO/FILE.txt
Thanks! That worked loads!