简体   繁体   English

Bash:读取文件并移至子目录

[英]Bash: reading files and moving into sub-directory

I have around 1000 files (png) and need to move them into the corresponding directory and their sub-directory. 我大约有1000个文件(png),需要将它们移动到相应的目录及其子目录中。

I do have 26 directories (A - Z) and below each directory the complete alphabet AZ again. 我确实有26个目录(A-Z),并且在每个目录下又有完整的字母AZ。 File names are 6 characters/digits long and have a png extension, egeg AH2BC0.png 文件名长度为6个字符/数字,并具有png扩展名,例如AH2BC0.png

I would need to move the file AH2BC0.png into the directory A and within that directory into the sub-directory H, egA->H->AH2BC0.png. 我需要将文件AH2BC0.png移至目录A,并将该目录内的子目录移至子目录H,例如A-> H-> AH2BC0.png。

I have created following script which is not really working as expected: 我创建了以下脚本,该脚本实际上无法按预期工作:

#!/bin/bash
ls >LISTE.txt
for i in LISTE.txt; do
a=$(cat $i | cut -b 1 | tr '[:lower:]' '[:upper:]')
b=$(cat $i | cut -b 2 | tr '[:lower:]' '[:upper:]')
mkdir -p $a/$b
cat $i | xargs mv $a/$b
rm $i
done

Problem is that a) the sub-directory is not created and b) the files are not moved. 问题是a)未创建子目录,b)文件未移动。 Any suggestions or better ideas for the script? 对脚本有什么建议或更好的主意吗?

Thanks 谢谢

PS: I guess it's obvious that it's quite some years ago that I have created any bash scripts or coded so please bear with me. PS:我想很明显,很早以前我已经创建了任何bash脚本或编码,所以请耐心等待。 PSS: working on MAC OSX bash 3.2 PSS:在MAC OSX bash 3.2上工作

#!/bin/bash
for item in *; do
    first=${item:0:1}
    second=${item:1:1}

    folder="$first/$second"
    mkdir -p $folder
    mv $item $folder/
done

There's already a post showing a better program to do what you want but I thought I'd show you how to fix yours. 已经有一篇文章显示了一个更好的程序来执行您想要的操作,但是我想我会告诉您如何修复您的程序。 Hopefully you'll find it informative. 希望您会发现它有用。

#!/bin/bash
ls >LISTE.txt
for i in LISTE.txt; do

This loops over the single value LISTE.txt; 这会遍历单个值LISTE.txt; replace it with: 替换为:

for i in $(cat LISTE.txt); do

to loop over the contents of the file instead. 而是遍历文件的内容

    a=$(cat $i | cut -b 1 | tr '[:lower:]' '[:upper:]')
    b=$(cat $i | cut -b 2 | tr '[:lower:]' '[:upper:]')

You want to use echo rather than cat in the above two lines, as you're after the name of the file not its content. 您要在上面两行中使用echo而不是cat ,因为您是在文件而不是其内容之后。

    mkdir -p $a/$b
    cat $i | xargs mv $a/$b

I don't think the above line does what you think it does... It will attempt to rename the $a/$b directory to C, where C is the content of file $i. 我认为上面的代码行不如您想的那样...它将尝试将$ a / $ b目录重命名为C,其中C是文件$ i的内容 Replace it with: 替换为:

    mv $i $a/$b

The following line is not needed: 不需要以下行:

    rm $i

So simply delete it. 因此,只需将其删除。 It would only be necessary if you copied rather than moved the files using mv . 仅当您使用mv 复制而不是移动文件时才需要。

done

Here's your complete program after the changes I've suggested. 这是我建议的更改之后的完整程序。

#!/bin/bash
ls >LISTE.txt
for i in $(cat LISTE.txt); do
    a=$(echo $i | cut -b 1 | tr '[:lower:]' '[:upper:]')
    b=$(echo $i | cut -b 2 | tr '[:lower:]' '[:upper:]')
    mkdir -p $a/$b
    mv $i $a/$b
done

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

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