gpt4 book ai didi

用于创建文件夹和移动文件的 linux bash 脚本

转载 作者:太空狗 更新时间:2023-10-29 11:27:21 25 4
gpt4 key购买 nike

您好,我需要根据文件名创建文件夹,并在该文件夹中创建另一个文件夹,然后将文件移动到第二个文件夹

例子:
我的文件.jpg
创建文件夹 my_file
创建文件夹图片
将my_file.jpg移动到图片

我有这个脚本,但它只能在 Windows 上运行,现在我正在使用 Linux

for %%A in (*.jpg) do mkdir "%%~nA/picture" & move "%%A" "%%~nA/picture"
pause

对不起,如果我不准确,但英语不是我的母语。

最佳答案

#!/usr/bin/env bash

# Enable bash built-in extglob to ease file matching.
shopt -s extglob
# To deal with the case where nothing matches. (courtesy of mklement0)
shopt -s nullglob

# A pattern to match files with specific file extensions.
# Example for matching additional file types.
#match="*+(jpg|.png|.gif)"
match="*+(.jpg)"

# By default use the current working directory.
src="${1:-.}"
dest="${2:-/root/Desktop/My_pictures/}"

# Pass an argument to this script to name the subdirectory
# something other than picture.
subdirectory="${3:-picture}"

# For each file matched
for file in "${src}"/$match
do
# make a directory with the same name without file extension
# and a subdirectory.
targetdir="${dest}/$(basename "${file%.*}")/${subdirectory}"
# Remove echo command after the script outputs fit your use case.
echo mkdir -p "${targetdir}"
# Move the file to the subdirectory.
echo mv "$file" "${targetdir}"
done

关于用于创建文件夹和移动文件的 linux bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22668866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com