简体   繁体   English

在Shell Script Linux中重命名文件

[英]Renaming files in Shell Script Linux

I want to rename files I have downloaded from the following script: 我想重命名从以下脚本下载的文件:

exec < input_list.txt
while read line
do
get $line
wget ftp://hgdownload.cse.ucsc.edu/goldenPath/hg19/encodeDCC/$2/$4
# Rename $4
mv $4 $1"_"$3".bam"
done

The input file (input_list.txt) is tab delimited and contains four columns. 输入文件(input_list.txt)由制表符分隔,并包含四列。 The first, $1= name, $2= wget address, $3= factor and $4 is the file name. 第一个是$ 1 =名称,$ 2 = wget地址,$ 3 =因数,$ 4是文件名。

A549    wgEncodeBroadHistone    H2azDex100nm    wgEncodeBroadHistoneA549H2azDex100nmAlnRep1.bam

I want to rename $4 (the file that has been downloaded) to a shorter file name that only includes the corresponding $1 and $3 terms. 我想将$ 4(已下载的文件)重命名为较短的文件名,该文件名仅包含相应的$ 1​​和$ 3条款。 For example, wgEncodeBroadHistoneA549H2azDex100nmAlnRep1.bam becomes A549_H2azDex100nm.bam 例如, wgEncodeBroadHistoneA549H2azDex100nmAlnRep1.bam变为A549_H2azDex100nm.bam

I've played around with " but I keep getting error messages for the mv command and that $4 is a bad variable name. Any suggestions would be greatly appreciated. 我玩过"但我不断收到有关mv命令的错误消息,并且$4是一个错误的变量名。任何建议将不胜感激。

You don't need to rename the file if you use wget's -O option: 如果使用wget的-O选项,则无需重命名文件:

#!/bin/bash

[ -n "$BASH_VERSION" ] || {
    echo "You need Bash to run this script."
    exit 1
}

while IFS=$'\t' read -a INPUT; do
    wget -O "${INPUT[0]}_${INPUT[2]}.bam" "ftp://hgdownload.cse.ucsc.edu/goldenPath/hg19/encodeDCC/${INPUT[1]}/${INPUT[3]}"
done < input_list.txt

Make sure you save the file in UNIX file format like script.sh and run bash script.sh . 确保以UNIX文件格式(如script.sh保存文件,然后运行bash script.sh

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

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