简体   繁体   English

从Linux中的两个文件内容重命名文件名

[英]Renaming file names from two files content in linux

I am having basic knowledge in Linux and need your help to develop concept for following requirement. 我具有Linux的基础知识,需要您的帮助来开发满足以下要求的概念。 I have two files FILE_NAMES.txt and FILE_NAMES_TS.txt. 我有两个文件FILE_NAMES.txt和FILE_NAMES_TS.txt。 The content of files as below. 文件内容如下。

FILE_NAMES.txt
====
jan_
feb_
mar_

FILE_NAMES_TS.txt
====
jan_20170921.csv
feb_20170921.csv
mar_20170921.csv

All the above content are nothing but file names and are placed in db_views/data directory. 以上所有内容不过是文件名而已,并放在db_views / data目录中。 For the content of FILE_NAMES_TS.txt are 0 bytes initially. 对于FILE_NAMES_TS.txt的内容,初始为0字节。 Now have to move / rename from jan_ to jan_20170921.csv , and so on. 现在必须从jan_移动/重命名为jan_20170921.csv ,依此类推。 That means the file jan_20170921.txt is now non-zero bytes. 这意味着文件jan_20170921.txt现在为非零字节。 Please help. 请帮忙。

You can use readarray bash function to read files into 2 arrays and then use mv command to rename each file 您可以使用readarray bash函数将文件读取为2个数组,然后使用mv命令重命名每个文件

#!/bin/bash

readarray a < names.txt
readarray b < names_ts.txt

len=${#a[@]}

echo $len

for (( i=0; i<${len}; i++ ));
do
    echo mv ${a[$i]} ${b[$i]}
done

您可以扩展特殊的文件描述符技术,以同时读取两个文件,然后读取mv

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

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