Find the answer to your Linux question:
Results 1 to 6 of 6
Hi im trying to clean up my hard disk a little, basically i have tons of music on my hard drive and i want to try to move it all ...
  1. #1
    Just Joined!
    Join Date
    Nov 2009
    Posts
    6

    Finding all my music on hard drive and moving it to a directory for music

    Hi im trying to clean up my hard disk a little, basically i have tons of music on my hard drive and i want to try to move it all in to a single directory called music. All the music is spread out in the my document directory and its sub directories below is the code i have so far but its not working any ideas? thanks in advance


    #!/bin/bash

    for file in /my-documents/*; do
    if echo "$file" | grep -q '.mp3$'; then
    mv "$file" ~/my-documents/music
    echo "mp3 files moved to music folder"

    else echo "No MP3 files to move"

    fi

    Done

  2. #2
    Linux Newbie
    Join Date
    Nov 2007
    Location
    Planet Earth
    Posts
    152
    Code:
    find . -type f -name *.mp3 | while read $file
    do
         (
               # maybe, you want to validate before move
               mv $file /path/to/music/
         )
    done
    I didn't test, but that's the idea
    EOF

  3. #3
    Just Joined!
    Join Date
    Nov 2009
    Posts
    6
    Ok so i gave that a try but its saying i need to add a file operand after destination, im not rele sure what this means below is my code can u spot the problem?

    thanks

    #! /bin/bash

    find ~/my-documents/* -type f -name "*.mp3" | while read $file
    do
    (

    mv $file ~/my-documents/catalogs/mp3/

    )


    done

  4. #4
    Just Joined!
    Join Date
    Aug 2005
    Posts
    65
    Code:
    #! /bin/bash
    
    find ~/my-documents/* -type f -name "*.mp3" | while read file
    do
    (
    
    mv $file ~/my-documents/catalogs/mp3/
    
    )
    
    
    done
    does teh above solve it ?
    u can also try this
    Code:
    find ~/my-documents/* -type f -name "*.mp3" -exec mv {} /path/to/mp3 \;

  5. #5
    Just Joined!
    Join Date
    Nov 2009
    Posts
    6

    Great Success !

    Hey sorry for getting back to this so late .... yes ur first suggestion worked thanks very much. So ive set that up now im thinking i might set it up for a few other file types and give my hard drive a proper clean out, any ideas how i could have this same code running a loop so that it first searches for mp3s then moves if there is some then goes to search for jpegs and if there is any moves to jpeg dir and so on ? would i need a simple for loop or is there a better way?

  6. #6
    Just Joined!
    Join Date
    Aug 2005
    Posts
    65
    Code:
    for i in mp3 txt avi 
    do 
    find ~/my-documents/* -type f -name "*.$i" -exec mv {} /path/to/$i \;
    done

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...