gpt4 book ai didi

linux - 如何根据文件名创建目录并使用 bash 脚本将该文件复制到目录的末尾?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:52:39 25 4
gpt4 key购买 nike

假设我从 YouTube 下载了一个名为 some-user-uploader-how-to-do-something-playlist-video-1.mp4 的视频文件,其中 some-user 是上传者的名字,how-to-do-something 是播放列表的名字,video-1 是视频的名字。现在,我想编写一个 bash 脚本,将文件从其当前目录复制到类似 /uploader/playlist/ 的目录。我知道这对某些人来说可能相当容易,但我对脚本编写真的很陌生。到目前为止,我已经写了这个。

#script to copy a file named some-user-uploader-how-to-do-something-
#playlist-video-1.mp4 to /where/to/copy/uploader/playlist
#!/bin/bash

#set the path of the files to FILES

FILES=*

#goes through all the files one by one and initialize them to f
for f in $FILES
do
#I have no idea what happens here. I wanted to check if a file has
#"-playlist" in it's name
if [[ !($f == ${f%%-playlist*}) ]]
then
#set the uploader's name to uploader
uploader="${f%%-uploader*}"
#set the playlist's name to playlist
playlist="${f%%-playlist*}"
#checks if the destination directory exists
if [ -f "/where/to/copy/$uploader" ]
then
#if it exists prints the message "directory already exists"
echo "directory already exists."
else
#if the directory doesn't exist it creates the directory to
#name of the uploader
`mkdir /where/to/copy/$uploader`
fi


#checks if the destination directory exists
if [ -f "/where/to/copy/$uploader/$playlist" ]
then
#if it exists prints the message "directory already exists"
echo "file already exists."
else
#if the directory doesn't exist it creates the directory to
#name of the playlist
`mkdir /where/to/copy/$uploader/$playlist`
fi
#checks if the file exists in the destination directory
if [ -f "/where/to/copy/$uploader/$playlist/$f" ]
then
#if it exists prints the message "file already exists"
echo "file already exists."
else
#if the file doesn't exist in the destination directory it
#copies the file to that directory
`cp -r $f /where/to/copy/$uploader/$playlist/$f`
fi
fi
done

它工作正常,但我不知道为什么。如果有人可以解释它是如何工作的或知道获得相同结果的更好方法,请提供帮助。

我对脚本所做的更改。希望这对正在寻找具有类似功能的脚本的其他人有所帮助。

#!/bin/bash

# script to move a file named some-user-uploader-how-to-do-something-
# playlist-video-1.mp4 to /where/to/copy/uploader/playlist/video-1.mp4

FILES="*"

DESTDIR=${1:-"/where/to/copy"}

for f in $FILES
do
if [[ ! ( "$f" == "${f%%-playlist*}" ) ]]
then
uploader="${f%%-uploader*}"
uploader_playlist="${f%%-playlist*}"
playlist="${uploader_playlist#${uploader}-uploader-*}"
filename="${f#${uploader_playlist}-playlist-*}"

mkdir -p "$DESTDIR/$uploader/$playlist"

mv -n "$f" "$DESTDIR/$uploader/$playlist/$filename"

fi
done

最佳答案

一个应该有效的版本和下面的一些注释。测试一下。
chmod u+x Go.sh 之后,您可以使用 ./Go.sh 调用,它将采用默认路径,或者您可以将其传递给另一个目标路径,例如./Go.sh/media/UndercoverLemon/storage/definitive

#!/bin/bash
# Comment from the second line the first is the shebang # [0]
FILES="*"
DESTDIR=${1:-"/media/UndercoverLemon/storage/test"} # [1]
for f in $FILES
do
if [[ ! ( "$f" == "${f%%-playlist*}" ) ]] # [2]
then
uploader="${f%%-uploader*}" # [3]
playlist="${f%%-playlist*}" # [4]
playlist="${playlist#${uploader}-uploader-*}" # [5]
mkdir -p "$DESTDIR/$uploader/$playlist" # [6]
cp -n "$f" "$DESTDIR/$uploader/$playlist/$f" # [7]
fi
done

注意事项:

shebang #!/bin/bash 必须是脚本的第一行。

  1. 最好只有 1 个地方
  2. 在下面检查字符串 $f 是否等于 -playlist* 之前剪切的字符串。如果相等,则表示没有$f 中的子字符串“-playlist”。使用 not ! 可以否定测试结果。
  3. 在这里 uploader="some-user"
  4. 此处 playlist="some-user-uploader-how-to-do-something"。现在你需要用当前用户 $uploaderkeyword -uploader-
  5. 剪切部分
  6. 这里播放列表="how-to-do-something"
  7. 现在您可以根据需要创建目录。 mkdir -p full/path/to 将如果存在则不报错,并根据需要创建父目录。
  8. 现在您可以根据需要复制文件。选项 -n 将告诉 cp “不要覆盖现有文件”

关于linux - 如何根据文件名创建目录并使用 bash 脚本将该文件复制到目录的末尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29708778/

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