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

How to create directories according to filename and copy that file to the end of the directory using bash scripting?

假设我从 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"。现在你需要剪切当前用户 $uploader 关键字 -uploader-
  5. 的部分
  6. 此处播放列表="how-to-do-something"
  7. 现在您可以根据需要创建目录。 mkdir -p full/path/to 将 如果存在则不报错,并将根据需要创建父目录。
  8. 现在您可以根据需要复制文件。选项 -n 会说 cp "do not overwrite an existing file".