简体   繁体   English

将文件移动到自己的目录中

[英]Move files into their own directories

I have several hundred rar files. 我有几百个rar文件。 I would like to create a directory for each rar file then move the file into the newly created directory. 我想为每个rar文件创建一个目录,然后将文件移动到新创建的目录中。

This is the code I am using to create the rar's 这是我用来创建rar的代码

#!bin/bash
for f in *; do 
rar a -s -m5 "${f%.*}.rar" "$f";
done

This is the code I am using to move the files. 这是我用来移动文件的代码。

#!/bin/bash
for i in *.rar; do
dir=$(echo "$i" | \
sed 's/\(.\)\([^ ]\+\) \([^ ]\+\) - \(.*\)\.pdf/\1\/\1\2 \3/')
dir="DestinationDirectory/$dir"
mkdir -p -- "$dir" && mv -uv "$i" "$dir/$i"
done

The problem is that it creates the directory with the extension name. 问题是它创建了具有扩展名的目录。

ie: file irclog3_26_198.rar is moved into folder /DestinationDirectory/irclog3_26_1988.rar/irclog3_26_1988.rar

I would like the folder to be created ignoring the .rar and just use the name of the file. 我想创建文件夹忽略.rar并只使用文件的名称。

How about: 怎么样:

dir="${dir%.rar}"
mkdir -p -- "$dir" ...

Read more about it at the abs . 腹肌了解更多相关信息。

dir=$(echo ${i[@]::-4})

${name[@]:pos:len})获取字符串/数组的子字符串/子数组,对于字符串, [@]可以避免。

dir=$(echo ${i::-4})

You can use the normal bash shell parameter expension https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html 您可以使用普通的bash shell参数扩展https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html

FILE="TEST.rar" FILE = “TEST.rar”

echo "${FILE%%.*}" echo“$ {FILE %%。*}”

--> TEST - >测试

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

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