Results 1 to 6 of 6

Thread: Looking for help with Bash Script to make directories from Filenames

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

    Question Looking for help with Bash Script to make directories from Filenames

    Hi everyone,

    So I'm trying to make a Nautilus bash script that will allow me to create a directory based off of a file's name

    Example: file.gets.named.here.xxx (xxx in place of extension) - then I can highlight the file and make a directory named after it.

    Pretty simple but it comes in handy for organizing my multimedia. So if someone could provide some help for this, I'd be very thankful. I want to get it to work in Nautilus, not just a bash script, a Nautilus script.

  2. #2
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Looking for help with Bash Script to make directories from Filenames

    This should work with multiple files select:

    Code:
    #/bin/bash
    
    for file in "$@"
    do
        if [[ -f "$file" ]]
        then
            mkdir -- "${file%.*}"
        fi
    done

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

    Re: Looking for help with Bash Script to make directories from Filenames

    Quote Originally Posted by sisco311 View Post
    This should work with multiple files select:

    Code:
    #/bin/bash
    
    for file in "$@"
    do
        if [[ -f "$file" ]]
        then
            mkdir -- "${file%.*}"
        fi
    done
    would this work for single files as well?

    Upon execution of this script in Nautilus it didnt produce any folders..
    Something is missing.. I wish I knew what.
    Last edited by AlexOnVinyl; May 9th, 2012 at 11:19 PM. Reason: Updated because script isnt working.

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

    Re: Looking for help with Bash Script to make directories from Filenames

    Bump

  5. #5
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Looking for help with Bash Script to make directories from Filenames

    Upon execution of this script in Nautilus it didnt produce any folders..
    Something is missing.. I wish I knew what.
    some details would be nice

  6. #6
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Looking for help with Bash Script to make directories from Filenames

    Check out the errors:
    Code:
    #/bin/bash
    
    for file in "$@"
    do
        if [[ -f "$file" ]]
        then
            new="${file%.*}"
            if [[ "$file" == "$new" ]]
            then
                new="$new.dir"
            fi
            mkdir -- "$new"
        fi
    done > out.err.log 2>&1
    this will create a log file (out.err.log) in the directory where the files are. Please post its content.

    EDIT: Just tried it out and it works for me with 1 or more files.

    I guess you also want to move the files to the newly created directory:
    Code:
    #!/bin/bash
    
    shopt -s nullglob
    
    for file
    do
        if [[ -f "$file" ]]
        then
            new="${file%.*}"
            if [[ "$file" == "$new" ]]
            then
                new="$new.dir"
            fi
            mkdir -p -- "$new" || continue
            mv -b -S '.backup' -- "$file" "$new"
        fi
    done
    Last edited by sisco311; May 10th, 2012 at 04:25 PM.

Tags for this Thread

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
  •