Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.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
Linux 创建文件夹和移动文件的bash脚本_Linux_Bash_Shell - Fatal编程技术网

Linux 创建文件夹和移动文件的bash脚本

Linux 创建文件夹和移动文件的bash脚本,linux,bash,shell,Linux,Bash,Shell,我有许多模拟创建的文件。 像这样:res_00001.root到res_09999.root 我想创建一系列文件夹,根据我们正在移动的文件名,将1000个文件按顺序批量移动到新创建的文件夹中。e、 g.folder1将包含res_00001.root到res_00999.root,folder2 res_01000.root到res_01999.root 我试图创建一个脚本,但它不起作用: #!/bin/bash N_files=$1 for (( file=0; file<$N_file

我有许多模拟创建的文件。
像这样:res_00001.root到res_09999.root

我想创建一系列文件夹,根据我们正在移动的文件名,将1000个文件按顺序批量移动到新创建的文件夹中。e、 g.folder1将包含res_00001.root到res_00999.root,folder2 res_01000.root到res_01999.root

我试图创建一个脚本,但它不起作用:

#!/bin/bash
N_files=$1
for (( file=0; file<$N_files; ++file )) do #state what file I am looking at
  s=file%1000 INPUT=printf data/output_%04lu.root $file` OUTPUT=printf data/folder%02lu/res_%04lu.root $s # move the files
  mv INPUT OUTPUT
done`
#/bin/bash
N_文件=$1

对于((file=0;file更新的答案)

如果找不到
重命名
程序,可以运行这个小脚本-先备份

#!/bin/bash
shopt -s nullglob nocaseglob
for f in *.root; do
   n=$(tr -dc '[0-9]' <<< $f)
   ((d=(10#$n/1000)+1))
   [ ! -d folder$d ] && mkdir folder$d
   echo mv "$f" folder$d/$f
done
样本输出

'res_00001.root' would be renamed to 'folder1/res_00001.root'
'res_00002.root' would be renamed to 'folder1/res_00002.root'
'res_00003.root' would be renamed to 'folder1/res_00003.root'
'res_00004.root' would be renamed to 'folder1/res_00004.root'
'res_00005.root' would be renamed to 'folder1/res_00005.root'
...
...
'res_00997.root' would be renamed to 'folder1/res_00997.root'
'res_00998.root' would be renamed to 'folder1/res_00998.root'
'res_00999.root' would be renamed to 'folder1/res_00999.root'
'res_01000.root' would be renamed to 'folder2/res_01000.root'
'res_01001.root' would be renamed to 'folder2/res_01001.root'
'res_01002.root' would be renamed to 'folder2/res_01002.root'
'res_01003.root' would be renamed to 'folder2/res_01003.root'
...
...
如果它看起来不错,请删除
--dry run
,这样它实际上可以做一些事情,而不仅仅是说它可以做什么

  • s/[^0-9]//g
    删除文件名中任何非数字的内容
  • my$d=int($\u/1000)+1
    计算目录名
  • $\uxAE=“folder$d/res\u$\ uxAE.root”
    生成输出文件名

更新答案

如果找不到
重命名
程序,可以运行这个小脚本-先备份

#!/bin/bash
shopt -s nullglob nocaseglob
for f in *.root; do
   n=$(tr -dc '[0-9]' <<< $f)
   ((d=(10#$n/1000)+1))
   [ ! -d folder$d ] && mkdir folder$d
   echo mv "$f" folder$d/$f
done
样本输出

'res_00001.root' would be renamed to 'folder1/res_00001.root'
'res_00002.root' would be renamed to 'folder1/res_00002.root'
'res_00003.root' would be renamed to 'folder1/res_00003.root'
'res_00004.root' would be renamed to 'folder1/res_00004.root'
'res_00005.root' would be renamed to 'folder1/res_00005.root'
...
...
'res_00997.root' would be renamed to 'folder1/res_00997.root'
'res_00998.root' would be renamed to 'folder1/res_00998.root'
'res_00999.root' would be renamed to 'folder1/res_00999.root'
'res_01000.root' would be renamed to 'folder2/res_01000.root'
'res_01001.root' would be renamed to 'folder2/res_01001.root'
'res_01002.root' would be renamed to 'folder2/res_01002.root'
'res_01003.root' would be renamed to 'folder2/res_01003.root'
...
...
如果它看起来不错,请删除
--dry run
,这样它实际上可以做一些事情,而不仅仅是说它可以做什么

  • s/[^0-9]//g
    删除文件名中任何非数字的内容
  • my$d=int($\u/1000)+1
    计算目录名
  • $\uxAE=“folder$d/res\u$\ uxAE.root”
    生成输出文件名


请准确地向我们展示您尝试过的内容,而不是阅读材料的链接也不是代码编写服务,请提供错误/不起作用的代码,以便我们帮助您解决问题。
#!/bin/bash N_files=$1 for((file=0;fileSorry,格式错误。您可能需要编辑答案并放入代码()这里。请告诉我们您到底尝试了什么,而不是阅读材料的链接。这也不是代码编写服务,请提供错误/不起作用的代码,以便我们可以帮助您解决问题。
#!/bin/bash N_files=$1 for((file=0;fileSorry关于此,格式错误。您可能需要编辑答案并放入代码()好了。谢谢。我在终端上以一行程序的形式运行了这个,但我没有看到任何文件夹生成。如果我在.././数据中,我希望看到数据/folder1、数据/folder2等。它运行时没有注释,但没有生成任何文件夹。我确实删除了--dry run,我还应该提到我正在Centos7上运行,看起来我已经重命名了(1) 使用选项-v verbose,-s symlink,-h help,-v Version。我没有用于干运行的-n。我没有sudo。您可以使用
mkdir文件夹{1..10}预先创建目录
。不确定Centos上有哪个软件包
rename
可能是
moreutils
。它只是一个Perl脚本。它有时被称为
prename
…Perl rename.prename不存在。它并不讨厌这个命令,但它在这台机器上不起任何作用。我创建了folder1、folder2,…我正在运行的文件子集ng这个是res_5000.root(填充更改,所以我更新了s/[^0-8]//g)这应该进入我创建的folder6中。谢谢。我在终端上以一行程序的形式运行了它,但我没有看到任何正在生成的文件夹。如果我在.././数据中,我希望看到数据/folder1、数据/folder2等。它运行时没有注释,但没有生成文件夹。我确实删除了--dry run,我还应该提到我正在运行g在Centos7上,看起来我用选项-v verbose,-s symlink,-h help,-v Version重命名了(1)。我没有-n用于干运行。我没有sudo。你可以使用
mkdir文件夹{1..10}将目录放在你自己的前面
。不确定Centos上有哪个软件包
rename
可能是
moreutils
。它只是一个Perl脚本。它有时被称为
prename
…Perl rename.prename不存在。它并不讨厌这个命令,但它在这台机器上不起任何作用。我创建了folder1、folder2,…我正在运行的文件子集ng这个是res_5000.root(填充更改,所以我更新了s/[^0-8]//g),它应该进入我创建的folder6中。