问题描述
我只想备份我的 FLAC 音乐文件夹。FLAC 文件可以像这样嵌套在文件夹中:
AlbumName/
├── Files/
│ ├── someSong01.flac
│ ├── someSong02.flac
├── Covers/
│ ├── someCover01.jpg
│ └── someCover02.jpg
如何复制和移动所有 AlbumName 的文件夹及其相应的结构和内容,其中包含至少一个 FLAC 文件(我假设这足以说明:音乐是 FLAC 格式)
编辑:\nFLAC 文件可以嵌套;所以我可以拥有:
AlbumName2/
├── someSong01.flac
├── someSong02.flac
├── Covers/
│ ├── someCover01.jpg
| └── someCover02.jpg
我想复制这些文件夹及其所有内容(不仅仅是 FLAC 文件),然后粘贴到另一个目录。
所以如果我也有
AlbumName3/
├── someSong01.mp3
├── someSong02.mp3
├── Covers/
│ ├── someCover01.jpg
| └── someHiddenSong.flac
和
AlbumName4/
├── Files/
│ ├── someSong01.mp3
│ ├── someSong02.mp3
├── Covers/
│ ├── someCover01.jpg
│ └── someCover02.jpg
我想递归地 cp 到另一个目录 AlbumName、AlbumName2 和 AlbumName3,但不是 AlbumName4
编辑:\n 没有一个答案真正满足我的要求,所以我最终使用了类似的东西:
find -mindepth 2 -name '*.flac' -exec dirname {} \; | awk -F "/" '{print $2}' | sort -u | while read -r dirname; do cp -r "$dirname" "backup/"; done
基本上我列出了所有的 flac 文件,我使用 awk 检索根文件夹,我删除了重复项,然后我做了我想做的事情
最佳方案
一种选择是使用 rsync,它只复制 flac 文件并保留目录结构:
rsync -avzm --include=*/ --include=*.flac --exclude=* albums/ backup/
-
档案
-
非常详细
-
z 传输过程中压缩(在同一台计算机上复制可能无用)
-
m 删除空目录
-
首先包含所有目录
-
第二个包含 flac 文件
-
最后一个排除排除所有其他文件
次佳方案
嗨我的朋友你可以使用
mkdir newdirectory
cp -r --parents */*.flac newdirectory/
第三种方案
很好的答案
我想再添加一种方法,您也可以使用 find 和 cpio 的组合
find . -name "*.flac" -print0|cpio --null -pdm destination/
\\n
Explanation:
\\n
GNU find searches the directory tree rooted at each given file name by evaluating the given expression from left to right, according to the rules of precedence (see Operators), until the outcome is known (the left-hand side is false for AND operations, true for OR), at which point find moves on to the next file name.
\\n
GNU cpio is a tool for creating and extracting archives, or copying files from one place to another. It handles a number of cpio formats as well as reading and writing tar files.
\\n
There are 3 cpio modes:
\\n
\\n
Copy-out mode:\\n
In copy-out mode, cpio copies files into an archive. It reads a list of filenames, one per line, on the standard input, and writes the archive onto the standard output.
\\n
Copy-in mode:\\n
In copy-in mode, cpio copies files out of an archive or lists the archive contents. It reads the archive from the standard input.
\\n
Copy-pass mode:\\n
In copy-pass mode, cpio copies files from one directory tree to another, combining the copy-out and copy-in steps without actually using an archive. It reads the list of files to copy from the standard input; the directory into which it will copy them is given as a non-option argument.
\\n
\\n
Here, we are using Copy-pass mode.
\\n
-
上述命令中使用的选项:
\\n
find:
\\n
\\n
- -name -> filename or use regex instead of writing full-name
\\n
\\n
cpio:
\\n
\\n
- ‘-m, –preserve-modification-time’\\n
Retain previous file modification times when creating files.
\\n
- ‘-p,–pass-through’\\n
Run in copy-pass mode. see ``Copy-pass mode``.
\\n
- ‘-d, –make-directories’\\n
Create leading directories where needed.'
\\n
--quiet'\\n
Do not print the number of blocks copied.`\\n
\\n
New options are the "-print0" available with GNU find, combined with the "--null" option of cpio. These two options act together to send file names between find and cpio, even if special characters are embedded in the file names.
\\n
还可以使用 rsync
或编写 shell 脚本来查找和复制具有目录结构的文件。
\\n
For Rsync explanation, Please have a look at above answers.
\\n
第四种方案
答案 1:您也可以使用 find
命令。
mkdir newDir && find AlbumName/ -iname "*.flac" -exec cp --parents "{}" newDir/ \;
解释:
mkdir
创建新目录。find
命令查找位于 AlbumName
文件夹中的 *.flac
文件。exec
命令对 find
返回的每个文件名执行 cp
命令。
答案 2:您也可以将 find
命令与 xargs
一起使用
mkdir newDir && find AlbumName/ -iname "*.flac" | xargs -I{} cp --parents {} newDir/