Getting all "Level-1" File- & Folder-Names of a folder-path

Let's say Ordner 1 is the name of a sub-folder in your ~/Pictures folder. Then:

for f in ~/"Pictures/Ordner 1"/*; do
	printf '%s\n' "$(basename "${f%.*}")"
done

which, of course, can be saved to a variable instead of displayed in a window. The output returned is one filename per line, without filename extensions.

The important syntactical feature to note when copying and pasting the shell code above is the position of the double quotes where I've specified the folder path. ~/ is shorthand for your home directory (i.e. /Users/%you%/), and needs to remain outside the double quotes, with no space; and /* is shorthand for every file and folder at that path, and also needs to remain outside the double quotes, with no space.

3 Likes