Find the answer to your Linux question:
Results 1 to 8 of 8
I am looking for some help in making directories and moving files to them. I am doing it manually and since I have tens of thousands of these files to ...
  1. #1
    Just Joined!
    Join Date
    Dec 2007
    Posts
    2

    Help with make directory and move files to that directory

    I am looking for some help in making directories and moving files to them. I am doing it manually and since I have tens of thousands of these files to move, I figure there must be an easier way to do it.
    Here is what I have and want to do.

    In one directory I have tens of thousands of .txt files named as such:

    rdb-001-001.txt
    rdb-001-002.txt
    rdb-002-001.txt
    rdb-002-002.txt
    and so on.
    What I want to do( and not manually ) is
    mkdir rdb-001
    mv rdb-001-* rdb-001
    I want a script that can grab a file name, make the directory and move all corresponding files to that directory.
    Any help? Pointers as to what program to use? Or is a script the best?
    Thanks

  2. #2
    Just Joined!
    Join Date
    Nov 2004
    Posts
    18
    Something like this should do the trick:
    Code:
    #!/bin/sh
    
    DIR=""
    for F in $(ls -1 *.txt); do
        D="${F%-*}"
        if [ "$DIR" != "$D" ]; then
    	DIR="$D"
    	mkdir -p $DIR
        fi
        mv -f "$F" "$DIR"    
    done

  3. #3
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by patch_linams View Post
    Something like this should do the trick:
    Code:
    #!/bin/sh
    
    DIR=""
    for F in $(ls -1 *.txt); do
        D="${F%-*}"
        if [ "$DIR" != "$D" ]; then
    	DIR="$D"
    	mkdir -p $DIR
        fi
        mv -f "$F" "$DIR"    
    done
    actually, there's no need to check for existing directory since you have used -p for mkdir. Also, its better to use shell expansion, instead of ls -1 *txt when passing it to the for loop. If files has spaces in them, using ls -1 will break the for loop.

    so the whole thing might be
    Code:
    for F in *.txt; do
        mkdir -p ${F%-*.txt}
        mv -f "$F" ${F%-*.txt}
    done

  4. #4
    Just Joined!
    Join Date
    Nov 2004
    Posts
    18
    Quote Originally Posted by ghostdog74 View Post
    its better to use shell expansion, instead of ls -1 *txt when passing it to the for loop. If files has spaces in them, using ls -1 will break the for loop.
    Since the exact naming scheme of all files is known it won't be necessary to check for spaces in filenames. But I agree that "ls" is not needed.
    Quote Originally Posted by ghostdog74 View Post
    Well, since the exact format of all files is known
    so the whole thing might be
    Code:
    for F in *.txt; do
        mkdir -p ${F%-*.txt}
        mv -f "$F" ${F%-*.txt}
    done
    well, your mkdir statement will create several directories if there are spaces in filenames. You might want to add double quotation marks
    This would improve both versions:
    Code:
    #!/bin/sh
    for F in *.txt; do
        D="${F%-*.txt}"
        mkdir -p "$D"
        mv -f "$F" "$D"
    done

  5. #5
    Just Joined!
    Join Date
    Dec 2007
    Posts
    2

    Thanks for all the great help!

    I've kind of added/tweaked a little from each reply and its working fantastic!! Actually I'm completely finished moving the files!! It sure saved me tons of work. I knew there was a reason I left Windows
    Thanks to everyone who replied, I can't thank you enough for all the help.

  6. #6
    Just Joined!
    Join Date
    Dec 2007
    Location
    Ceredigion, Wales,UK.
    Posts
    10

    Help with make directory and move files to that directory

    As a very new bee in the scripting hive I prefer to google my way out of trouble whenever possible, but one line of the proposed script has me completely lost. What does:-

    "${F%-*.txt}" mean?

    I know what it is doing in this context, but can find no reference to "F%", or anything like it in the man pages, the numerous tutorials I have studied, nor from googling "mkdir -p ${".

    Enlighten me, someone, please.

  7. #7
    drl
    drl is online now
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi, Hatrick.

    A few things are deep within man bash. Look for the heading Parameter Expansion in that man page. You'll find some interesting descriptions of % and # in such expressions.

    If you wish to see some examples and references, look through http://www.tldp.org/LDP/abs/html/index.html -- it's long, but valuable.

    And, of course, there is nothing quite like experimentation.

    And a pat on the back for doing your own searching first ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  8. #8
    Just Joined!
    Join Date
    Dec 2007
    Location
    Ceredigion, Wales,UK.
    Posts
    10
    Thanks drl. Heading for man bash, right now.

Posting Permissions

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