0

I wanted to automate the process of fixing the indexing on some videos. Below is my code.

for f in ~/Videos/Temp/*
do
    f=$(echo $f | sed 's/ /\\ /g')
    name=$(echo $f | sed 's/Temp/Fixed/1')
    mencoder -forceidx "$f" -o "$name" -oac copy -ovc copy
done

The problem I am having is that mencoder is claiming that it cannot find the file store in the variable $f.

MEncoder svn r34540 (Ubuntu), built with gcc-4.6 (C) 2000-2012 MPlayer Team
File not found: '/home/name/Videos/Temp/file\ name.avi'
Failed to open /home/bryan/Videos/Temp/file\ name.avi.
Cannot open file/device.

Exiting...

When I print the command to the terminal with the expanded variables and run it, it works just fine. I even modified my above code to print out the command for fixing each video, pasted that into a new script, and everything worked without a hitch. I am running this on Ubuntu. Any ideas as to what might cause this?

4
  • You don't need to escape your spaces, since you (correctly) use quotes around your variable. Maybe that's (part of?) the problem? You may also want to check that your Terminal is also using bash as it's shell. Commented May 25, 2012 at 7:35
  • @mivk: Except that the variable isn't quoted when it's echoed in the assignment to name. Variables should always be quoted when they're expanded (as you know). Commented May 25, 2012 at 11:26
  • 1
    What is causing it (as you know) is spaces in the filenames. The best solution is to rename all the files so that they don't contain whitespace. Allowing whitespace in paths is a poor design decision on the part of the filesystem maintainer. Using whitespace in paths is absurd. Demanding that filesystems support them ought to be a capital offense. Commented May 25, 2012 at 12:23
  • @dennis: f=$(echo some file name); touch "$f"; ls "$f"returns some file name. But f2=$(echo $f | sed 's/ /\\ /g'); ls "$f2" returns ls: cannot access some\ file\ name: No such file or directory. (At least with bash 4.1.5.) Commented Jun 5, 2012 at 22:17

2 Answers 2

1

As others have pointed out, you don't need to manually escape the spaces in the file name. However, you don't need to use sed to modify the paths, either.

for f in ~/Videos/Temp/*
do
    mencoder -forceidx "$f" -o "${f/Temp/Fixed}" -oac copy -ovc copy
done

Only the first occurrence of "Temp" is replaced with "Fixed", so you needn't worry about unintended replacements in your file names.

Sign up to request clarification or add additional context in comments.

Comments

0


    find ~/Videos/Temp/ -type f -print0 | while IFS= read -r -d $'\0' f; do
        name=$(echo "$f" | sed 's/Temp/Fixed/1')
        mencoder -forceidx "$f" -o "$name" -oac copy -ovc copy
    done

3 Comments

It's probably best to use full paths as well in a script, just in case you want to schedule it later.
That's true. I do normally convert relative path to full path at the beginning of the script.
$f should be quoted in the second line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.