简体   繁体   English

Bash - 移动包含特定字符串的文件

[英]Bash - Move files that contain certain string

I have two folders: Controls and Patients .我有两个文件夹: ControlsPatients Inside each one of these there are several folders for different individuals ( CO1 , CO2 ), with this organization:在每一个里面都有几个文件夹,用于不同的个人( CO1CO2 ),与这个组织:

   Controls

   └───C01
   │   └───ROIS 
   │       └───rs_rois (imgs inside)
   │   └───Cortical_masks
   │       └─── accumbens
   │       └─── putamen
   │       └─── caudatus
   │   
   └───C02
   │   └───ROIS 
   │       └───rs_rois (imgs inside)
   │   └───Cortical_masks
   │       └─── accumbens
   │       └─── putamen
   │       └─── caudatus

And the same for PatientsPatients也是如此

I want to move the imgs from rs_rois to Cortical_masks folder based on a substring of their names.我想根据名称的 ZE83AED3DDF4667DEC0DAAAACB2BB3BE0BZ 将 imgs 从rs_rois移动到Cortical_masks文件夹。 So for example L_accumbens_rsfmri file should go to accumbens folder and R_caudate_rsfmri should go to caudate folder.因此,例如L_accumbens_rsfmri文件应该 go 到accumbens文件夹, R_caudate_rsfmri应该 go 到caudate文件夹。

This is what I have:这就是我所拥有的:

#!/bin/bash
DIR="/media/roy"; cd "$DIR/Analysis" || exit
for group in Controls Patients; do
    for case in "$group"/*; do
        for file in $DIR/Analysis/$case/ROIS2/rs_roi/*; do
            if grep -q accumbens "$file"; then
                mv $file $DIR/Analysis/$case/Cortical_masks/accumbens
            elif grep -q putamen "$file"; then
                mv $file $DIR/Analysis/$case/Cortical_masks/putamen
            elif grep -q caudate "$file"; then
                mv $file $DIR/Analysis/$case/Cortical_masks/caudate
            fi
        done;
    done;
done;

This script doesn´t do anything, why is that?这个脚本没有做任何事情,这是为什么呢? If I add some echo statements, I can see that the for file loop goes through all my files, but the code never gets to the mv statements.如果我添加一些echo语句,我可以看到for file循环遍历了我的所有文件,但代码永远不会到达mv语句。

The problem is that the grep expression you use looks for the string ( accumbens , etc) in the contents of the file instead of in the name of the file.问题是您使用的grep表达式在文件内容而不是文件名中查找字符串( accumbens等)。

Replace each grep by a pattern match like if [[ $file =~ accumbens ]] ...if [[ $file =~ accumbens ]]之类的模式匹配替换每个grep ...

The script then becomes:然后脚本变为:

#!/bin/bash
DIR="/media/roy"; cd "$DIR/Analysis" || exit
for group in Controls Patients; do
    for case in "$group"/*; do
        for file in $DIR/Analysis/$case/ROIS2/rs_roi/*; do
            if [[ $file =~ accumbens ]]; then
                mv $file $DIR/Analysis/$case/Cortical_masks/accumbens
            elif [[ $file =~ putamen ]]; then
                mv $file $DIR/Analysis/$case/Cortical_masks/putamen
            elif [[ $file =~ caudate ]]; then
                mv $file $DIR/Analysis/$case/Cortical_masks/caudate
            fi
        done;
    done;
done;

Use find .使用find

#!/bin/bash
cd /path/to/Analysis/
for group in Control Patients; do
  for case in "$group"/*; do
    orig="$group/$case/ROIS/rs_rois"
    dest="$group/$case/Cortica_masks"
    find "$orig" -type f -name '*accumbens*' -exec mv {} "$dest" \;
    find "$orig" -type f -name '*putamen*' -exec mv {} "$dest" \;
    find "$orig" -type f -name '*caudate*' -exec mv {} "$dest" \;
  done
done

This is a clear way that doesn't need string comparations.这是一种不需要字符串比较的清晰方法。

Would something like this work?像这样的东西会起作用吗?

make a file.txt containing the strings you want制作一个包含所需字符串的 file.txt

cat file.txt

caudate

putamen

Then然后

while read p; do mv $p $DIR/Analysis/$case/Cortical_masks/${p} done < file.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM