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 3 Answers
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 {} + 3tree -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 "\->"
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