+ Post New Thread
Results 1 to 3 of 3
Scripts Thread, Bash script for moving multiple files to multipe directories in Coding and Web Development; Hello, I recently started learning bash and had an idea how to make my life easier with one script, but ...
  1. #1

    Join Date
    Oct 2013
    Posts
    5
    Thank Post
    2
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Bash script for moving multiple files to multipe directories

    Hello,
    I recently started learning bash and had an idea how to make my life easier with one script, but now I am just pulling my hair with frustration because I can't think of any idea how to exactly do it, so maybe you guys can help me out with this.
    So what I want to do is that I have one directory with like ~600 .pfd files in it and I want to split(or move) all these files to different directories.
    I will just write and example which will allow you to understand what i want to do.

    Lets say all my files are stored in:
    /home/user/Desktop/list
    and i have another dir on desktop (lets say it's /home/user/Desktop/Result ) which contains folders like: Item001, Item002, Item003 etc...

    All i want to is to take first .pdf from (/home/user/Desktop/list) and move it to folder (/home/user/Desktop/Result/Item001) and the take second .pdf and move it to (/home/user/Desktop/Result/Item002) and so on.

    Code:
    #!/bin/bash
    cd /home/user/Desktop/list
    i=0
    while read line
    do
        array[ $i ]="$line"        
        (( i++ ))
    done < <(ls -R)
    echo ${array[1]}
    mv "${array[1]}" /home/user/Desktop/Result/Item001
    I managed to make a little script which is almost doing what i need, but i do not know how to make this line (
    Code:
    mv "${array[1]}" /home/user/Desktop/Result/Item001
    ) to add like +1....

    I know that this probably very noobish-like way of doing it but as i said i just started learning bash and i am not very much experienced in programing at all.

    Hope someone can enlighten me on this thing.

    Thanks in advance.

  2. #2

    jinnantonnixx's Avatar
    Join Date
    Mar 2011
    Location
    In the Calamatorium.
    Posts
    1,985
    Thank Post
    114
    Thanked 505 Times in 340 Posts
    Blog Entries
    2
    Rep Power
    286
    It's a bit old skool, but should work.

    Code:
    COUNTER=1
    
    for f in /home/user/Desktop/list/*
    do
        PREFIX=
        if [ $COUNTER -lt 100 ]; then PREFIX=0; fi
        if [ $COUNTER -lt 10 ]; then PREFIX=00; fi  #should use an else but meh...
        echo $PREFIX
        echo $PREFIX$COUNTER
        
        mkdir /home/user/Desktop/Result/Item$PREFIX$COUNTER
        mv $f /home/user/Desktop/Result/Item$PREFIX$COUNTER
        let COUNTER=COUNTER+1
        
    done
    Last edited by jinnantonnixx; 4th April 2014 at 12:28 PM.

  3. Thanks to jinnantonnixx from:

    mantas (4th April 2014)

  4. #3

    Join Date
    Oct 2013
    Posts
    5
    Thank Post
    2
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    wow that was fast..
    only had to add ""
    mv "$f" /home/user/Desktop/Result/Item$PREFIX$COUNTER
    on $f variable because there are spaceses in file name.
    but anyway THANK YOU!!!

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 3
    Last Post: 17th February 2014, 04:11 PM
  2. Replies: 5
    Last Post: 10th October 2012, 08:57 AM
  3. Replies: 6
    Last Post: 12th September 2011, 10:24 AM
  4. Scripting to Delete multiple files and folders.
    By mdixon123 in forum Scripts
    Replies: 8
    Last Post: 2nd January 2011, 07:28 PM
  5. Replies: 2
    Last Post: 17th October 2009, 03:55 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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