Terminal Commands to Order Files in a Directory

Can you please point me to a web site with terminal command directions for fixing the order of files in a directory. After that, I want to ZIP them. I am a novice. My OS is 10.4.11 Thanks.

iMac G5 and iBook G3 (10 yrs. old!), Mac OS X (10.4.11)

Posted on Jun 11, 2010 1:40 PM

Reply
18 replies

Jun 11, 2010 2:07 PM in response to Fabe

Do you want to display them, and use that ordered list as input to a zip utility?

man ls
ls -t # sort by modification time (mtime)
ls -c # sort by change time (ctime)
ls -u # sort by access time (atime)
ls -U # sort by creating time (Mac specific)
ls -A # include all hidden files except . and ..
ls -f # display exactly the way they are stored in directory
ls -S # sort by size
ls -r # reverse the sort order
man sort
ls | sort # sort alphabetical
ls | sort -n # sort numeric
ls | sort -f # ignore case alphabetical sort
ls | sort -r # reverse the sort order

Or do you want to physically change the order they are stored in the directory file, so that when read one after another by zip, they are in the order you desire?

This I do not know how to do, short of creating a new directory, and copying the files into the directory in the order you want them stored.

ls -f # display files in the order they are stored in the directory

Jun 11, 2010 5:44 PM in response to Fabe

Why not create a list of files in the order you desire, then use the command

zip -@ archive.zip <file.with.a.list.of.files.with.one.file.per.line

Then you do not need to mess with the directory.

You could use any of the 'ls' or 'ls | sort' commands I suggested in my first reply, as well as just create a file with any editor, or script, or anything that works for you.

Jun 12, 2010 5:18 AM in response to Fabe

You could try something like:
<pre style="border: 1px solid #ddd; padding-left: .75ex; padding-top: .25em; padding-bottom: .25em; margin-top: .5em; margin-bottom: .5em; margin-left: 1ex; max-width: 30ex; overflow: auto; font-size: 10px; font-family: Monaco, 'Courier New', Courier, monospace; color: #444; background: #eee; line-height: normal">zip foo.zip `ls`</pre>
Or if you want an order other than ls default:
<pre style="border: 1px solid #ddd; padding-left: .75ex; padding-top: .25em; padding-bottom: .25em; margin-top: .5em; margin-bottom: .5em; margin-left: 1ex; max-width: 30ex; overflow: auto; font-size: 10px; font-family: Monaco, 'Courier New', Courier, monospace; color: #444; background: #eee; line-height: normal">zip foo.zip `ls | sort -r`</pre>

Jun 16, 2010 1:34 AM in response to Fabe

Fabe wrote:
To everyone - thank you. It is taking me a long time to work through this. It's like changing the crankshaft on my car engine after watching an episode of HorsePower TV. From what I've learned elsewhere, there are also issues of eliminating bits & bytes of extra data. E-gads.


Just curious, does this mean, it didn't work or you did give up? and what issues... do you mean?

Jun 16, 2010 5:00 AM in response to Obj-D

"Just curious, does this mean, it didn't work or you did give up? and what issues... do you mean? "
Obj-D - It means I haven't succeeded at my goal. The failure is one of implementation not instruction. I am a WYSIWYG computer user lost in the Terminal world of command lines. (The last time I dealt with command line instruction, CPM was the dominate OS.)

I am building an e-book from scratch. The final file has the extension .epub. It is a .zip file in disguise. In that zip archive is a folder containing three items, a file and two folders. Each is constructed and named a certain way. Let's call them A, B, C. The file is B. The rules say file B must be the first file in the folder's directory. I have no idea how to do that. Several source pointed to Terminal and command lines. Hence my original question.

It appears, but I cannot confirm, Window users do this more easily than Mac users. Mac adds characters to names and files (so I'm told) that my end product does not want. When I run a validator on my files they are always rejected. Is anyone here specifically working on .epub creation?

Jun 16, 2010 5:22 AM in response to Fabe

Fabe wrote:
"Just curious, does this mean, it didn't work or you did give up? and what issues... do you mean? "
Obj-D - It means I haven't succeeded at my goal. The failure is one of implementation not instruction. I am a WYSIWYG computer user lost in the Terminal world of command lines. (The last time I dealt with command line instruction, CPM was the dominate OS.)


So dont give up, try a little harder. Read, try and look till you got it.

I am building an e-book from scratch. The final file has the extension .epub. It is a .zip file in disguise. In that zip archive is a folder containing three items, a file and two folders. Each is constructed and named a certain way. Let's call them A, B, C. The file is B. The rules say file B must be the first file in the folder's directory.


A and C are Folders then, right?

I have no idea how to do that. Several source pointed to Terminal and command lines. Hence my original question.


Actually, it is in the answers and a little read more and test you will see that you can just add file names to the zip command, something like ( not tested )

zip -r MyBook.zip /Volumes/Users/you/ebook/title/B.file /Volumes/Users/you/ebook/title/A /Volumes/Users/you/ebook/title/C

It appears, but I cannot confirm, Window users do this more easily than Mac users. Mac adds characters to names and files (so I'm told) that my end product does not want.


Mac add's nothing, the user does.
Which end product?

When I run a validator on my files they are always rejected. Is anyone here specifically working on .epub creation?


Oke what validator? Curious again, because I didn't know about that epub == zip


HTH

Jun 16, 2010 11:52 AM in response to Fabe

Are you really wedded to using a text editor and trying to organize the files and directories as you've described?

Because a quick couple of google searches suggest that you have other options:

If you have used LaTeX at all, you could use one of the built in converters to turn LaTeX source into HTML, then use [BookGlutton|https://www.bookglutton.com/api/convert.html] or [Calibre|http://calibre-ebook.com] to convert the HTML to ePub format;

Actually, Calibre supports a large number of input formats...include .txt;

If you use Open Office (and maybe this works with NeoOffice as well...?) then there's [eScape|http://www.infogridpacific.com/igp/AZARDI/eScape%20-ODT2ePub];

Or you could go whole-hog and just get a tool that builds the ePub directly - a WYSIWYG Book Editor, like [Sigil|http://code.google.com/p/sigil]

Why do things the hard way if you aren't a command line lover in the first place?

Cheers.

Message was edited by: g_wolfman

Jun 16, 2010 12:04 PM in response to g_wolfman

g_wolfman - I am very familiar with Calibre and Sigil. Both are good for what they do. However, they do not do everything. Ultimately, to get a truly good e-book one must do some hand fabrication. As this is done, one learns that the whole effort can be done relatively easily and with great control over the finished document, until - until you run into errors like, "mimetype file cannot be found. Mimetype is expected to be first in directory." So curious minds like mine go searching for answers and find ourselves among bright programmers like yourself and others here.

There has yet to be created and marketed a program to take a vanilla text file, allow you to select chapter and section headings, place some simple graphics, and bind it all up together in and epub container. Calibre and Sigil each do their part, but for the end user, it's a bumpy learning curve.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Terminal Commands to Order Files in a Directory

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.