Results 1 to 10 of 10

Thread: Bash script to create folders from file names

  1. #1
    Join Date
    Sep 2011
    Beans
    3

    Bash script to create folders from file names

    Hey everyone, newbie at the site.

    Been using Ubuntu on and off for a year now. While I usually take a hands on approach to learning this stuff, my new external HD has just arrived and I want to back everything up in a semi organised fashion asap.


    I want to create a script to read the contents of a directory and create folders in that directory from the named files within and then move the corresponding files to their new folder.
    i.e.

    Desktop/Folder 1
    file 1.doc
    file 2.xls

    would become

    Desktop/Folder 1/file 1/
    file 1.doc
    Desktop/Folder 1/file 2/
    file 2.xls

    I tried this in Windows and the script collapsed when it ran into files named with spaces i.e "file 1.doc" creating a folder "file" and obviously not moving the corresponding file.

    I tried the search function in the forums and of course google, but surprisingly for such a trivial (or so I thought) task I found very little.

    Can anyone help me?

    Cheers,
    r34ch
    Last edited by Elfy; September 21st, 2011 at 07:46 PM. Reason: windows

  2. #2
    Join Date
    Jan 2009
    Location
    Flanders
    Beans
    Hidden!

    Re: Bash script to create folders from file names

    something like this:

    Code:
    for file in $( ls /home/user/Desktop/Folder ) 
    do
        mkdir "/home/user/Desktop/Folder/${file%.*}"
        mv "/home/user/Desktop/Folder/$file" "/home/user/Desktop/Folder/${file%.*}/$file"
    done
    I did not test this. So tell me if you get errors, and try it on a test directory first.

  3. #3
    Join Date
    Jan 2008
    Location
    Manchester UK
    Beans
    13,573
    Distro
    Ubuntu

    Re: Bash script to create folders from file names

    Quote Originally Posted by sanderd17 View Post
    something like this:

    Code:
    for file in $( ls /home/user/Desktop/Folder ) 
    do
        mkdir "/home/user/Desktop/Folder/${file%.*}"
        mv "/home/user/Desktop/Folder/$file" "/home/user/Desktop/Folder/${file%.*}/$file"
    done
    I did not test this. So tell me if you get errors, and try it on a test directory first.
    Well, the first line is Bash Pitfall no 1

    see here

    http://mywiki.wooledge.org/BashPitfalls

    I'll leave sisco311 to explain the rest when he gets here later because no doubt I'll get it wrong anyway.

  4. #4
    Join Date
    Feb 2008
    Beans
    5,636

    Re: Bash script to create folders from file names

    It won't be the first time Sisco makes me look like a total hobgoblin so I'll give it a go:
    PHP Code:
    #!/bin/bash
    find . -type f ! -name "*script*" | while read file;
    do
        
    f=$(basename "$file")
        
    f1=${f%.*}
        
    mkdir "$f1"
        
    mv "$f" "$f1"
    done 
    Name is as script, put it in the folder with the files you want moved, make it executable
    Code:
    chmod +x script
    and run it
    Code:
    ./script
    Last edited by Elfy; September 21st, 2011 at 07:47 PM. Reason: goblin snip

  5. #5
    Join Date
    Feb 2008
    Beans
    5,636

    Re: Bash script to create folders from file names

    Quote Originally Posted by TeoBigusGeekus View Post
    It won't be the first time Sisco makes me look like a total hobgoblin...

  6. #6
    Join Date
    Sep 2011
    Beans
    3

    Re: Bash script to create folders from file names

    Quote Originally Posted by TeoBigusGeekus View Post
    I'll give it a go
    Seems to work perfectly, thanks!

  7. #7
    Join Date
    Sep 2011
    Beans
    3

    Re: Bash script to create folders from file names

    Ok thanks everyone, you've now wet my whistle.

    I want to learn a bit more on bash scripting and make a script myself that replaces "."s in a file name with spaces, except the last "." for the extension.

    Where is a good site to start and what are a good few terms to google?

    Cheers,
    r34ch

  8. #8
    Join Date
    Jan 2008
    Location
    Manchester UK
    Beans
    13,573
    Distro
    Ubuntu

    Re: Bash script to create folders from file names

    This is the best place to start.

  9. #9
    Join Date
    Sep 2011
    Beans
    180
    Distro
    Ubuntu

    Question Re: Bash script to create folders from file names

    Is there anyway to make this compatible with Nautilus scripts - for an simple right-click and run?

    I was looking for a script just like this for a while the only part that irritates me is I can't find one that will work with Nautilus.

    ex: ~/.gnome2/nautilus-scripts
    - then I can execute it by right clicking the file I want a directory made after.

    PS. Sorry for reviving an old thread. Just looking for my answer and then I'll leave it to rest.

  10. #10
    Join Date
    Jan 2010
    Location
    Sydney, Australia
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Bash script to create folders from file names

    Quote Originally Posted by nothingspecial View Post
    This is the best place to start.
    I would also like to add abs guide to the list .A bit tough of a nut for a beginner though , still it has a array of tips and tricks in its bag .

    http://tldp.org/LDP/abs/html/
    “Progress is made by lazy men looking for easier ways to do things”
    — Robert A. Heinlein

Bookmarks

Posting Permissions

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