0

I have several video files, which contain escape characters like [ in combination with [1920x1080]. When I execute:

find /run/media/user/usbdisk -type f -iname [HD, 1920x1080] - Wild Wild West.mp4 -ls

then I receive an error like this:

find:paths must precede expression

When I execute

find /run/media/user/usbdisk -type f -iname "[HD, 1920x1080] - Wild Wild West.mp4" -ls

the I get NO result, though I'm sure, the file exist.

Same result (nothing found) when execute:

find /run/media/user/usbdisk -type f -iname '[HD, 1920x1080] - Wild Wild West.mp4' -ls

To receive the directory name (basename) of my movie, I have stored the name in a variable named video, so that my command finally looks like this:

dname=$(find /run/media/user/usbdisk -type f -iname "$video" -exec dirname {} \;)

but this leads to error result (see above).

How can I make the command find (or any else) to exactly find my exact named file including escape signs? Is there any way or is it necessary to substitute the escape signs in the file names? I want prevent this. Thank you in advance.

1

-iname evaluates its argument, *,? and [] have special meaning to it, so you have to escape them if you want to pass them literally.

Also consider using -quit in case you otherwise find two or more files in different folders (duplicates?). I guess you expect dname to be a single directory name.

I understand $video is already given:

video="[HD, 1920x1080] - Wild Wild West.mp4"

In this case try:

video1=$(echo $video | sed 's|\[|\\[|g; s|\]|\\]|g')
dname=$(find /run/media/user/usbdisk -type f -iname "$video1" -exec dirname {} \; -quit)

It can be done in one line. I chose to introduce additional variable for clarity.

  • Thanks first. The point is, the (movie) names are fixed, preset. I can't add a \ to pass escape special characters. I simply want to pass the content of a variable (here $video) no matter what it contains. And yes, $dname should contain the directory's name, where the video is found. – Linuxwhisperer Jul 4 '17 at 13:35
  • @Linuxwhisperer I have adjusted my answer to your needs. – Kamil Maciorowski Jul 4 '17 at 13:49
  • I tried - no result... – Linuxwhisperer Jul 4 '17 at 13:54
  • @Linuxwhisperer It works in my Kubuntu. Just to make sure: you didn't expect dname=… to return something, you checked echo $dname after, right? Can you give me any feedback? (e.g. "it works with such-and-such filename but not with…" or "video1 doesn't seem right because…"). – Kamil Maciorowski Jul 4 '17 at 14:14
  • sorry for late reply. I had to try again, copy and paste and adopt... Now you way seems to work. I will make more testings in my scripts now. Thanks again! – Linuxwhisperer Jul 4 '17 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.