Sorting Files according to their Extensions... - Unix

This is a discussion on Sorting Files according to their Extensions... - Unix ; I am trying to write a Korne Shell Script wherein we have to sort files according to their extensions(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 move them to their respective directories ...

+ Reply to Thread
Results 1 to 6 of 6

Thread: Sorting Files according to their Extensions...

  1. Sorting Files according to their Extensions...

    I am trying to write a Korne Shell Script wherein we have to sort
    files according to their extensions(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
    move them to their respective directories viz. sh, csh and ksh...

    I think, Regular Expressions can be used here for sorting.

    Any Inputs please...

    Thanks a lot in advance..





  2. Re: Sorting Files according to their Extensions...

    In <5a2d10a1-e351-42b4-afa9-0fd5d7609f77@b40g2000prf.googlegroups.com> kirankumar.techy@gmail.com writes:

    > I am trying to write a Korne Shell Script wherein we have to sort
    > files according to their extensions(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
    > move them to their respective directories viz. sh, csh and ksh...


    for ext in ` ls -d *.* | awk -F. '{print $NF}' | sort -u `
    do
    mv *."$ext" "$ext"/.
    done

    --
    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"


  3. Re: Sorting Files according to their Extensions...

    kirankumar.techy@gmail.com wrote:
    > I am trying to write a Korne Shell Script wherein we have to sort
    > files according to their extensions(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
    > move them to their respective directories viz. sh, csh and ksh...
    >
    > I think, Regular Expressions can be used here for sorting.


    Why do you think a regular expression would be useful in sorting?
    Regular expressions don't sort things; instead, they match things.

    For that matter, why do you think you need to sort at all? It sounds
    like you only need to put things into a directory based on their
    extension. In computer programming, the word "sort" refers to the
    process of taking a list of items that aren't in order and the list
    in order. Your task, instead, is to take each thing and determine
    what category it belongs in, then move it into a directory based on
    the category. That's not the same thing as sorting.

    - Logan

  4. Re: Sorting Files according to their Extensions...

    kirankumar.techy@gmail.com wrote:
    >
    > I am trying to write a Korne Shell Script wherein we have to sort
    > files according to their extensions(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
    > move them to their respective directories viz. sh, csh and ksh...
    >
    > I think, Regular Expressions can be used here for sorting.


    Regular Expressions are used for matching, not sorting.

    > Any Inputs please...


    Perhaps this Perl example may help:

    #!/usr/bin/perl

    # get input directory name from the command line
    # or default to the current directory
    $dir_in = shift() || '.';

    # get output directory name from the command line
    # or default to the current directory
    $dir_out = shift() || '.';

    opendir DIR, $dir_in or die "Cannot open '$dir_in' $!";

    while ( defined( $file = readdir DIR ) ) {
    # does file name have extention?
    # if not get the next file name
    next unless $file =~ /\.([^.]+)$/;

    # if yes, use it as a directory name
    $dir_out .= "/$1";

    # If directory doesn't exist create it
    -d $dir_out
    or mkdir $dir_out
    or die "Cannot create '$dir_out' $!";

    # and move the file
    rename "$dir_in/$file", "$dir_out/$file"
    or die "Cannot rename '$dir_in/$file' $!";
    }

    __END__



    John
    --
    use Perl;
    program
    fulfillment

  5. Re: Sorting Files according to their Extensions...

    "Logan Shaw" wrote in message
    news:47598cef$0$15413$4c368faf@roadrunner.com...
    [snip]
    > For that matter, why do you think you need to sort at all? It sounds
    > like you only need to put things into a directory based on their
    > extension. In computer programming, the word "sort" refers to the
    > process of taking a list of items that aren't in order and the list
    > in order. Your task, instead, is to take each thing and determine
    > what category it belongs in, then move it into a directory based on
    > the category. That's not the same thing as sorting.


    Hmm, sounds a lot like a bucket sort to me .

    Alex



  6. Re: Sorting Files according to their Extensions...

    "John W. Krahn" wrote:
    >
    > kirankumar.techy@gmail.com wrote:
    > >
    > > I am trying to write a Korne Shell Script wherein we have to sort
    > > files according to their extensions(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
    > > move them to their respective directories viz. sh, csh and ksh...
    > >
    > > I think, Regular Expressions can be used here for sorting.

    >
    > Regular Expressions are used for matching, not sorting.
    >
    > > Any Inputs please...

    >
    > Perhaps this Perl example may help:
    >
    > #!/usr/bin/perl
    >
    > # get input directory name from the command line
    > # or default to the current directory
    > $dir_in = shift() || '.';
    >
    > # get output directory name from the command line
    > # or default to the current directory
    > $dir_out = shift() || '.';
    >
    > opendir DIR, $dir_in or die "Cannot open '$dir_in' $!";
    >
    > while ( defined( $file = readdir DIR ) ) {
    > # does file name have extention?
    > # if not get the next file name
    > next unless $file =~ /\.([^.]+)$/;
    >
    > # if yes, use it as a directory name
    > $dir_out .= "/$1";
    >
    > # If directory doesn't exist create it
    > -d $dir_out
    > or mkdir $dir_out
    > or die "Cannot create '$dir_out' $!";
    >
    > # and move the file
    > rename "$dir_in/$file", "$dir_out/$file"
    > or die "Cannot rename '$dir_in/$file' $!";
    > }
    >
    > __END__



    Sorry, that won't work correctly, it should be:


    #!/usr/bin/perl

    # get input directory name from the command line
    # or default to the current directory
    $dir_in = shift() || '.';

    # get output directory name from the command line
    # or default to the current directory
    $dir_out = shift() || '.';

    opendir DIR, $dir_in or die "Cannot open '$dir_in' $!";

    while ( defined( $file = readdir DIR ) ) {
    # does file name have extention?
    # if not get the next file name
    next unless $file =~ /\.([^.]+)$/;

    # if yes, use it as a directory name
    $dir_new = "$dir_out/$1";

    # If directory doesn't exist create it
    -d $dir_new
    or mkdir $dir_new
    or die "Cannot create '$dir_new' $!";

    # and move the file
    rename "$dir_in/$file", "$dir_new/$file"
    or die "Cannot rename '$dir_in/$file' $!";
    }

    __END__



    John
    --
    use Perl;
    program
    fulfillment

+ Reply to Thread