0

I have this bit of code:

find . ! -path . -type d -maxdepth 3 -mindepth 3 -exec sh -c '
    dir="$0"
    tvnamer --batch $dir
    mv $dir/*.mkv $dir/..
    [ -f $dir/*.part ] || trash $dir
' {} ';'

However, it breaks because there are spaces in directory names in $dir (TVnamer gets confused).

./Show/Season/[ www.Torrenting.com ] - Castle.2009.S06E01.720p.HDTV.X264-DIMENSION
./Show/Season/[ www.Torrenting.com ] - Revenge.S03E01.720p.HDTV.x264-2HD

How can I remove the spaces / rename the folders in $dir and then pass that to the rest of the code?

2 Answers 2

2

Use double quotes whenever you reference $dir:

find . ! -path . -type d -maxdepth 3 -mindepth 3 -exec sh -c '
    dir="$0"
    tvnamer --batch "$dir"
    mv "$dir"/*.mkv "$dir"/..
    [ -f "$dir"/*.part ] || trash "$dir"
' {} ';'
Sign up to request clarification or add additional context in comments.

Comments

1

As @fedorqui said, it is a bug in your "bit of code" where it has not quoted the directory name. However, to answer your question How to remove spaces from variable/directories in shell?:

dir="Program Files"
new_dir=${dir// /}
mv "$dir" "$new_dir"

The string substitution is supported in Bash and ksh93 (and maybe elsewhere). ${dir// /} means: take the variable dir and replace globally (//) the pattern space with nothing (the text after the final /).

Comments

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.