1

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")?

  • 1
    as long as the varying directory level is a single directory, what I mean by that, if you have /dir1/dirX/dir3/dir4 and the dirX part is not changing like dirX/dirY/dirZ but can only be one of the dirX, dirY or dirZ, then you can reference /dir1/dirX/dir3/dir4 as /dir1/*/dir3/dir4 – MelBurslan Feb 18 '16 at 16:15
  • @MelBurslan - please post that as answer. In my other case: mpv /media/username/*/VIDEO_TS does what I want. –  Feb 18 '16 at 16:20
  • @MelBurslan - I have edited the question to point out that it is about a single directory –  Feb 18 '16 at 16:41

2 Answers2

2

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

Centimane
  • 4,490
1

as long as the varying directory level is a single directory, what I mean by that, if you have /dir1/dirX/dir3/dir4 and the dirX part is not changing like dirX/dirY/dirZ but can only be one of the dirX, dirY or dirZ, then you can reference /dir1/dirX/dir3/dir4 as /dir1/*/dir3/dir4

In your case mpv /media/username/*/VIDEO_TS should work, although, since the DVD titles usually contain spaces, I suggest enclosing the whole path in between double quotes such as "/media/username/*/VIDEO_TS"

MelBurslan
  • 6,966