Bash 脚本更改动态变量上的文件夹路径的一部分
Bash script changing part of folder path on dynamic variable
所以我整理了下面的bash脚本,我想修改我抓取的文件路径目录
所以我的布局是这样的
/home/videos_test/categoryname/1/vid.mp4
/home/videos_test/categoryname/2/randomvid.mp4
我想修改它抓取的文件路径,使其看起来像这样
/home/videos_test/changed_folder/1/vid.mp4
/home/videos_test/changed_folder/2/randomvid.mp4
完整剧本
#!/bin/bash
echo "Type the category that you want, followed by [ENTER]:"
read category
export FILE_PATH="/home/videos_test"
export STAR="/*"
FILE_PATH=$FILE_PATH"/"$category;
echo $FILE_PATH;
minimumsize=1000
for dir in $FILE_PATH$STAR$STAR; do
[[ ! -f "${dir}" ]] && continue # if its NOT a file then skip
actualsize=$(du -k "$dir" | cut -f 1)
if [[ ! $actualsize -ge $minimumsize ]]; then #if file is less than 1mb delete it
echo $dir
echo size is under $minimumsize kilobytes
rm "$dir"
echo deleted
else #for files over 1mb in size convert with ffmpeg
full_file=$(basename $dir)
full_dir=$(dirname $dir)
echo $full_file
echo $full_dir
ffmpeg -i "$dir"
#ffmpeg output file directory needs to be mirrored but with a folder change
read -p "Press any key to resume ..."
fi
done
我想要的 ffmpeg 文件夹输出
/home/videos_test/$category _new/1/vid.mp4
/home/videos_test/$category _new/2/randomvid.mp4
假设:
- 用户提供了一个包含一些视频文件的基本目录 (
read category
)
- 脚本需要将转换后的文件写入新的目录结构,将
${category}
(在目录名中)替换为 ${category}_new
- 问题是:如何给目录名添加
_new
后缀
一种思路是使用参数替换创建新的目标目录名,然后使用mkdir -p
创建新目录:
$ category=videos
$ full_dir="/some/parent/dir/${category}/subdir1/subdir2"
$ echo "${full_dir}"
/some/parent/dir/videos/subdir1/subdir2
$ new_dir="${full_dir//${category}/${category}_new}"
$ echo "${new_dir}"
/some/parent/dir/videos_new/subdir1/subdir2
$ [ -d "${new_dir}" ]
$ echo $?
1 # ${new_dir} does not exist
$ mkdir -p "${new_dir}"
$ [ -d "${new_dir}" ]
$ echo $?
0 # ${new_dir} does exist
path_start=$(echo $dir | rev | cut -d'/' -f4- | rev)
path_end=$(echo $dir | cut -d'/' -f5-)
new=_new
new_path=$path_start/$category$new/$path_end
new_path_no_file=$(echo $new_path | rev | cut -d'/' -f2- | rev)
mkdir -p $new_path_no_file
ffmpeg -i "$dir" -c:v copy -c:a copy -x264opts opencl -movflags +faststart -analyzeduration 2147483647 -probesize 2147483647 -pix_fmt yuv420p "$new_path"
所以我整理了下面的bash脚本,我想修改我抓取的文件路径目录
所以我的布局是这样的
/home/videos_test/categoryname/1/vid.mp4
/home/videos_test/categoryname/2/randomvid.mp4
我想修改它抓取的文件路径,使其看起来像这样
/home/videos_test/changed_folder/1/vid.mp4
/home/videos_test/changed_folder/2/randomvid.mp4
完整剧本
#!/bin/bash
echo "Type the category that you want, followed by [ENTER]:"
read category
export FILE_PATH="/home/videos_test"
export STAR="/*"
FILE_PATH=$FILE_PATH"/"$category;
echo $FILE_PATH;
minimumsize=1000
for dir in $FILE_PATH$STAR$STAR; do
[[ ! -f "${dir}" ]] && continue # if its NOT a file then skip
actualsize=$(du -k "$dir" | cut -f 1)
if [[ ! $actualsize -ge $minimumsize ]]; then #if file is less than 1mb delete it
echo $dir
echo size is under $minimumsize kilobytes
rm "$dir"
echo deleted
else #for files over 1mb in size convert with ffmpeg
full_file=$(basename $dir)
full_dir=$(dirname $dir)
echo $full_file
echo $full_dir
ffmpeg -i "$dir"
#ffmpeg output file directory needs to be mirrored but with a folder change
read -p "Press any key to resume ..."
fi
done
我想要的 ffmpeg 文件夹输出
/home/videos_test/$category _new/1/vid.mp4
/home/videos_test/$category _new/2/randomvid.mp4
假设:
- 用户提供了一个包含一些视频文件的基本目录 (
read category
) - 脚本需要将转换后的文件写入新的目录结构,将
${category}
(在目录名中)替换为${category}_new
- 问题是:如何给目录名添加
_new
后缀
一种思路是使用参数替换创建新的目标目录名,然后使用mkdir -p
创建新目录:
$ category=videos
$ full_dir="/some/parent/dir/${category}/subdir1/subdir2"
$ echo "${full_dir}"
/some/parent/dir/videos/subdir1/subdir2
$ new_dir="${full_dir//${category}/${category}_new}"
$ echo "${new_dir}"
/some/parent/dir/videos_new/subdir1/subdir2
$ [ -d "${new_dir}" ]
$ echo $?
1 # ${new_dir} does not exist
$ mkdir -p "${new_dir}"
$ [ -d "${new_dir}" ]
$ echo $?
0 # ${new_dir} does exist
path_start=$(echo $dir | rev | cut -d'/' -f4- | rev)
path_end=$(echo $dir | cut -d'/' -f5-)
new=_new
new_path=$path_start/$category$new/$path_end
new_path_no_file=$(echo $new_path | rev | cut -d'/' -f2- | rev)
mkdir -p $new_path_no_file
ffmpeg -i "$dir" -c:v copy -c:a copy -x264opts opencl -movflags +faststart -analyzeduration 2147483647 -probesize 2147483647 -pix_fmt yuv420p "$new_path"