Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 16
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    change the order of files in a directory


    hi all,

    i need to change the unsorted order of files in a directory. That sounds
    like a contradiction in terms, but it's what you get if you run
    Code:
    ls -f
    also, it's seems to be the only order that my mp3 player knows.

    i found this work-around: if i create a directory anew and copy one file at
    a time, e.g.
    Code:
    ls -1 | while read FILE
    do
      cp $FILE $TARGETDIRECTORY
    done
    will do the trick. all this to illustrate my problem.

    now my question is this: surely there must be an easier way to do this?

    i'd like to convert all directories of my mp3 collection such that unsorted
    directory order and alphabetical order coincide. i'm suspecting that the type
    of the file system has an influence on this (the above works if the $TARGET-
    DIRECTORY is on a VFAT fs, but it failed when i copied to a ext3 disk.

    cheers, kai

  2. #2
    Just Joined! otama71's Avatar
    Join Date
    Sep 2007
    Location
    Perth, Australia
    Posts
    12
    Kai,

    If I have interpreted your post correctly, you use the ls -f to list your mp3 files, however, these are not listed alphabetically?

    I would use the following:

    Code:
    ls -f | sort
    hope that this has answered your question.

    otama71

  3. #3
    No, his MP3 player seems to read the directory sequentially, so the order of the filename entries is significant. His solution is the only one I can thing of to guarantee the order of the entries in a directory but it doesn't work on an ext3 filesystem, which presumably uses some sort of hashing to determine entry placement. Or get a better MP3 player that you can specify the file order to.

    BTW
    Code:
    cp -p * $TARGETDIRECTORY
    is easier than piping ls in a read loop. The "-p" preserves timestamps, etc. but it doesn't solve the ext3 behaviour. I'll need to do a bit of research into it.

  4. $spacer_open
    $spacer_close
  5. #4
    Quote Originally Posted by scm View Post
    No, his MP3 player seems to read the directory sequentially, so the order of the filename entries is significant.
    yes, this is the point.


    Or get a better MP3 player that you can specify the file order to.
    yeah, I suppose you're right. It's just that I use it every day, and I usually
    end up breaking it within a year's time. That's why I don't want to invest
    too much in the player.

    BTW
    Code:
    cp -p * $TARGETDIRECTORY
    is easier than piping ls in a read loop. The "-p" preserves timestamps, etc. but it doesn't solve the ext3 behaviour. I'll need to do a bit of research into it.
    thanks for this hint. that's indeed more convinient.

    still, if i could fix the order on the ext3 file system i could also drag and drop
    directories with mp3 files. so i'm still interested in solutions, but i what you
    pointed out about ext3 using hashing would prevent this, for all i know.

    thanks to you both!
    kai

  6. #5
    Just Joined!
    Join Date
    Jan 2008
    Posts
    3

    use mv

    I wrote a small script after Christmas that orders files in a directory, for just this purpose. It's below. It works on my IDE disk on my old laptop (ext3). It fails on my USB-attached drive (also ext3). They have comparable tune2fs parameters. I have no idea why it works in one place and not the other. What you can do, however, is run it on the mounted USB filesystem for the player after you copy the files (with cp -a) to the player. The player is almost certainly a vfat filesystem, or some other variant of fat, and won't be as sophisticated as ext3 about hashing directory entries.

    The script is fast because it uses mv rather than cp.

    Finally, you can also generate a playlist, as follows:

    cd (to album directory)
    find . | sort -n | grep mp3 > title.m3u

    The script findalpha is below. It wouldn't let me attach something without an extension (hey, what's with that?? this is LINUX, not M$).

    --jh--
    Code:
    #! /bin/sh
    
    # Usage:
    # findalpha [-R]
    
    # Reorder the directory entries according to alphanumerical order, so
    # find output is in order.  Useful for preparing files for play on an
    # MP3 player that follows directory order.
    # If -R is given, do recursively.
    
    # Thu Dec 27 02:47:00 EST 2007 v. 0.1 jh initial version
    
    # TODO:
    # handle spaces in file/directory names
    # figure out why it works on IDE but not on USB, even if the FS is ext3
    
    start="`pwd`"
    
    if [ "$1" = "-R" ] ; then
      dirs=`find . -type d`
    else
      dirs=.
    fi
    
    for dir in $dirs; do
      cd "$dir"
      tmp=`mktemp -d -p .`
      mv `find . -maxdepth 1 -mindepth 1 | grep -v $tmp | despecial` $tmp
      cd $tmp
      mv `find . -maxdepth 1 -mindepth 1 | sort -n` ..
      cd ..
      rmdir $tmp
      cd "$start"
    done
    Last edited by devils casper; 01-05-2008 at 05:54 AM. Reason: Added [code]...[/code] tag.

  7. #6
    hi jh,
    thanks for sharing your script!

    i've been using 'mv' myself during xmas break, and it's fast indeed; so your
    script might be just what i need.

    what's
    Code:
    despecial
    though? sounds like it would remove special
    characters but i couldn't find a tool by this name.

    cheers, kai

  8. #7
    I think
    Code:
    mv `find . -maxdepth 1 -mindepth 1 | grep -v $tmp | despecial` $tmp
    would be more efficient written as
    Code:
    mv `find . -type f | despecial` $tmp
    assuming the newly created temporary directory is the only subdirectory in the current one.

  9. #8
    Just Joined!
    Join Date
    Jan 2008
    Posts
    3
    OOPS. despecial is a script that quotes characters that are special to the shell. It's below.

    SCM: yes, you can remove -maxdepth 1 -mindepth 1 IF you have no subdirectories. But, often one has a large tree of multiple CDs, such as an audiobook, with one cd per directory, or a 2-CD album, or several albums. The way I now use the script, I load up my player and do the command from the top level of the audio directory and it does all of the directories on the whole player. There's no need for efficiency here; modern computers are thousands of times faster than the CPU clock rate and disk speeds at which you'd notice the difference in execution time with or without the grep.

    --jh--

    #! /bin/sed -f
    # Wed Aug 31 12:33:39 EDT 2005
    # version 1.1
    # jh
    # Filter to quote special characters. Useful for protecting filenames with odd
    # characters in them from the shell.

    # The backslash line must come first!
    # version 1.1: Escape the space character.

    s/\\/\\\\/g
    s/\~/\\\~/g
    s/`/\\`/g
    s/\!/\\\!/g
    s/\#/\\\#/g
    s/\$/\\\$/g
    s/\%/\\\%/g
    s/\^/\\\^/g
    s/\&/\\\&/g
    s/\*/\\\*/g
    s/(/\\(/g
    s/)/\\)/g
    s/{/\\{/g
    s/}/\\}/g
    s/\[/\\\[/g
    s/\]/\\\]/g
    s/|/\\|/g
    s/\;/\\\;/g
    s/\:/\\\:/g
    s/\"/\\\"/g
    s/'/\\'/g
    s/</\\</g
    s/>/\\>/g
    s/\?/\\\?/g
    s/\n/\\\n/g
    s/ /\\ /g

  10. #9

    enhanced "findalpha"

    I just started using a USB mp3 player today too and was displeased at the fact it dosn't play alphebetically. Some google'ing brought me here, and I found jh's script (findalpha above) to be almost just what I needed. For anyone like me who finds this thread in the future, here is my "slightly" modified version to handle directories and files with spaces in them. Note that because of the use of xargs in this version, the "despecial" script is no longer needed. (Though that is a useful script, thanks jh for that too )

    Code:
    #!/bin/sh
    
    # Usage:
    # findalpha [-R]
    
    # Reorder the directory entries according to alphanumerical order, so
    # find output is in order.  Useful for preparing files for play on an
    # MP3 player that follows directory order.
    # If -R is given, do recursively.
    
    # Thu Dec 27 02:47:00 EST 2007 v. 0.1 jh initial version
    # Wed Oct 22 11:08:00 EST 2008 v. 0.2 andrew-regner added support for directories with spaces
    
    # TODO:
    # figure out why it works on IDE but not on USB, even if the FS is ext3
    
    start="`pwd`"
    
    if [ "$1" = "-R" ] ; then
      dirs=`find . -type d`
    else
      dirs=.
    fi
    
    for dir in $dirs; do
      test "$sdir" != "" && dir="$sdir $dir"
      if [ ! -d "$dir" ]; then
        sdir="$dir"
        continue
      fi
      cd "$dir"
      tmp=`mktemp -d -p .`
      find . -maxdepth 1 -mindepth 1 | grep -v $tmp | xargs -n1 -d"\n" -I'{}' mv '{}' $tmp
      cd $tmp
      find . -maxdepth 1 -mindepth 1 | sort -n | xargs -n1 -d"\n" -I'{}' mv '{}' ..
      cd ..
      rmdir $tmp
      cd "$start"
    done

  11. #10
    hi ADRegner! thanks for the follow-up!
    cheers, kai

Posting Permissions

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