I want to backup only my FLAC music folders. FLAC files could be nested like that inside the folders:
AlbumName/
├── Files/
│ ├── someSong01.flac
│ ├── someSong02.flac
├── Covers/
│ ├── someCover01.jpg
│ └── someCover02.jpg
How do I copy and move all AlbumName's folders with their corresponding structure and content that contain somewhere inside at least one FLAC file (I'll assume this is enough to say: the music is in FLAC format)
EDIT: FLAC files could be nested; so I can have:
AlbumName2/
├── someSong01.flac
├── someSong02.flac
├── Covers/
│ ├── someCover01.jpg
| └── someCover02.jpg
And I want to copy those folders with all their contents, not only FLAC files, and paste to another directory.
So if I have as well
AlbumName3/
├── someSong01.mp3
├── someSong02.mp3
├── Covers/
│ ├── someCover01.jpg
| └── someHiddenSong.flac
and
AlbumName4/
├── Files/
│ ├── someSong01.mp3
│ ├── someSong02.mp3
├── Covers/
│ ├── someCover01.jpg
│ └── someCover02.jpg
I want to cp recursively to another directory AlbumName, AlbumName2 and AlbumName3 but not AlbumName4
EDIT: None of the answers were really doing what I want, so I ended up using something like that:
find -mindepth 2 -name '*.flac' -exec dirname {} \; | awk -F "/" '{print $2}' | sort -u | while read -r dirname; do cp -r "$dirname" "backup/"; done
basically I list all flac files, I retrieve the root folder using awk, I delete the duplicates and I do what I want
An option is to use rsync, which copies only flac files and preserves directory structure:
hi my friend you can use
Nice answers
I want to add one more way, you can also use a combination of find and cpio
find . -name "*.flac" -print0|cpio --null -pdm destination/
One can also use
rsync
or write a shell script to find and copy files with the directory structure.Answer 1: You can also use
find
command for same.Explaination:
mkdir
creates new directory.find
command finds*.flac
files located inAlbumName
folder.exec
command executescp
command to each of the filename thatfind
has returned.Answer 2: You can also use
find
command withxargs
as wellMore information: find, xargs
Just had the to solve the same problem for my music library and solved it using the following adapted script. I did not fully engineer it with i.e. variables, as I only intend to use it once :).
The script moves every directory containing flac files including all other files and subdirectories to a new location using rsync. rsync allows you to resume the script if it stops during the process.
My library structure:
New library structure:
(1) start the following shell script in the SOURCE destination (replace TARGET with the respective directory of your choice - I just used the absolute path)
row 1: find all directories containing flac files and pipe (|) that into sort
row 2: remove duplicates with sort and pipe that list into the do loop
row 3: for each directory do
row 4: show the directory in process
row 5: rsync to the target directory plus remove source and preserve structure (-R) in target location
row 6: remove empty directory (as rsync only removes files)
(2) after successfully executing the script in (1) you may execute the following command in the source directory
This command deletes all empty directories not deleted within the script. I.e. "Artist" directories, where all subfolders/albums included flac files.