Find the answer to your Linux question:
Results 1 to 6 of 6
I'd like to use a bash script to move specific files, to another directory. I have a directory "/mnt/video" with several subfolders containing .mkv files. /mnt/video/movie1/file1.mkv /mnt/video/movie1/file2.mkv /mnt/video/movie1/file3.mkv /mnt/video/movie2/file1.mkv /mnt/video/movie2/file2.mkv ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2013
    Posts
    2

    Bash move variable folders/files


    I'd like to use a bash script to move specific files, to another directory.

    I have a directory "/mnt/video" with several subfolders containing .mkv files.

    /mnt/video/movie1/file1.mkv
    /mnt/video/movie1/file2.mkv
    /mnt/video/movie1/file3.mkv

    /mnt/video/movie2/file1.mkv
    /mnt/video/movie2/file2.mkv
    /mnt/video/movie2/file3.mkv

    /mnt/video/movie3/dvd1/file1.mkv
    /mnt/video/movie3/dvd1/file2.mkv

    /mnt/video/movie3/dvd2/file1.mkv
    /mnt/video/movie3/dvd2/file2.mkv

    etc...

    I would like to move these files from this directories to a new directory with the same name

    Example:

    /mnt/video/movie1/file1.mkv
    /mnt/video/movie1/file2.mkv
    /mnt/video/movie1/file3.mkv

    Would be

    /mnt/ripped/movie1/file1.mkv
    /mnt/ripped/movie1/file2.mkv
    /mnt/ripped/movie1/file3.mkv

    etc...

    Filenames vary and there are more files except the .mkv files in these directories which i do not want to move.

    Any ideas on this one?

    Thank you in advance.

  2. #2
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    224
    hi,

    easy !
    use `cp` and `rm` both recursively.
    rtfm.

  3. #3
    Just Joined!
    Join Date
    Apr 2013
    Posts
    2
    Quote Originally Posted by watael View Post
    hi,

    easy !
    use `cp` and `rm` both recursively.
    rtfm.
    My folder/filenames/depth vary. I have read the manual, thank you. Any other ideas?, Examples perhaps?

  4. #4
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    224
    in the given examples depth does not vary : you seem to want to move everything under /mnt/video/ to /mnt/ripped/
    if not, then provide more examples.

  5. #5
    Just Joined!
    Join Date
    Apr 2013
    Location
    Gurgaon, India
    Posts
    6
    Hi dihavs,

    This script wud work, try once,

    #!/bin/bash
    for i in {0..n}
    do
    mv /mnt/video/movie${i} /mnt/ripped/movies${i}
    done

    Try this and tell me if u want me to explain it more

  6. #6
    Just Joined!
    Join Date
    Mar 2009
    Location
    Norway
    Posts
    67
    Quote Originally Posted by deepanshu_here_2 View Post
    #!/bin/bash
    for i in {0..n}
    do
    mv /mnt/video/movie${i} /mnt/ripped/movies${i}
    done
    Careful... if the process dies in the middle of that mv, things disappear. What's wrong with find?

    something along the lines of (totally untested, you should test it..)

    Code:
    #!/bin/bash
    # hackish way of moving files from one folder to another.
    #
    # rsync is an option, but we want to remove the old file once done so we
    # have to keep a reference to the file anyway
    
    sourcedir="/mnt/video"
    destdir="/mnt/ripped"
    
    pushd $sourcedir > /dev/null
    find . -name "*.mkv" | while read file
    do
        filename=$(basename "$file")
        dirname=$(dirname "$file")
        test -d "$destdir/$dirname" || mkdir -p "$destdir/$dirname"
        cp -v $file $destdir/$dirname/.
        # rm -fv $file
    done
    
    popd >/dev/null

Posting Permissions

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