shell Bash脚本更改动态变量上的部分文件夹路径

30byixjq  于 2022-11-16  发布在  Shell
关注(0)|答案(2)|浏览(141)

因此,我将下面的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
svmlkihl

svmlkihl1#

假设条件:

  • 用户提供包含一些视频文件的基目录(read category
  • 脚本需要将转换后的文件写入新的目录结构,用${category}_new替换${category}(在目录名中
  • 问题是:如何在目录名中添加_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
dddzy1tm

dddzy1tm2#

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"

相关问题