Is it possible to specify a path in which a directory name is variable?

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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"


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x