如何根据文件名和计数文件将文件移动到目录中

时间:2015-03-06 06:52:39

标签: bash command-line ipython-notebook

我已经看到其他主题与我的问题有些相似,但我仍然是初学者,无法真正理解发布的一些代码。

我有一个目录列表,它遵循其中一种格式,但最后的数字不一样,缩写(UVM)可以来自缩写列表。

jhu-usc.edu_UVM.HumanMethylation450.Level_1.1.0.0/ jhu-usc.edu_UVM.HumanMethylation450.aux.1.0.0/ jhu-usc.edu_UVM.HumanMethylation450.mage-tab.1.0.0/

我希望制作一个脚本,根据缩写(ex.UVM)将目录及其文件从当前目录递归移动到新目录(如果还没有,则创建目录)。

然后我希望能够计算目录中以* idat结尾的文件数,并输出一个.txt文件,上面写着“For abbreviation > idat文件。

我没有太多时间去讨论这件事,我的截止日期即将到来。如果有人能帮助我,我会非常感激。

如果问题措辞或格式不正确,请原谅我,这是我的第一篇文章,所以我尽我所能。

谢谢!

2 个答案:

答案 0 :(得分:0)

这样的东西?

#!/bin/sh

#Definition of where you want the directory to be moved
destination_root="/tmp/stuff"

 function abbreviation() {

    #Default
    destination="${destination_root}/UNKNOWN"

    if [[ $1 == UVM* ]]; then
            destination="${destination_root}/UVMFOLDER"
    fi

    if [[ $1 == BRCA* ]]; then
            destination="${destination_root}/BRCAFOLDER"
    fi

    #Ensure folder is present
    mkdir -p ${destination} 
}


#Loop through all folders matching the prefix
for instance in jhu-usc.edu_*
do
    #Count instances of *idat files and put the result in a.txt file
    echo "${instance} - `find ${instance} -type f -name "*idat" | wc -l`" | tee -a a.txt

    abbreviation `echo "${instance}" | sed s/jhu-usc.edu_//g`

    #Move the folder to the destination
    mv ${instance} ${destination}
done

这将创建一个包含以下内容的a.txt:

jhu-usc.edu_UVM.HumanMethylation450.Level_1.1.0.0 - 3
jhu-usc.edu_UVM.HumanMethylation450.aux.1.0.0 - 2

其中3和2是文件夹中以idat结尾的文件的实例数。

修改

更改了移动文件的输出文件夹 - 我删除了前缀jhu-usc.edu_。例如,jhu-usc.edu_UVM.HumanMethylation450.Level_1.1.0.0将被移动到/tmp/stuff/UVM.HumanMethylation450.Level_1.1.0.0

答案 1 :(得分:0)

#!/bin/bash
#Definition of where you want the directory to be moved
destination_root="/data/nrnb01_nobackup/agross/TCGA_methylation"

#Delete tar.gz files
find . -type f -name '*tar.gz' -exec rm {} +

#Move all files within cancersubtype folders into current directory to allow ease of moving to specific directories
find . -mindepth 2 -type f -print -exec mv {} . \;

function abbreviation() {
    #Default
    destination="${destination_root}/UNKNOWN"

    if [[ $1 == UVM* ]]; then
            destination="${destination_root}/UVMFOLDER"
    fi

    if [[ $1 == BRCA* ]]; then
            destination="${destination_root}/BRCAFOLDER"
    fi

    if [[ $1 == ACC* ]]; then
            destination="${destination_root}/ACCFOLDER"
    fi

    if [[ $1 == BLCA* ]]; then
            destination="${destination_root}/BLCAFOLDER"
    fi

    if [[ $1 == CESC* ]]; then
            destination="${destination_root}/CESCFOLDER"
    fi

    if [[ $1 == CHOL* ]]; then
            destination="${destination_root}/CHOLFOLDER"
    fi

    if [[ $1 == COAD* ]]; then
            destination="${destination_root}/COADFOLDER"
    fi

    if [[ $1 == DLBC* ]]; then
            destination="${destination_root}/DLBCFOLDER"
    fi

    if [[ $1 == ESCA* ]]; then
            destination="${destination_root}/ESCAFOLDER"
    fi

    if [[ $1 == GBM* ]]; then
            destination="${destination_root}/GBMFOLDER"
    fi

    if [[ $1 == HNSC* ]]; then
            destination="${destination_root}/HNSCFOLDER"
    fi

    if [[ $1 == KICH* ]]; then
            destination="${destination_root}/KICHFOLDER"
    fi

    if [[ $1 == KIRC* ]]; then
            destination="${destination_root}/KIRCFOLDER"
    fi

    if [[ $1 == KIRP* ]]; then
            destination="${destination_root}/KIRPFOLDER"
    fi

    if [[ $1 == LAML* ]]; then
            destination="${destination_root}/LAMLFOLDER"
    fi

    if [[ $1 == LGG* ]]; then
            destination="${destination_root}/LGGFOLDER"
    fi

    if [[ $1 == LIHC* ]]; then
            destination="${destination_root}/LIHCFOLDER"
    fi

    if [[ $1 == LUAD* ]]; then
            destination="${destination_root}/LUADFOLDER"
    fi

    if [[ $1 == LUSC* ]]; then
            destination="${destination_root}/LUSCFOLDER"
    fi

    if [[ $1 == MESO* ]]; then
            destination="${destination_root}/MESOFOLDER"
    fi

    if [[ $1 == OV* ]]; then
            destination="${destination_root}/OVFOLDER"
    fi

    if [[ $1 == PAAD* ]]; then
            destination="${destination_root}/PAADFOLDER"
    fi

    if [[ $1 == PCPG* ]]; then
            destination="${destination_root}/PCPGFOLDER"
    fi

    if [[ $1 == PRAD* ]]; then
            destination="${destination_root}/PRADFOLDER"
    fi

    if [[ $1 == READ* ]]; then
            destination="${destination_root}/READFOLDER"
    fi

    if [[ $1 == SARC* ]]; then
            destination="${destination_root}/SARCFOLDER"
    fi

    if [[ $1 == SKCM* ]]; then
            destination="${destination_root}/SKCMFOLDER"
    fi

    if [[ $1 == STAD* ]]; then
            destination="${destination_root}/STADFOLDER"
    fi

    if [[ $1 == TGCT* ]]; then
            destination="${destination_root}/TGCTFOLDER"
    fi

    if [[ $1 == THCA* ]]; then
            destination="${destination_root}/THCAFOLDER"
    fi

    if [[ $1 == THYM* ]]; then
            destination="${destination_root}/THYMFOLDER"
    fi

    if [[ $1 == UCEC* ]]; then
            destination="${destination_root}/UCECFOLDER"
    fi

    if [[ $1 == UCS* ]]; then
            destination="${destination_root}/UCSFOLDER"
    fi


    #Ensure folder is present
    mkdir -p ${destination} 
}

#Loop through all folders matching the jhu prefix
for instance in jhu-usc.edu_*
do
    #Count instances of *idat files and put the result in idat_count.txt file
    echo "${instance} - `find ${instance} -type f -name "*idat" | wc -l`" | tee -a idat_count.txt

    abbreviation `echo "${instance}" | sed s/jhu-usc.edu_//g`

    #Move the folder to the destination
    mv ${instance} ${destination}
done