Bash If语句来查找文件

Bash If语句来查找文件,bash,Bash,获取错误第20行:cd:-9:无效选项 cd:用法:cd[-L |[-P[-e]]][dir] mv:无效选项--“9” 请尝试“mv--help”了解更多信息。我不是bash专家,但有几种方法可以做到这一点 您可以将其他案例合并到文件中 您可以开发其他匹配器来覆盖您想要的情况,并依次检查每个匹配器 第二个的伪代码: #!/bin/bash # This would match files that begin with YYYYMMDD format. files=(*[0-9][0-9

获取错误第20行:cd:-9:无效选项 cd:用法:cd[-L |[-P[-e]]][dir] mv:无效选项--“9”
请尝试“mv--help”了解更多信息。

我不是bash专家,但有几种方法可以做到这一点

  • 您可以将其他案例合并到
    文件中
  • 您可以开发其他匹配器来覆盖您想要的情况,并依次检查每个匹配器
第二个的伪代码:

#!/bin/bash

# This would match files that begin with YYYYMMDD format.
files=(*[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].wav)

# If you want to match those in the current year, start it with that year instead.
# current_year=$(date +%Y)
# files=("$current_year"[0-9][0-9][0-9][0-9]*)

#expands its values to multiple arguments '${files[@]}'
for file in ${files[@]}; do
file_date=${file:(-12):8}
file_year=${file_date:0:4}  
file_month=${file_date:4:2}

#  Adding -p option to mkdir would create the directory only if it doesn't exist.
  mkdir -p "$file_year"

  file_month=${file:4:2}
  cd  $file_year
  mkdir -p "$file_month"
  cd ..
  mv $files "$file_year"/"$file_month"

done
#/bin/bash
#如果找不到匹配项,则不显示任何内容。
shopt-s nullglob
#匹配包含日期格式的所有.wav文件。
文件=(*[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]*.wav)
如果[${#files[@]}-eq 0]];然后
echo“未找到匹配项。”
fi
对于“${files[@]}”中的文件;做
#我们一部分一部分地得到日期
文件\u日期=“”
#把它分成几部分。

IFS=“-”在编辑原始问题之前,阅读-ra部分,您有一些文件名如下:
OUTxxx-20130913.wav
。基于此文件名,您可以执行以下操作。它还适用于文件名
qxxxx-20130913.wav
xxxx-20130913.wav

#!/bin/bash

# When a match is not found, just present nothing.
shopt -s nullglob

# Match all .wav files containing the date format.
files=(*[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]*.wav)

if [[ ${#files[@]} -eq 0 ]]; then
    echo "No match found."
fi

for file in "${files[@]}"; do
    # We get the date part by part
    file_date=''
    # Split it into parts.
    IFS="-." read -ra parts <<< "$file"
    for t in "${parts[@]}"; do
            # Break from the loop if a match is found
        if [[ $t == [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] ]]; then
            file_date=$t
            break
        fi
    done
    # If a value was not assigned, then show an error message and continue to the next file.
    if [[ -z $file_date ]]; then
        echo "Unable to find date string in $file."
        continue
    fi

    file_year=${file_date:0:4}
    file_month=${file_date:4:2}

    mkdir -p "$file_year/$file_month"

    # -- is just there to not interpret filenames starting with - as options.
    echo mv -- "$file" "$file_year/$file_month"
done

为“${files[@]}”中的文件引用
;执行
cd“$file\u year”
如果我使用您的it,这些错误mkdir:无效选项--“9”请尝试
mkdir--help”以获取更多信息/years.sh:第20行:cd:-9:无效选项cd:用法:cd[-L |[-P[-e]]]][dir]mv:无效选项--“9”请尝试
mv--help”以了解详细信息。@Ryan您应该在双引号周围正确地引用变量,以防止出现错误。另外,在您的mv命令中,我注意到您仍然在使用变量
“$files”
,而不仅仅是
“$file”
。我正在使用您刚才通过复制和粘贴编写的代码,并获得了此…mkdir--help”以获取更多信息/years.sh:第20行:cd:-9:无效选项cd:用法:cd[-L |[-P[-e]]]][dir]mv:无效选项--'9'请尝试mv--help'了解详细信息。。。。如果我这样做“我怎么才能拿到qrongones@Ryan我做了一个更新。您可以再次运行它并在中显示输出吗?我想看看这些扩展是如何不按预期工作的。该死的,你很好。你想要什么好的基础书?加上它的魅力!如果你在这里,我会请你吃晚饭,洛莉不会想做一个ls statement,因为我有很多这样的文件,你可以用
查找
替换
\ls-1
。编辑了我的答案。
#!/bin/bash

# When a match is not found, just present nothing.
shopt -s nullglob

# Match all .wav files containing the date format.
files=(*[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]*.wav)

if [[ ${#files[@]} -eq 0 ]]; then
    echo "No match found."
fi

for file in "${files[@]}"; do
    # We get the date part by part
    file_date=''
    # Split it into parts.
    IFS="-." read -ra parts <<< "$file"
    for t in "${parts[@]}"; do
            # Break from the loop if a match is found
        if [[ $t == [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] ]]; then
            file_date=$t
            break
        fi
    done
    # If a value was not assigned, then show an error message and continue to the next file.
    if [[ -z $file_date ]]; then
        echo "Unable to find date string in $file."
        continue
    fi

    file_year=${file_date:0:4}
    file_month=${file_date:4:2}

    mkdir -p "$file_year/$file_month"

    # -- is just there to not interpret filenames starting with - as options.
    echo mv -- "$file" "$file_year/$file_month"
done
IFS=$(echo -en "\n\b");
for file in $(find . -type f -name "*.wav"); 
do 
  fn=$(echo $file | sed -e 's/.*- //g' -e 's/\.wav//g');
  fyear=${fn:0:4};
  fmonth=${fn:4:2};
  fdate=${fn:6:2};
  echo $fyear; echo $fmonth;echo $fdate;
  mkdir -p "$fyear"/"$fmonth";
  mv $fn "$fyear"/"$fmonth";
done