How to decide on the sort with ls - Suse

This is a discussion on How to decide on the sort with ls - Suse ; When you do a `ls -U` you get the order of the files unsorted. From the man pages: -U do not sort; list entries in directory order I have a car radio that shows files in this order. The files ...

+ Reply to Thread
Results 1 to 13 of 13

Thread: How to decide on the sort with ls

  1. How to decide on the sort with ls

    When you do a `ls -U` you get the order of the files unsorted. From the
    man pages:
    -U do not sort; list entries in directory order

    I have a car radio that shows files in this order. The files are mp3
    files I ripped from my CD's

    Here a sample of some files:
    #v+
    [/mnt/pink_floyd/the_wall]
    houghi@penne : l -U
    total 112576
    drwxr-xr-x 2 houghi root 16384 2007-10-13 10:43 ./
    drwxr-xr-x 4 houghi root 16384 2008-01-11 07:50 ../
    -rwxr-xr-x 1 houghi root 4668317 2007-10-07 21:35 03_Another_Brick_In_The_Wall_(Part_1).mp3*
    -rwxr-xr-x 1 houghi root 2174976 2007-10-07 21:34 18_Bring_The_Boys_Back_Home.mp3*
    -rwxr-xr-x 1 houghi root 4353583 2007-10-07 21:35 15_Is_There_Anybody_Out_There.mp3*
    -rwxr-xr-x 1 houghi root 6250688 2007-10-07 21:35 11_Don't_Leave_Me_Now.mp3*
    -rwxr-xr-x 1 houghi root 8098271 2007-10-07 21:35 06_Mother.mp3*
    -rwxr-xr-x 1 houghi root 2755272 2007-10-07 21:34 04_The_Happiest_Days_Of_Our_Lives.mp3*
    -rwxr-xr-x 1 houghi root 1949275 2007-10-07 21:33 12_Another_Brick_In_The_Wall_(Part_3).mp3*
    -rwxr-xr-x 1 houghi root 3440750 2007-10-07 21:34 25_The_Trial.mp3*
    -rwxr-xr-x 1 houghi root 2218214 2007-10-07 21:35 17_Vera.mp3*
    -rwxr-xr-x 1 houghi root 4697745 2007-10-07 21:35 16_Nobody_Home.mp3*
    #v-

    So when I play the files on the car radio, I first hear number 03, then
    18, the 15 and so on. That is obviously not very nice.

    I can not change the order on the car radio to look at the name of the
    file, so I must do the changes on the PC.

    So how can I change the order of the entries in the directory?

    houghi
    --
    Dr. Walter Gibbs: Won't that be grand? Computers and the programs
    will start thinking and the people will stop.
    -- Tron (1982)

  2. Re: How to decide on the sort with ls

    On Sun, 13 Jan 2008 13:55:40 +0100, houghi wrote:

    > When you do a `ls -U` you get the order of the files unsorted. From the
    > man pages:
    > -U do not sort; list entries in directory order
    >
    > I have a car radio that shows files in this order. The files are mp3
    > files I ripped from my CD's
    >
    > Here a sample of some files:
    > #v+
    > [/mnt/pink_floyd/the_wall]
    > houghi@penne : l -U
    > total 112576
    > drwxr-xr-x 2 houghi root 16384 2007-10-13 10:43 ./
    > drwxr-xr-x 4 houghi root 16384 2008-01-11 07:50 ../
    > -rwxr-xr-x 1 houghi root 4668317 2007-10-07 21:35 03_Another_Brick_In_The_Wall_(Part_1).mp3*
    > -rwxr-xr-x 1 houghi root 2174976 2007-10-07 21:34 18_Bring_The_Boys_Back_Home.mp3*
    > -rwxr-xr-x 1 houghi root 4353583 2007-10-07 21:35 15_Is_There_Anybody_Out_There.mp3*
    > -rwxr-xr-x 1 houghi root 6250688 2007-10-07 21:35 11_Don't_Leave_Me_Now.mp3*
    > -rwxr-xr-x 1 houghi root 8098271 2007-10-07 21:35 06_Mother.mp3*
    > -rwxr-xr-x 1 houghi root 2755272 2007-10-07 21:34 04_The_Happiest_Days_Of_Our_Lives.mp3*
    > -rwxr-xr-x 1 houghi root 1949275 2007-10-07 21:33 12_Another_Brick_In_The_Wall_(Part_3).mp3*
    > -rwxr-xr-x 1 houghi root 3440750 2007-10-07 21:34 25_The_Trial.mp3*
    > -rwxr-xr-x 1 houghi root 2218214 2007-10-07 21:35 17_Vera.mp3*
    > -rwxr-xr-x 1 houghi root 4697745 2007-10-07 21:35 16_Nobody_Home.mp3*
    > #v-
    >
    > So when I play the files on the car radio, I first hear number 03, then
    > 18, the 15 and so on. That is obviously not very nice.
    >
    > I can not change the order on the car radio to look at the name of the
    > file, so I must do the changes on the PC.
    >
    > So how can I change the order of the entries in the directory?
    >
    > houghi


    You might consider piping through 'sort' - as I recall, it is much more
    flexible.


  3. Re: How to decide on the sort with ls

    ray wrote:
    > On Sun, 13 Jan 2008 13:55:40 +0100, houghi wrote:
    >
    >> When you do a `ls -U` you get the order of the files unsorted. From the
    >> man pages:
    >> -U do not sort; list entries in directory order
    >>
    >> I have a car radio that shows files in this order. The files are mp3
    >> files I ripped from my CD's
    >>
    >> Here a sample of some files:
    >> #v+
    >> [/mnt/pink_floyd/the_wall]
    >> houghi@penne : l -U
    >> total 112576
    >> drwxr-xr-x 2 houghi root 16384 2007-10-13 10:43 ./
    >> drwxr-xr-x 4 houghi root 16384 2008-01-11 07:50 ../
    >> -rwxr-xr-x 1 houghi root 4668317 2007-10-07 21:35 03_Another_Brick_In_The_Wall_(Part_1).mp3*
    >> -rwxr-xr-x 1 houghi root 2174976 2007-10-07 21:34 18_Bring_The_Boys_Back_Home.mp3*
    >> -rwxr-xr-x 1 houghi root 4353583 2007-10-07 21:35 15_Is_There_Anybody_Out_There.mp3*
    >> -rwxr-xr-x 1 houghi root 6250688 2007-10-07 21:35 11_Don't_Leave_Me_Now.mp3*
    >> -rwxr-xr-x 1 houghi root 8098271 2007-10-07 21:35 06_Mother.mp3*
    >> -rwxr-xr-x 1 houghi root 2755272 2007-10-07 21:34 04_The_Happiest_Days_Of_Our_Lives.mp3*
    >> -rwxr-xr-x 1 houghi root 1949275 2007-10-07 21:33 12_Another_Brick_In_The_Wall_(Part_3).mp3*
    >> -rwxr-xr-x 1 houghi root 3440750 2007-10-07 21:34 25_The_Trial.mp3*
    >> -rwxr-xr-x 1 houghi root 2218214 2007-10-07 21:35 17_Vera.mp3*
    >> -rwxr-xr-x 1 houghi root 4697745 2007-10-07 21:35 16_Nobody_Home.mp3*
    >> #v-
    >>
    >> So when I play the files on the car radio, I first hear number 03, then
    >> 18, the 15 and so on. That is obviously not very nice.
    >>
    >> I can not change the order on the car radio to look at the name of the
    >> file, so I must do the changes on the PC.
    >>
    >> So how can I change the order of the entries in the directory?
    >>
    >> houghi

    >
    > You might consider piping through 'sort' - as I recall, it is much more
    > flexible.


    Could you elborate on that? How do I get then to a correct `l U`?

    houghi
    --
    Dr. Walter Gibbs: Won't that be grand? Computers and the programs
    will start thinking and the people will stop.
    -- Tron (1982)

  4. Re: How to decide on the sort with ls

    On Sun, 13 Jan 2008, houghi wrote:-

    >When you do a `ls -U` you get the order of the files unsorted. From the
    >man pages:
    >-U do not sort; list entries in directory order
    >
    >I have a car radio that shows files in this order. The files are mp3
    >files I ripped from my CD's




    >I can not change the order on the car radio to look at the name of the
    >file, so I must do the changes on the PC.
    >
    >So how can I change the order of the entries in the directory?


    Are you intending to rebuild the CD? If so, this should help do the job:

    #!/bin/bash

    # assume the CD is already mounted on /mnt
    #

    # make a temporary directory to hold the sorted structure
    #
    mkdir /tmp/temp.dir

    # rebuild the directory structure
    #
    find /mnt -type d | \
    sort | \
    while read DIR
    do
    mkdir -p "/tmp/temp.dir${DIR:4}" # ${DIR:4} means strip off the first 4 characters from ${DIR}
    done

    # now find all the files, sort the list and copy them into the temporary directory structure
    #
    find /mnt -type f | \
    sort | \
    while read DIR
    do
    cp "${DIR}" "/tmp/temp.dir${DIR:4}" # same as the above
    done

    cd /tmp/temp.dir
    mkisofs -v \
    -r -J -l \
    -publisher "${USER}" \
    -graft-points \
    -iso-level 4 \
    -pad \
    -allow-leading-dots \
    -o /tmp/write_me.iso .

    [ "$?" -eq 0 ] && rm -rf /tmp/temp.dir


    Regards,
    David Bolt

    --
    www.davjam.org/lifetype/ www.distributed.net: OGR@100Mnodes, RC5-72@15Mkeys
    SUSE 10.1 32bit | openSUSE 10.2 32bit | openSUSE 10.3 32bit | openSUSE 11.0a0
    SUSE 10.1 64bit | openSUSE 10.2 64bit | openSUSE 10.3 64bit
    RISC OS 3.6 | TOS 4.02 | openSUSE 10.3 PPC |RISC OS 3.11

  5. Re: How to decide on the sort with ls

    David Bolt wrote:
    > Are you intending to rebuild the CD? If so, this should help do the job:


    No, not realy I would have to dig them up. e.g. The Hobbit and Lord of
    the Rings is a LOT of CD's. Those only is 50+ CD's or so. The same for
    HHGTTG. I bought them, ripped them and then put them in a box and put
    them away.

    Shirley there must be another way to manipulate files and the order they
    are in?

    I tried to copy and also move the files one by one, but that did not
    work. :-(

    houghi
    --
    You are about to enter another dimension, a dimension not only of
    sight and sound but of mind. A journey into a wondrous land of
    imagination. Next stop, Usenet!

  6. Re: How to decide on the sort with ls

    On 2008-01-13 13:55, houghi wrote:
    > When you do a `ls -U` you get the order of the files unsorted. From the
    > man pages:
    > -U do not sort; list entries in directory order
    >
    > I have a car radio that shows files in this order. The files are mp3
    > files I ripped from my CD's
    >
    > Here a sample of some files:
    > #v+
    > [/mnt/pink_floyd/the_wall]
    > houghi@penne : l -U
    > total 112576
    > drwxr-xr-x 2 houghi root 16384 2007-10-13 10:43 ./
    > drwxr-xr-x 4 houghi root 16384 2008-01-11 07:50 ../
    > -rwxr-xr-x 1 houghi root 4668317 2007-10-07 21:35 03_Another_Brick_In_The_Wall_(Part_1).mp3*
    > -rwxr-xr-x 1 houghi root 2174976 2007-10-07 21:34 18_Bring_The_Boys_Back_Home.mp3*
    > -rwxr-xr-x 1 houghi root 4353583 2007-10-07 21:35 15_Is_There_Anybody_Out_There.mp3*
    > -rwxr-xr-x 1 houghi root 6250688 2007-10-07 21:35 11_Don't_Leave_Me_Now.mp3*
    > -rwxr-xr-x 1 houghi root 8098271 2007-10-07 21:35 06_Mother.mp3*
    > -rwxr-xr-x 1 houghi root 2755272 2007-10-07 21:34 04_The_Happiest_Days_Of_Our_Lives.mp3*
    > -rwxr-xr-x 1 houghi root 1949275 2007-10-07 21:33 12_Another_Brick_In_The_Wall_(Part_3).mp3*
    > -rwxr-xr-x 1 houghi root 3440750 2007-10-07 21:34 25_The_Trial.mp3*
    > -rwxr-xr-x 1 houghi root 2218214 2007-10-07 21:35 17_Vera.mp3*
    > -rwxr-xr-x 1 houghi root 4697745 2007-10-07 21:35 16_Nobody_Home.mp3*
    > #v-
    >
    > So when I play the files on the car radio, I first hear number 03, then
    > 18, the 15 and so on. That is obviously not very nice.
    >
    > I can not change the order on the car radio to look at the name of the
    > file, so I must do the changes on the PC.
    >
    > So how can I change the order of the entries in the directory?
    >
    > houghi


    ls *.mp3 > playlist.m3u
    Then play the m3u file.

    /bb

  7. Re: How to decide on the sort with ls

    On Mon, 14 Jan 2008, houghi wrote:-

    >David Bolt wrote:
    >> Are you intending to rebuild the CD? If so, this should help do the job:

    >
    >No, not realy I would have to dig them up.


    Not unless you were planning on re-ripping the files. The script only
    needs the discs with the MP3s on them. All it does is to copy the
    contents from the mounted CD onto the hard drive, creates a sorted
    directory list and builds a matching directory structure. Then it
    creates a sorted file list and copies the files into the directories,
    and finally builds a new ISO image for burning.

    >Shirley there must be another way to manipulate files and the order they
    >are in?


    Not without creating a new CD, there isn't.

    >I tried to copy and also move the files one by one, but that did not
    >work. :-(


    Try running the script, mounting the resultant image and looking at the
    directory structure. A couple of quick tests here shows it does the job.


    Regards,
    David Bolt

    --
    www.davjam.org/lifetype/ www.distributed.net: OGR@100Mnodes, RC5-72@15Mkeys
    SUSE 10.1 32bit | openSUSE 10.2 32bit | openSUSE 10.3 32bit | openSUSE 11.0a0
    SUSE 10.1 64bit | openSUSE 10.2 64bit | openSUSE 10.3 64bit
    RISC OS 3.6 | TOS 4.02 | openSUSE 10.3 PPC |RISC OS 3.11

  8. Re: How to decide on the sort with ls

    birre wrote:
    >> So how can I change the order of the entries in the directory?
    >>

    >
    > ls *.mp3 > playlist.m3u
    > Then play the m3u file.


    Not an option. The radio does not recognize m3u files.

    houghi
    --
    You are about to enter another dimension, a dimension not only of
    sight and sound but of mind. A journey into a wondrous land of
    imagination. Next stop, Usenet!

  9. Re: How to decide on the sort with ls

    David Bolt wrote:
    > Not unless you were planning on re-ripping the files. The script only
    > needs the discs with the MP3s on them.


    Discs? What discs? I put them on a SD card and/or on a USB device. I
    overwrite them when I want to listen to something else. I could try and
    copy a directory one by one to the device, but I am sure there is a way
    to do it on the HD itself.

    >>Shirley there must be another way to manipulate files and the order they
    >>are in?

    >
    > Not without creating a new CD, there isn't.


    I find that highly unlikely. How is the order determined? There must be
    some logic behind it. Also I am not trying to make a CD anyway. I just
    try to change the order.

    houghi
    --
    You are about to enter another dimension, a dimension not only of
    sight and sound but of mind. A journey into a wondrous land of
    imagination. Next stop, Usenet!

  10. Re: How to decide on the sort with ls

    On Mon, 14 Jan 2008, houghi wrote:-

    >David Bolt wrote:
    >> Not unless you were planning on re-ripping the files. The script only
    >> needs the discs with the MP3s on them.

    >
    >Discs? What discs?


    My assumption was that you were referring to CDs, not something else.

    >I put them on a SD card and/or on a USB device. I
    >overwrite them when I want to listen to something else. I could try and
    >copy a directory one by one to the device, but I am sure there is a way
    >to do it on the HD itself.


    Probably.

    >>>Shirley there must be another way to manipulate files and the order they
    >>>are in?

    >>
    >> Not without creating a new CD, there isn't.

    >
    >I find that highly unlikely. How is the order determined? There must be
    >some logic behind it. Also I am not trying to make a CD anyway. I just
    >try to change the order.


    Okay, how about this then:

    #!/bin/bash

    # assume the SD card is already mounted on /mnt
    # if not, change here
    #
    SD="/mnt"
    TMP="/tmp/temp.dir"

    # make a temporary directory to hold the sorted structure
    #
    mkdir -p "${TMP}"

    # bad assumption, but assume SD card is writable for the user
    #
    mv "${SD}"/* "${TMP}/"

    # if you're paranoid, and you have more than one SD card available
    # use the following instead of the mv above
    #
    # cp -r "${SD}/"* "${TMP}/"
    # read -p "Swap the cards now. Press return when ready."

    # rebuild the directory structure
    #
    find "${TMP}" -type d | \
    sort | \
    while read DIR
    do

    # ${#TMP} returns the length of ${TMP}
    # ${DIR:${#TMP}} means remove that many characters from the start of ${DIR}
    #
    mkdir -p "${SD}${DIR:${#TMP}}"
    done

    # now find all the files, sort the list and copy them into the temporary directory structure
    #
    find "${TMP}" -type f | \
    sort | \
    while read DIR
    do
    cp "${DIR}" "${SD}/${DIR:${#TMP}}" # same as the above
    done



    Regards,
    David Bolt

    --
    www.davjam.org/lifetype/ www.distributed.net: OGR@100Mnodes, RC5-72@15Mkeys
    SUSE 10.1 32bit | openSUSE 10.2 32bit | openSUSE 10.3 32bit | openSUSE 11.0a0
    SUSE 10.1 64bit | openSUSE 10.2 64bit | openSUSE 10.3 64bit
    RISC OS 3.6 | TOS 4.02 | openSUSE 10.3 PPC |RISC OS 3.11

  11. Re: How to decide on the sort with ls

    On Mon, 14 Jan 2008 11:31:59 +0100, houghi wrote:

    > birre wrote:
    >>> So how can I change the order of the entries in the directory?
    >>>

    >>
    >> ls *.mp3 > playlist.m3u
    >> Then play the m3u file.

    >
    > Not an option. The radio does not recognize m3u files.
    >
    > houghi


    cd into the top directory containing your mp3 files.
    Issue a command like the following:

    find . -name '*.mp3' | sort | cpio -o | (cd /tmp; cpio -ivd)

    Change /tmp to the directory that your sd card is mounted on -
    /media/whatever. Also, you can add extra params to the find to control
    what files will be transfered. Use the command up to the sort to show what
    files will be transfered. The back to back cpio commands will copy the
    files specified on standard input in the order specified.

    regards,
    Frank Ranner

  12. Re: How to decide on the sort with ls

    David Bolt wrote:
    >>Discs? What discs?

    >
    > My assumption was that you were referring to CDs, not something else.


    They are SOOOO last century. ;-)

    >>I find that highly unlikely. How is the order determined? There must be
    >>some logic behind it. Also I am not trying to make a CD anyway. I just
    >>try to change the order.

    >
    > Okay, how about this then:


    I think I am being not very clear. The end result is to be able to copy
    to the SD card (which is in itself also a USB reader). However as much
    as writing files one by one, I would like to sort correctly on the HD
    already in such a way that whenever I copy to wherever, the order is
    correct.

    I will try the workaround when I am at home. It is a lousy workaround,
    if you ask me. :-(

    > #!/bin/bash

    for I in `ls -1 *.mp3`;do cp $I /mnt/$I;done

    I would think that that would work. I have tried :
    mkdir tmp && for I in `ls -1 *.mp3`;do cp $I tmp/$I;done

    And still the files in tmp are NOT in the right order

    What I want
    houghi@penne : ls -1
    01_hotel_california.mp3
    02_new_kid_in_town.mp3
    03_life_in_the_fast_lane.mp3
    04_wasted_time.mp3
    05_wasted_time_reprise.mp3
    06_victim_of_love.mp3
    07_pretty_maids_all_in_a_row.mp3
    08_try_and_love_again.mp3
    09_the_last_resort.mp3

    The list (to a subdirectory. Identical to the original)
    houghi@penne : ls -U1
    04_wasted_time.mp3
    05_wasted_time_reprise.mp3
    09_the_last_resort.mp3
    07_pretty_maids_all_in_a_row.mp3
    08_try_and_love_again.mp3
    03_life_in_the_fast_lane.mp3
    06_victim_of_love.mp3
    01_hotel_california.mp3
    02_new_kid_in_town.mp3

    The list (to a different partition. Different to the original)
    houghi@penne : ls -U1
    06_victim_of_love.mp3
    04_wasted_time.mp3
    02_new_kid_in_town.mp3
    01_hotel_california.mp3
    03_life_in_the_fast_lane.mp3
    09_the_last_resort.mp3
    07_pretty_maids_all_in_a_row.mp3
    08_try_and_love_again.mp3
    05_wasted_time_reprise.mp3


    houghi
    --
    You are about to enter another dimension, a dimension not only of
    sight and sound but of mind. A journey into a wondrous land of
    imagination. Next stop, Usenet!

  13. Re: How to decide on the sort with ls

    Frank Ranner wrote:
    > cd into the top directory containing your mp3 files.
    > Issue a command like the following:
    >
    > find . -name '*.mp3' | sort | cpio -o | (cd /tmp; cpio -ivd)
    >
    > Change /tmp to the directory that your sd card is mounted on -
    > /media/whatever.


    I will see if that works when I am home and can put the card in, but it
    does not work on HD when I do it towards a different partition.

    houghi
    --
    You are about to enter another dimension, a dimension not only of
    sight and sound but of mind. A journey into a wondrous land of
    imagination. Next stop, Usenet!

+ Reply to Thread