【问题标题】:Error - script move files related to name file inside folder错误 - 脚本移动与文件夹内名称文件相关的文件
【发布时间】:2022-07-26 22:10:56
【问题描述】:

大家好,我正在构建一个脚本来订购与我的研究文件相关的文件,但我不明白为什么提示会给我这个错误

错误 1.1

mv: cannot stat 'filefilefilefilefilefilefilefilefilefilefilefile.pdf'$'\n': File name too long  

这意味着我必须重命名所有长文件?是否存在其他方法来防止此错误? 下面的例子是产生错误的脚本

脚本 1 - 将所有包含业务的 greped 文件移到其名称文件中,并将它们移到 auto_folder_business 中


mkdir -p /mnt/c/Users/alber/Desktop/testfileorder/auto_folder_business
ls /mnt/c/Users/alber/Desktop/testfileorder | egrep -i 'business.' | xargs -0 -I '{}' mv '{}' /mnt/c/Users/alber/Desktop/testfileorder/auto_folder_business

在上面的示例中,我也遇到了其他错误

错误 1.2

xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option

我解决了插入 -0 选项, 尽管如此,我试图概括这个过程写这个sn-p

脚本 2 - 将所有包含插入关键字的 greped 文件移动到其名称文件中,并将它们移动到 auto_folder_business 中

#!/bin/sh
read -p "file to order: --> " fetching_keyword

mypath=/mnt/c/Users/alber/Desktop/testfileorder/auto_folder_$fetching_keyword/

echo $mypath

mkdir -p $mypath

ls /mnt/c/Users/alber/Desktop/testfileorder | 
egrep -i "$fetching_keyword" | 
xargs -0 -I {} mv -n {} $mypath

这里我还有一个错误,我认为它们是相关的

错误 2

mv: cannot stat 'Statino (1).pdf'$'\n''Statino (2).pdf'$'\n''Statino (3).pdf'$'\n''Statino (4).pdf'$'\n''Statino.pdf'$'\n''auto_folder_statino'$'\n': No such file or directory
xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option

我不明白我做错了什么......

【问题讨论】:

    标签: bash file automation script productivity-power-tools


    【解决方案1】:

    xargs -0 期望从标准输入中读取以空值结尾的行。 lsegrep 都在编写以换行符结尾的行,因此它们的整个输出在此处被作为单个输入读取。

    最快的解决方法是使用find -print0

    find /mnt/c/Users/alber/Desktop/testfileorder -iname "*${fetching_keyword}*" -print0 | \
    xargs -0 -I {} mv -n {} "$mypath"
    

    ...但在这种特定情况下,您可能只想在此时使用 -exec。

    find /mnt/c/Users/alber/Desktop/testfileorder -iname "*${fetching_keyword}*" \
     -exec mv -n {} "${mypath}" \;
    

    【讨论】:

      猜你喜欢
      • 2021-12-26
      • 1970-01-01
      • 1970-01-01
      • 2013-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-22
      • 1970-01-01
      相关资源
      最近更新 更多