简体   繁体   English

linux创建目录并将相应文件移动到目录

[英]linux create directories and move corresponding files to the directories

I have a text file listed the directory names and what files should included inside. 我有一个文本文件,列出了目录名称以及应包含的文件。

my text file:
SRS000111    ERR1045156     
SRS000112    ERR1045188     
SRS000123    ERR1045204     
SRS000134    ERR1045237  ERR1045238  ERR1045239
SRS000154    ERR1045255  ERR1045256 
SRS000168    ERR1045260  ERR1045261  ERR1045262
...          ...         ...
SRS001567    ERR1547451  ERR1547676

now I want to create all the directories using the first column of the text file but I don't know how to do the for loop. 现在,我想使用文本文件的第一列创建所有目录,但是我不知道如何执行for循环。

for filename in cat file.txt | awk -F, '{print $1}'; do mkdir ${filename}; done

but it goes to error. 但这会出错。

second I have all the ERR files and I want to move them to the corresponding directories according to the text file. 第二,我拥有所有的ERR文件,我想根据文本文件将其移动到相应的目录。 I have not any idea how to do this part. 我不知道该怎么做。

You have to make system calls from awk for mkdir and mv files 您必须从awk进行mkdirmv文件的system调用

This awk would do 这个awk会做

awk 'FNR>1{system("mkdir \"" $1 "\""); for(i=2; i<=NF; i++) system("mv \"" $i "\" " "\"" $1 "\"")}' file

FNR>1 because we don't want to create directory for first line ie header names in your CSV file FNR>1因为我们不想为第一行创建目录,即您的CSV文件中的标题名称

Note : Run this command from the directory where all the filenames as mentioned in your source/input file are present. 注意:从存在您的源/输入文件中提到的所有文件名的目录中运行此命令。 This will create directories there itself and will move all the files in those newly created directories. 这将在其中创建目录,并将所有文件移动到这些新创建的目录中。

I recommend you read the file, split the folder name columns and file name columns and makes de directories and the movements. 我建议您阅读文件,拆分文件夹名称列和文件名称列,并进行目录和移动。

This script makes it: 该脚本使其:

#!/bin/bash
while IFS='' read -r line || [[ -n "$line" ]]; do
  dir=$(echo "$line" | awk '{print $1}')
  files=$(echo "$line" | awk '{$1=""; print $0}')

  mkdir $dir
  mv $files $dir/
done < myfile.txt

Is not to complicated but if you have questions about it you can make me any question 并不复杂,但是如果您有任何疑问,可以问我任何问题

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

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