【问题标题】:Bash script changing part of folder path on dynamic variableBash脚本在动态变量上更改部分文件夹路径
【发布时间】:2021-11-13 00:43:54
【问题描述】:

所以我整理了以下 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

【问题讨论】:

  • 有什么问题吗?脚本运行吗?它会产生错误吗(如果是,错误是什么)?脚本是否做“错误”的事情(如果是的话,它做“错误”的原因是什么)?提供了什么作为 $category 变量的值...categorynamechange_folder 或其他?
  • 读取类别是用户输入的内容,我只是希望 ffmpeg 输出文件类似于用户提供的类别名称,但修改后附加了 _new 之类的内容
  • 抱歉,仍然不明白您在问什么...您是否正在寻求帮助修改full_dir 变量以包含_new 后缀,或者如何重定向所有@987654330 @ 输出到不同的目录?
  • 我想通了并发布了我的解决方案作为答案,我试图将 $dir 变量修改为 ffmpeg 将文件输出到的新路径
  • 好的,所以你只想添加_new 后缀...很容易进行参数替换(假设字符串$category 只在$full_dir 中出现一次)

标签: bash shell file directory script


【解决方案1】:
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"

【讨论】:

    【解决方案2】:

    假设:

    • 用户提供包含一些视频文件的基本目录 (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
    

    【讨论】:

      猜你喜欢
      • 2022-10-13
      • 2015-03-30
      • 2011-02-05
      • 2013-03-23
      • 2021-12-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-27
      • 1970-01-01
      相关资源
      最近更新 更多