您好,我需要根据文件名创建文件夹,然后在该文件夹中创建另一个文件夹,然后将文件移动到第二个文件夹
例子:
cat.jpg
create folder cat
create folder picture
move cat.jpg to picture
我的所有 .jpg 文件都在
/root/桌面/我的图片
所以它应该看起来像这样:
示例图片“cat.jpg”
/root/Desktop/My_pictures/cat/pictures/cat.jpg
抱歉,如果我说得不够准确,但英语不是我的母语。
谨致问候并致以感谢
答案1
您还可以删除ls *.jpg
并简单地使用shell 通配符:
#!/bin/bash
for full_filename in *jpg; do
extension="${full_filename##*.}"
filename="${full_filename%.*}"
mkdir -p "$filename/picture"
mv "$full_filename" "$filename/picture"
done
请创建并运行这个脚本里面/root/Desktop/My_pictures
。
答案2
类似的方法:
#!/usr/bin/env bash
## iterate through each file whose name ends in 'jpg'
## saving it as $file. ~ is your $HOME directory
for file in ~/Desktop/My_pictures/*jpg
do
## basename will remove the path (~/Desktop/My_pictures) and also
## remove the extension you give as a second argument
name="$(basename "$file" .jpg)"
## create the directory, the -p means it will create
## the parent directories if needed and it won't complain
## if the directory exists.
mkdir -p ~/Desktop/My_pictures/"$name"
## copy the file to the new directory
mv "$file" "~/Desktop/My_pictures/$name"
done
将上面的脚本保存为,例如,~/movefiles.sh
使其可执行chmod +x movefiles.sh
并运行它:
~/movefiles.sh