Reputation: 13
I have been searching for several hours now, but can't seem to find an answer to this. I am attempting to create a shell script in a FreeNAS 11 jail (freebsd) that will move some files into a sub folder. I have it getting the paths correctly, but when it comes time to move the files over, it doesn't work (when the file name contains spaces).
#!/bin/sh
# Primary dir to scan
SRC_DIR=/mnt/transcode/batch/
WORKING_DIR="${SRC_DIR}working/"
for FILE in $SRC_DIR*
do
# only run against files...
if test -f "${FILE}"
then
#FILE_NAME=$(basename "$FILE" | sed 's/ /\\ /g') # replace spaces with escaped space
#mv "$SRC_DIR""$FILE_NAME" "$WORKING_DIR""$FILE_NAME"
#mv $SRC_DIR$FILE_NAME $WORKING_DIR$FILE_NAME
FILE_NAME=$(basename "$FILE")
mv \""$SRC_DIR""$FILE_NAME"\" \""$WORKING_DIR""$FILE_NAME"\" #wrap each parameter in double quotes
# copying this output back into terminal works
#echo mv \""$SRC_DIR""$FILE_NAME"\" \""$WORKING_DIR""$FILE_NAME"\"
fi
done
The above code also contains a few extra things that I have tried as well.
I get the following message when running the script
mv: rename "/mnt/transcode/batch/REDACTED FILE NAME.mkv" to "/mnt/transcode/batch/working/REDACTED FILE NAME.mkv": No such file or directory
The thing that is interesting is that when I take the output of echo mv \""$SRC_DIR""$FILE_NAME"\" \""$WORKING_DIR""$FILE_NAME"\"
and paste it back into terminal it will execute perfectly fine.
I am running this via SSH as root. Destination folder permissions are drwxrwxr-x+
I am running my script via ./test.sh
(as root user)
Is there another way to do this, or is there a problem with my script?
Any help is appreciated.
PS There is no /bin/bash in my jail, not sure if that makes a difference.
--Edit--
Here is the (hopefully) final code that I want to execute:
#!/bin/sh
# Primary dir to scan
SRC_DIR=/mnt/transcode/batch/
DEST_DIR="${SRC_DIR}converted/"
WORKING_DIR="${SRC_DIR}working/"
DONE_DIR="${SRC_DIR}orginal/"
# only get files, not directories
#for FILE in `ls -p $SRC_DIR | grep -v /`
for FILE in $SRC_DIR*
do
# only run against files...
if test -f "${FILE}"
then
FILE_NAME=$(basename "$FILE" | sed 's/ /\\ /g')
# move the file to the working dir
mv $SRC_DIR$FILE_NAME $WORKING_DIR$FILE_NAME
# vars for later
MEDIA_FILE=$WORKING_DIR$FILE_NAME
# Get some into about the current file
AUDIO_CODEC="$(ffprobe -v error -select_streams a:0 -show_entries stream=codec_name -of csv=s=x:p=0 $MEDIA_FILE)"
MEDIA_INFO="$(ffprobe -v error -select_streams v:0 -show_entries stream=height,width -of csv=s=x:p=0 $MEDIA_FILE)"
VIDEO_HEIGHT="$(echo "$MEDIA_INFO" | cut -d 'x' -f2)"
VIDEO_WIDTH="$(echo "$MEDIA_INFO" | cut -d 'x' -f1)"
# start up handbrake
HandBrakeCLI -i $WORKING_DIR$FILE_NAME -o $DEST_DIR${FILE_NAME%.*}.mkv -f av_mkv -m -e x264 -q 22 --vfr --audio-lang-list eng -E copy:$AUDIO_CODEC --crop 0:0:0:0 -w $VIDEO_WIDTH -l $VIDEO_HEIGHT --subtitle-lang-list eng
# move the file to the done dir
mv $WORKING_DIR$FILE_NAME $DONE_DIR$FILE_NAME
fi
done
The eventual goal is to move a single file to a working directory, while is is being transcoded, then move it out do a done folder.
Upvotes: 1
Views: 1697
Reputation: 85767
The problem is the extra quotes you're adding to the filenames.
mv \"foo\" bar
tries to rename a file literally called "foo"
(including quotes).
Also, don't use ALL_UPPERCASE
for your variables. ALL_UPPERCASE
is used by the system (e.g. HOME
) or the shell itself (e.g. RANDOM
, PWD
).
I'd do it like this:
src_dir=/mnt/transcode/batch
working_dir="$src_dir/working"
for file in "$src_dir"/*; do
# only run against files...
if test -f "$file"; then
mv -- "$file" "$working_dir/"
fi
done
You can mv
directly into a directory, no need to extract the basename first.
Untested, but here's the rest of your script. I've added the missing quotes, removed the weird backslash additions, etc.
#!/bin/sh
# Primary dir to scan
src_dir=/mnt/transcode/batch
dest_dir="$src_dir/converted"
working_dir="$src_dir/working"
done_dir="$src_dir/orginal"
for src_file in "$src_dir"/*; do
if test -f "${src_file}"; then
basename="${src_file##*/}"
working_file="$working_dir/$basename"
# move the file to the working dir
mv -- "$src_file" "$working_file"
# Get some into about the current file
audio_codec="$(ffprobe -v error -select_streams a:0 -show_entries stream=codec_name -of csv=s=x:p=0 "$working_file")"
media_info="$(ffprobe -v error -select_streams v:0 -show_entries stream=height,width -of csv=s=x:p=0 "$working_file")"
video_height="${media_info#*x}"
video_width="${media_info%%x*}"
# start up handbrake
HandBrakeCLI -i "$working_file" -o "$dest_dir/${basename%.*}.mkv" -f av_mkv -m -e x264 -q 22 --vfr --audio-lang-list eng -E copy:"$audio_codec" --crop 0:0:0:0 -w "$video_width" -l "$video_height" --subtitle-lang-list eng
# move the file to the done dir
mv -- "$working_file" "$done_dir/"
fi
done
Upvotes: 3