如何将多个文件夹的内容复制到一个文件夹中?

如何将多个文件夹的内容复制到一个文件夹中?

我有几个音乐文件,它们都按艺术家姓名放在单独的文件夹中,我想将它们复制到一个位置,以便 rhythmbox 可以从该位置导入所有文件。我见过简单的程序解决方案,但没有适用于 Ubuntu 的。有什么建议吗?

答案1

要移动文件,请执行以下操作:

find /my/files/path -iname "*.mp3" -exec mv {} /home/user/music \;

它的工作原理如下,第一个/我的/文件/路径是你想看的地方。/主页/用户/音乐是您要将所有文件移动到的位置。假设您的主文件夹中有所有音乐,/home/jon/music并且您想将其移动到,home/jon/newmusic请执行以下操作:

find /home/jon/music -iname "*.mp3" -exec mv {} /home/jon/newmusic \;

如果源和目标位于同一个驱动器上,那么它会非常快地完成。如果它位于另一个硬盘上,/media/jon/backup/music那么它将取决于硬盘速度。

如果您希望复制而不是移动文件,则必须使用 cp:

find /home/jon/music -iname "*.mp3" -exec cp {} /home/jon/newmusic \;

答案2

从这里获取:http://ubuntuforums.org/showthread.php?t=1385966

请勿意外地在您的主文件夹中执行此操作。

切换到包含所有这些文件的顶级目录并执行

find ./ -type f -exec cp '{}' ./ \;

根据 find 的手册页,您可能还需要转义 {}。

答案3

或者如果您更喜欢脚本:

#!/bin/bash
# to get description use the -h flag

# ==========
# preambula:

PROGNAME=${0##*/}
PROGVERSION=1.0
Name="*"
destDir=`basename ${0##*/} .bash`

# colors:
BLUE='\033[94m'
GREEN="\e[0;32m"
RED='\033[91m'
BLACK='\033[0m'

usage()
{
cat << EO
Usage: $PROGNAME <folder>

Copies files matching rgxp (default to all files) from the current dir
and its child dirs to the specified dir (default to $destDir).

I recomment back-up files before running this script.

Options:
EO
cat <<EO | column -s\& -t

  -d, --dest & destination dir (default to $destDir)
  -n, --name & files-of-interest shell-style rgxp (default to $Name)
 
  -h, --help & show this output
  -v, --version & show version information
      --nocolors & disable colors in output. In case Your terminal doesn't support coloring and You get the output like \"\\033[94mthis\" or Your terminal colors \"conflict\" with the output coloring"
EO
}

SHORTOPTS="hvn:d:"
LONGOPTS="help,version,nocolors,name:dest:"

ARGS=$(getopt -s bash --options $SHORTOPTS --longoptions $LONGOPTS --name $PROGNAME -- "$@")

eval set -- "$ARGS"

while true; do
    case $1 in

        -n|--name)
            Name=$2; shift;;
        -d|--dest)
            destDir=$2; shift;;

        -h|--help)
            usage; exit 0;;
        -v|--version)
            echo "$PROGVERSION"; exit 0;;
        --nocolors)
            noColors=true;;
        --)
            shift; break;;
        *)
            shift; break;;
    esac
    shift
done


# ===================
# finally the script:

# making a dir:
if [ -d "$destDir" ]; then
    rm -rf "$destDir"
fi
mkdir "$destDir"

# copying!
numberOfFiles=0
find . -type f -name "$Name" | {
while read fl; do
    echo -e "${RED}${PROGNAME}: ${GREEN}Copying \"$fl\" ..\c${BLACK}"
    cp "$fl" "$destDir/`basename "$fl"`"
    if [[ $? -eq 0 ]]; then
        echo " OK"
        numberOfFiles=`expr $numberOfFiles + 1`
    fi
done
echo -e "${RED}${PROGNAME}: ${GREEN}$numberOfFiles files got copied to \"$destDir\"${BLACK}"
}


# ===========
## reporting:

echo -e "${RED}${PROGNAME}: ${GREEN}Done.${BLACK}"

相关内容