根据文件名中的数字移动文件 [英] Move files according to number in filename

查看:100
本文介绍了根据文件名中的数字移动文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图根据名称中的数字移动文件夹中的文件。
文件名称如 fooNNN_bar.txt 我想将它们组织为 /NNN/fooNNN_bar.txt

I am trying to move files in folders according to a number in their names. Files are names like fooNNN_bar.txt I would like to organise them like /NNN/fooNNN_bar.txt

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

Here is what I have for now. It prints me the folder each file would have to move to. I'm not sure how to collect the number to add it into a mv command. Is this even the correct way to do it?

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


推荐答案

想要:

Assuming your grep works as you want:

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

这篇关于根据文件名中的数字移动文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆