Shell – Find directories with a certain name


How can I loop through each directory recursively and whenever it finds a folder called VIDEO_TS, let Genisoimage convert that folder's content to an dvd-video iso image, the name of the iso would be the parent folder's name.

The command that I use for a single directory:

genisoimage -o movie_1.iso -dvd-video /shares/media/movies/Movie 1

And this is the script I got:


for file in $(find /shares/media/ -type d -name 'VIDEO_TS')

parent_path="$(dirname -- "$file")"


So I got the parent path and the full path to the VIDEO_TS directory.

parent_path="/shares/media/Movie One/"
full_path="/shares/media/Movie One/VIDEO_TS/"

How can I get only the Movie One from parent_path?
And when I use sprintf to echo both variables, either spaces get removed or replaced with a line break.
Is it safe to use those variables with the command?

Best Answer

The command you are looking for is basename:

[root@localhost ~] # basename "/shares/media/Movie One/"
Movie One
[root@localhost ~] #
Related Question