Path Command – Specify a Path with Variable Directory Name

commanddirectory-structurepath

Asking this question on mpv player and dvds, I stumbled into a more generic question: is it generally possible to specify a path in which one of the directory names is variable?

Let's say that I want to execute a file with a command. The executable is in /dir1/dir2/dir3/, but the name of dir2 is variable, although it will always contain dir3 (similar to VIDEO_TS, which is always similar to /media/username/NAME-OF-DVD/VIDEO_TS/ while NAME-OF-DVD varies).

If I want to execute that file with a command I have to specify the path. Can a such command be used (with a path in which one directory-name may be "generic")?

Best Answer

Bash can make use of globbing. Globbing allows you to specify a pattern that will match multiple values. It works similarly to REGEX, but it is important to note they are not the same.

  • *(pattern) matches a pattern 0 or more times
  • ?(pattern) matches a pattern 0 or 1 times
  • +(pattern) matches a pattern 1 or more times
  • [ ] can match a value contained within, including [a-z] for a through z
  • ( | ) can match values on either side of the pipe

If you don't put a pattern the pattern acts as a wildcard.

So a path like /dir1/dir2/dir3/ can be represented as:

  • /dir1/*/dir3/
  • /dir1/dir*/dir3/
  • /dir1/*(dir2|otherdir)/dir3/
  • /dir1/dir*[1-99]/dir3/

For more info check out this link: http://mywiki.wooledge.org/glob

or this one: http://www.linuxjournal.com/content/bash-extended-globbing

Related Question