Very small Shell Script Help... - Unix

This is a discussion on Very small Shell Script Help... - Unix ; The following Script takes each extension and determine what category it belongs and then moves it into a directory based on the extension. (for eg. 1.sh, 5.sh, 9.sh together; 4.csh, 120.csh, 6.csh together and 7.ksh, 2.ksh, 59.ksh together) and moves ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: Very small Shell Script Help...

  1. Very small Shell Script Help...

    The following Script takes each extension and determine what category
    it belongs and then moves it into a directory based on the extension.
    (for eg. 1.sh, 5.sh, 9.sh together; 4.csh, 120.csh, 6.csh together and
    7.ksh, 2.ksh, 59.ksh together) and moves them to their respective
    directories viz. sh, csh and ksh...

    #!/usr/bin/ksh
    for ext in ` ls -d *.* | awk -F. '{print $NF}' | sort -u `
    do
    mkdir -p $ext
    mv *."$ext" "$ext"/.
    done

    The above Script sees for all similar extension, creates the
    directories and moves Files to their corresponding directories.

    I am trying to do it per extension only and not all at a time, like
    take all files with ksh only and try to move them to ksh directory
    while dont do anything to files with Extension sh and csh. That is,
    the Shell parameter should be taken.

    Can anyone give some inputs.

    Thanks a lot in advance.

  2. Re: Very small Shell Script Help...

    kirankumar.techy@gmail.com writes:
    > The following Script takes each extension and determine what category
    > it belongs and then moves it into a directory based on the extension.
    > (for eg. 1.sh, 5.sh, 9.sh together; 4.csh, 120.csh, 6.csh together and
    > 7.ksh, 2.ksh, 59.ksh together) and moves them to their respective
    > directories viz. sh, csh and ksh...
    >
    > #!/usr/bin/ksh
    > for ext in ` ls -d *.* | awk -F. '{print $NF}' | sort -u `
    > do
    > mkdir -p $ext
    > mv *."$ext" "$ext"/.
    > done
    >
    > The above Script sees for all similar extension, creates the
    > directories and moves Files to their corresponding directories.
    >
    > I am trying to do it per extension only and not all at a time, like
    > take all files with ksh only and try to move them to ksh directory
    > while dont do anything to files with Extension sh and csh. That is,
    > the Shell parameter should be taken.
    >
    > Can anyone give some inputs.


    Replace the *.* in the ls-call with *.$ext and pass $ext as argument?

  3. Re: Very small Shell Script Help...

    On 2007-12-10, kirankumar.techy@gmail.com wrote:
    >
    > The following Script takes each extension and determine what category
    > it belongs and then moves it into a directory based on the extension.
    > (for eg. 1.sh, 5.sh, 9.sh together; 4.csh, 120.csh, 6.csh together and
    > 7.ksh, 2.ksh, 59.ksh together) and moves them to their respective
    > directories viz. sh, csh and ksh...
    >
    > #!/usr/bin/ksh
    > for ext in ` ls -d *.* | awk -F. '{print $NF}' | sort -u `
    > do
    > mkdir -p $ext
    > mv *."$ext" "$ext"/.
    > done
    >
    > The above Script sees for all similar extension, creates the
    > directories and moves Files to their corresponding directories.
    >
    > I am trying to do it per extension only and not all at a time, like
    > take all files with ksh only and try to move them to ksh directory
    > while dont do anything to files with Extension sh and csh. That is,
    > the Shell parameter should be taken.


    ext=$1 ## extension given as first argument
    [ -d "$ext" ] || mkdir "$ext"
    mv *."$ext" "$ext"


    To move them all:

    is_file()
    {
    for f in "$@"
    do
    [ -f "$f" ] && return
    done
    return 1
    }

    while is_file *.*
    do
    ext=${f##*.}
    [ -d "$ext" ] || mkdir "$ext"
    mv *."$ext" "$ext"
    done

    --
    Chris F.A. Johnson, author |
    Shell Scripting Recipes: | My code in this post, if any,
    A Problem-Solution Approach | is released under the
    2005, Apress | GNU General Public Licence

  4. Re: Very small Shell Script Help...

    In <493652f7-19a4-49b0-91b9-0e48795027c3@q3g2000hsg.googlegroups.com> kirankumar.techy@gmail.com writes:

    > The following Script takes each extension and determine what category
    > it belongs and then moves it into a directory based on the extension.
    > (for eg. 1.sh, 5.sh, 9.sh together; 4.csh, 120.csh, 6.csh together and
    > 7.ksh, 2.ksh, 59.ksh together) and moves them to their respective
    > directories viz. sh, csh and ksh...


    > #!/usr/bin/ksh
    > for ext in ` ls -d *.* | awk -F. '{print $NF}' | sort -u `
    > do
    > mkdir -p $ext
    > mv *."$ext" "$ext"/.
    > done


    > The above Script sees for all similar extension, creates the
    > directories and moves Files to their corresponding directories.


    > I am trying to do it per extension only and not all at a time, like
    > take all files with ksh only and try to move them to ksh directory
    > while dont do anything to files with Extension sh and csh. That is,
    > the Shell parameter should be taken.


    The whole purpose of the "for ext in ..." line is to discover all of the
    file extensions in the current directory and operate on each one. If you
    only want to operate on one at a time, then you only need the mkdir and
    mv commands:

    #!/usr/bin/ksh
    mkdir -p "$1"
    mv *."$1" "$1"/.

    --
    John Gordon A is for Amy, who fell down the stairs
    gordon@panix.com B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"


+ Reply to Thread