home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 16.28 oldlinks: Find Unconnected Symbolic Links Chapter 16
Where Did I Put That?
Next: 17. Finding Files with find

16.29 sls: Super ls with Format You Can Choose

The ls -l command, and related commands like stat (21.13 ) , give lots of information about a file (more exactly, about a file's inode (1.22 ) ). The information is printed in a way that's (sort of) nice to look at. But the format might not be exactly what you want. That format can be tough for shell programmers to use: parsing the output with sed , awk , and others is tricky and a pain (article 16.25 has an example). Finally, the ls -l output is different on BSD and System V systems.

The sls command solves those problems and more. It lets you:
  • Make your own output format: pick the information you want to see and the order it's shown.

  • Sort the output on one or more fields.

  • Make a consistent date format: numeric or in words, include the seconds if you want to, and more. Best of all, the date format won't change for files more than six months old (unless you use the -u option).

And there's much more.

The manual page on the disc explains sls formatting in detail. Here are a few examples. Let's start with the style of ls -l output that has fixed-width columns and doesn't show group ownership. (The default sls -l is similar, but its date format doesn't change after six months and it doesn't have the total line.)

% ls -l

total 3
-rw-r-----  1 jerry        1641 Feb 29  1992 afile
lrwxrwxrwx  1 jerry           8 Nov 18 00:38 bfile -> ../bfile

Here's a more user-friendly format for people who aren't UNIX hackers (it might be best to put this into an alias or shell function (10.1 ) ). The date and time are shown, followed by the owner's name, the size in kbytes, and the filename without the symbolic link information like -> ../bfile :

% sls -p '%m"%F %d, 19%y  %r" %u %4skK %n'

February 29, 1992  03:43:00 PM    jerry    2K afile
November 18, 1992  00:38:22 AM    jerry    1K bfile

How about a simple ls output that shows all three file dates (16.5 ) : modification, access, and inode change? We'll use echo (8.6 ) to print a title first:

% echo 'modify   access   inode'; \
sls -p '%m"%D" %a"%D" %c"%D" %n'

modify   access   inode
02/29/92 09/17/92 11/18/92 afile
11/18/92 11/18/92 11/18/92 bfile

Finally, let's ask sls to make a set of UNIX commands that could be used at the end of a shell archive (19.2 ) file. These commands would recreate the modes, date and owner (with a numeric UID) as the files are extracted from the archive:


% sls -p 'chmod %P %n; chown %U %n; touch %m"%m%d%H%M%y" %n'

chmod 640 afile; chown  225 afile; touch 0229154392 afile
chmod 777 bfile; chown  225 bfile; touch 1118003892 bfile

I didn't show the sorting options or many of the other output format characters. But I hope I've given you an idea (or ten).

- JP

Previous: 16.28 oldlinks: Find Unconnected Symbolic Links UNIX Power Tools Next: 17. Finding Files with find
16.28 oldlinks: Find Unconnected Symbolic Links Book Index 17. Finding Files with find

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System