Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 根据文件名中的数字移动文件_Shell_Variables_Grep_Mv - Fatal编程技术网

Shell 根据文件名中的数字移动文件

Shell 根据文件名中的数字移动文件,shell,variables,grep,mv,Shell,Variables,Grep,Mv,我正在尝试根据文件夹名称中的数字移动文件夹中的文件。 文件的名称类似于fooNNN\u bar.txt我想把它们组织成/NNN/fooNNN\u bar.txt 这是我现在拥有的。它向我打印每个文件必须移动到的文件夹。我不知道如何收集数字以将其添加到mv命令中。这是正确的方法吗 #!/bin/bash for filename in foo*.txt; do echo "${filename}" | grep -Eo '[0-9]{1,4}'; done 假设您的grep按照您

我正在尝试根据文件夹名称中的数字移动文件夹中的文件。 文件的名称类似于
fooNNN\u bar.txt
我想把它们组织成
/NNN/fooNNN\u bar.txt

这是我现在拥有的。它向我打印每个文件必须移动到的文件夹。我不知道如何收集数字以将其添加到mv命令中。这是正确的方法吗

#!/bin/bash
  for filename in foo*.txt; 
  do 
  echo "${filename}" | grep -Eo '[0-9]{1,4}';
done

假设您的grep按照您的意愿工作:

#!/bin/bash
  for filename in foo*.txt; do 
      num=$(echo "${filename}" | grep -Eo '[0-9]{1,4}')
      mkdir -p "$num"
      mv "$filename" "$num"
done

假设您的grep按照您的意愿工作:

#!/bin/bash
  for filename in foo*.txt; do 
      num=$(echo "${filename}" | grep -Eo '[0-9]{1,4}')
      mkdir -p "$num"
      mv "$filename" "$num"
done

太好了。谢谢。不需要echo、pipe和grep:
num=“${filename//[^0-9]}”
。太好了。谢谢。不需要echo、pipe和grep:
num=“${filename/[^0-9]}”