Score:2

How would I create a list of sub-folders in a given folder without the full path?

gb flag

The following in a bash script:

find /Volumes/SpeedyG -type d >> file.txt

...works well to list the folders in that path in a text file,

/Volumes/SpeedyG/folder1  
/Volumes/SpeedyG/folder2  
/Volumes/SpeedyG/folder2

but the results are the full path to the folder.

What if I just wanted the folder name without the full path?


folder1  
folder2  
folder3  
Score:6
hr flag

For the GNU implementation of find, you can do this using the formatted output action printf:

          %P     File's name with the name  of  the  starting-point  under
                 which it was found removed.

So

find /Volumes/SpeedyG -type d -printf '%P\n' >> file.txt

If you want to remove all leading directory components, you can use %f instead of %P. If you have zsh, you can do the same using recursive shell globbing and the :t (tail) qualifier:

print -rC1 /Volumes/SpeedyG/**/*(ND/:t)

in which case you don't need find at all. With any POSIX sh, you could always do

find dir -type d -exec sh -c '
  for f do printf "%s\n" "${f##*/}"; done
' find-sh {} +
gb flag
I must have a syntax problem. If I run this from my mac terminal I get `find: -printf: unknown primary or operator` Perhaps it's different in OSX than it is in Ubuntu.
ojdo avatar
co flag
@JonGriffith: yes, this seems to be the case: https://superuser.com/a/293200/238733, and here a more up-to-date answer (2020 instead of 2011): https://stackoverflow.com/a/61158594/2375855
cn flag
@JonGriffith If you're seeking answers for OSX rather than Ubuntu, [Unix SE](https://unix.stackexchange.com/) is a much better place for asking. [Superuser SE](https://superuser.com/) also could work.
Score:0
cn flag

tree -di /Volumes/SpeedyG will give you a list of only subfolders.

tree -dia /Volumes/SpeedyG will give you a list of only subfolders to include hidden directories.

tree -diL 1 /Volumes/SpeedyG will give you a list of only subfolders 1 level in.

note: if a directory is a symbolic link to another directory, then that line of output will show the link and path to the target directory...
You could eliminate it with a grep statement like tree -di /Volumes/SpeedyG | grep -v "\->"

gb flag
I would prefer a result that trims the leading path so I only get everything to the right of SpeedyG/
WU-TANG avatar
cn flag
@JonGriffith To be clear, what you are saying is, if you did `tree -di /Volumes/SpeedyG > sampletext.txt`... And you look at sampletext.txt, the only thing that is undesired in the file, is the very first line in it... correct?
gb flag
No. Each line in the txt file refers to the full path of the files that were added to the txt, i.e. /Volumes/SpeedyG/folder1. I would like the resulting list to be trimmed to only show a list of all of the folders found at this level, so the list would be "folder1, folder2, folder3" etc.
WU-TANG avatar
cn flag
@JonGriffith ...I don't have the same results that you are seeing.. all i see is one subdirectory name per line... As a matter of fact, if I wanted to see the full path I would have to do `tree -fdi /Volumes/SpeedyG` instead... But now that you mentioned "at this level", I will edit for anyone else that would find that useful. Don't know what to say about why you are seeing what you are seeing though.
Score:0
mx flag

GNU basename removes any leading directory components from a full pathname.

$ basename /Volumes/SpeedyG/folder1
folder1

To process multiple pathnames, use the --multiple option:

‘-a’
‘--multiple’
     Support more than one argument.  Treat every argument as a NAME.
     With this, an optional SUFFIX must be specified using the ‘-s’
     option.

$ basename -a /Volumes/SpeedyG/folder1 /Volumes/SpeedyG/folder2 /Volumes/SpeedyG/folder3
folder1
folder2
folder3

In a script, a more efficient way of stripping off the largest prefix is to resort to parameter expansion like so:

file="/Volumes/SpeedyG/folder1"
file="${file##/*/}"

You can utilize this in a for loop:

for i in /Volumes/SpeedyG/*
do
    printf "%s\n" "${i##/*/}"
done > $HOME/files.txt

$ cat ~/files.txt
folder1
folder2
folder3
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.