Search code examples
bashfindrenamemv

Bash move files and rename it as username_filename in a different folder


There are 4 user folders user1, user2, user3 and user4.

They all have music in their folder and I need to move these .mp4, .mkv and .mp3 files into the folder /tmp/Papierkorb

Also I need to rename it like, when user1 has a music file, it should change the name of the file into the name of the user_filename, from which user It comes from.

This is what I have now:

for file in $(find -type f -name *.mp3;find -type f -name *.mkv;find -type f -name *.mp4)
do
echo mv "$file" /tmp/Papierkorb$file;
done

This is what appears with echo:

root@ubuntu-VirtualBox:/home# bash script.sh 
mv ./user2/music/hits.mp3 /tmp/Papierkorb./user2/music/hits.mp3
mv ./user4/hits/music.mp3 /tmp/Papierkorb./user4/hits/music.mp3
mv ./user1/lied1.mp3 /tmp/Papierkorb./user1/lied1.mp3
mv ./user1/lied1.mkv /tmp/Papierkorb./user1/lied1.mkv
mv ./user1/lied12.mp4 /tmp/Papierkorb./user1/lied12.mp4
mv ./user1/1lied12.mp4 /tmp/Papierkorb./user1/1lied12.mp4
mv ./user3/test/meinealben/testlied.mp4 /tmp/Papierkorb./user3/test/meinealben/testlied.mp4

When I remove the echo, it says, that the folder after Papierkorb doesn't exist. I also don't know anything how I rename it into the name of the user, from which user the file comes from.


Solution

  • With find you can group the -name predicates and use the -exec ... {} + construct:

    for user in user1 user2 user3 user4
    do
        find "./$user" '(' -name '*.[mM][pP][34]' -o -name '*.[mM][kK][vV]' ')' -exec sh -c '
            for file
            do
                mv "$file" "$0${file##*/}"
            done
        ' "/tmp/Papierkorb/${user}_" {} +
    done
    
    Notes:
    • to make the code easier I used one find per username
    • the -exec sh -c '...' "/tmp/Papierkorb/${user}_" {} + is a little hackish but thanks to that you'll directly have value of /tmp/Papierkorb/${user}_ as $0 in the inline script