Chinaunix

标题: 请问大神,请问linux下按照一定规则批量修改文件夹名该怎么办?谢谢!!!! [打印本页]

作者: gbird2    时间: 2016-08-15 16:57
标题: 请问大神,请问linux下按照一定规则批量修改文件夹名该怎么办?谢谢!!!!
比如我有这些文件:
/home/yeoman/first/afjldajflfda/a.nii
/home/yeoman/first/askajdf/a.nii
/home/yeoman/first/qwwerwe/a.nii
/home/yeoman/first/jlkxcv/a.nii
/home/yeoman/first/asdfkcc/a.nii
等等类似很多文件,所有的第四级目录名称放在subject.txt里,

我想把他们统一移动并修改成:
/home/yeoman/second/sub0001/a.nii
/home/yeoman/second/sub0002/a.nii
/home/yeoman/second/sub0003/a.nii
/home/yeoman/second/sub0004/a.nii
/home/yeoman/second/sub0005/a.nii
等等。所有的第四级目录名称放在result.txt里,
我写的脚本是个死循环,运行不了。。。请问怎么解决啊,谢谢!!!

#! /bin/bash bash
rawdatadir=/home/yeoman/first
outputdir=/home/yeoman/second

hc_list=/home/yeoman/first/subject.txt
sub_list=/home/yeoman/first/result.txt

hc=$( cat ${hc_list})
subjects=$( cat ${sub_list})

while read hc subject

do
set i $hc
set j $subjects
cp ${rawdatadir}/$i/anat/*.nii.gz ${outputdir}/$j/anat
done

作者: gbird2    时间: 2016-08-15 17:37
最初的目的其实就是直接改那个文件夹的名字,把第四级文件夹统一按顺序改成sub0001到sub0005就行了。。  
作者: moperyblue    时间: 2016-08-15 21:08
本帖最后由 moperyblue 于 2016-08-15 21:10 编辑

ls
  1. 1.sh  result.txt  subject.txt
复制代码
more *
  1. ::::::::::::::
  2. 1.sh
  3. ::::::::::::::
  4. #!/bin/bash

  5. rawdatadir=/home/yeoman/first
  6. outputdir=/home/yeoman/second

  7. exec 3<subject.txt 4<result.txt   #注: subject.txt 、result.txt 、1.sh 三个文件都在同一目录
  8. while read src<&3 && read dest<&4
  9. do
  10.     mv "$rawdatadir/$src" "$rawdatadir/$dest"
  11. done
  12. mv "$rawdatadir/"* "$outputdir"
  13. exec 3<&- && exec 4<&-
  14. ::::::::::::::
  15. result.txt
  16. ::::::::::::::
  17. sub0001
  18. sub0002
  19. sub0003
  20. sub0004
  21. sub0005
  22. ::::::::::::::
  23. subject.txt
  24. ::::::::::::::
  25. afjldajflfda
  26. askajdf
  27. qwwerwe
  28. jlkxcv
  29. asdfkcc
复制代码

作者: gbird2    时间: 2016-08-16 16:32
回复 3# moperyblue


   非常非常感谢!!!!!!!!!!!!!!!!!!!
作者: karma303    时间: 2016-08-23 21:43
本帖最后由 karma303 于 2016-08-23 21:50 编辑

楼主你好,借你的帖子一用。

下面是我所理解的你的意思:
1,你只是想把first目录下的一部分目录按照subxxxx重命名,而不是所有。否则subject.txt就是多余的。
2,你的 result.txt 似乎是多余的。

以下是lua shell的解决方式。

$` tree ./home
./home
└── yeoman
    ├── first
    │   ├── afjldajflfda
    │   │   └── a.nii
    │   ├── askajdf
    │   │   └── a.nii
    │   └── qwwerwe
    │       └── a.nii
    └── second

6 directories, 3 files

$` cat ./subject.txt
afjldajflfda
askajdf
qwwerwe

$` cat ./mvdir.lua
  1. #!/bin/lsh

  2. file = io.open("subject.txt", "r")

  3. local i = 0
  4. for dirname in file:lines() do
  5.         local newname = string.format("sub%04d", i)
  6.         `mv home/yeoman/first/$(dirname)  home/yeoman/second/$(newname)
  7.         i = i + 1
  8. end
复制代码



$` ./mvdir.lua
$` tree home
home
└── yeoman
    ├── first
    └── second
        ├── sub0000
        │   └── a.nii
        ├── sub0001
        │   └── a.nii
        └── sub0002
            └── a.nii
6 directories, 3 files





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2