【问题标题】:Bash copying files with variablesBash 使用变量复制文件
【发布时间】:2011-04-22 21:10:46
【问题描述】:

新的 bash 脚本,我正在编写一个脚本来将我的电视节目从下载文件夹复制到存档文件夹。

到目前为止,我有这个:

find `*`show1`*`.avi |  cp \"" $0 "\" "/mnt/main/data/tv/Show1" 
find `*`show2`*`.avi |  cp \"" $0 "\" "/mnt/main/data/tv/Show2"

我知道这不是最好的方法,但我的 bash 技能相当有限。

我需要知道如何复制找到的文件,或者如果找不到任何匹配的文件,则什么也不做(这将是一个 cron 脚本)。 例如。

find `*`show1`*`.avi |  cp "show1.hello.world.xvid.avi" "/mnt/main/data/tv/Show1" 
find `*`show2`*`.avi |  cp "show2.foo.bar.xvid.avi" "/mnt/main/data/tv/Show2" 
find `*`show3`*`.avi |  cp "null (nothing found)" "/mnt/main/data/tv/Show3"

谢谢!

编辑:已解决http://pastebin.com/aNLihR86

【问题讨论】:

标签: bash copy find cron


【解决方案1】:

重击 4+

shopt -s globstar
shopt -s nullglob
for file in **/*show*
do
  case "$file" in
   *show1* ) dest="/mnt/main/data/tv/Show1" ;;
   *show2* ) dest="/mnt/main/data/tv/Show2" ;;
   *show3* ) dest="/mnt/main/data/tv/Show2";;
  esac
  cp "$file" "$dest"
done

【讨论】:

    【解决方案2】:
    find . -name "*show1*" -exec cp {} /mnt/main/data/tv/Show1 \;
    

    (将 . 替换为您要查看文件的目录)

    【讨论】:

    • 谢谢,但是它返回:find /my/dir -name show1 -exec cp {} "/mnt/main/data/tv/Show 1" \;查找:show1.pdtv.avi:未知选项
    • 我想你忘记引用名称参数了,应该是:find /my/dir -name "show1" -exec cp {} "/mnt/main /data/tv/Show 1" \;
    • 当我引用它时,它没有找到任何东西(是的,有一个文件)。发现:不匹配
    • 如果你 cp 有 -t 选项(--target-directory),你可以使用 find 提高效率。 -name "show1" -exec cp -t /mnt/main/data/tv/Show1 {} +
    猜你喜欢
    • 2018-10-13
    • 1970-01-01
    • 2020-07-23
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    相关资源
    最近更新 更多