# Is there any way to coerce ls(1) to produce Octal output?



## Chris_H (Apr 5, 2014)

Greetings,

 I've been attempting to find a way to get ls()(1) to produce octal output, instead of symbolic output, e.g.;

```
# ls -l
-rw-r--r--  1 root  wheel    76 Mar 21 07:58 some.file
```
could produce:

```
-46-4--4--  1 root  wheel    76 Mar 21 07:58 some.file
```
well not quite like that, but that would produce the octal/numeric properties/values (permissions). Any thoughts, or other commands I haven't thought of, that will give me the desired output I'm looking for?

Thank you for all your time, and consideration.

--Chris

UPDATE: I forgot to mention, my shell is csh().


----------



## Chris_H (Apr 5, 2014)

*Re: Is there any way to coerce ls(1) to produce Octal output*

OK. I've managed to get something _close_ to what I'm attempting to achieve. What I've done, is create a "filter" octal, that I can feed (pipe) the output of ls(), with any of it's switches to: `ls -la / | octal` which produces:

```
total 93
755 drwxr-xr-x  20 root  wheel      512 Apr  4 08:38 .
755 drwxr-xr-x  20 root  wheel      512 Apr  4 08:38 ..
644 -rw-r--r--   1 root  wheel      963 Mar 26 12:09 .cshrc
600 -rw-------   1 root  wheel      122 Mar 25 16:58 .history
644 -rw-r--r--   1 root  wheel      251 Mar 26 12:09 .profile
775 drwxrwxr-x   2 root  operator   512 Apr  2 14:27 .snap
444 -r--r--r--   1 root  wheel     6199 Sep 27  2013 COPYRIGHT
755 drwxr-xr-x   2 root  wheel     1024 Mar 26 12:02 bin
755 drwxr-xr-x   9 root  wheel     1024 Apr  4 07:54 boot
755 drwxr-xr-x   3 root  wheel      512 Mar 26 17:51 compat
555 dr-xr-xr-x   6 root  wheel      512 Apr  4 01:46 dev
755 drwxr-xr-x   2 root  wheel      512 Mar 25 17:14 dvd
755 drwxr-xr-x  21 root  wheel     2048 Apr  4 08:53 etc
755 lrwxr-xr-x   1 root  wheel        8 Mar 25 16:43 home -> usr/home
755 drwxr-xr-x   3 root  wheel     1536 Mar 26 12:03 lib
755 drwxr-xr-x   3 root  wheel      512 Mar 26 12:05 libexec
755 drwxr-xr-x   2 root  wheel      512 Sep 26  2013 media
755 drwxr-xr-x   2 root  wheel      512 Sep 26  2013 mnt
555 dr-xr-xr-x   1 root  wheel        0 Apr  5 11:45 proc
755 drwxr-xr-x   2 root  wheel     2560 Mar 26 12:03 rescue
755 drwxr-xr-x  22 root  wheel     1024 Apr  5 11:45 root
755 drwxr-xr-x   2 root  wheel     2560 Mar 26 12:04 sbin
755 lrwxr-xr-x   1 root  wheel       11 Mar 26 12:01 sys -> usr/src/sys
776 drwxrwxrwt  22 root  wheel     1024 Apr  5 11:45 tmp
755 drwxr-xr-x  17 root  wheel      512 Apr  3 00:52 usr
755 drwxr-xr-x  26 root  wheel      512 Apr  4 01:46 var
```
as opposed to the usual `ls -la /` output:

```
total 93
drwxr-xr-x  20 root  wheel      512 Apr  4 08:38 .
drwxr-xr-x  20 root  wheel      512 Apr  4 08:38 ..
-rw-r--r--   1 root  wheel      963 Mar 26 12:09 .cshrc
-rw-------   1 root  wheel      122 Mar 25 16:58 .history
-rw-r--r--   1 root  wheel      251 Mar 26 12:09 .profile
drwxrwxr-x   2 root  operator   512 Apr  2 14:27 .snap
-r--r--r--   1 root  wheel     6199 Sep 27  2013 COPYRIGHT
drwxr-xr-x   2 root  wheel     1024 Mar 26 12:02 bin
drwxr-xr-x   9 root  wheel     1024 Apr  4 07:54 boot
drwxr-xr-x   3 root  wheel      512 Mar 26 17:51 compat
dr-xr-xr-x   6 root  wheel      512 Apr  4 01:46 dev
drwxr-xr-x   2 root  wheel      512 Mar 25 17:14 dvd
drwxr-xr-x  21 root  wheel     2048 Apr  4 08:53 etc
lrwxr-xr-x   1 root  wheel        8 Mar 25 16:43 home -> usr/home
drwxr-xr-x   3 root  wheel     1536 Mar 26 12:03 lib
drwxr-xr-x   3 root  wheel      512 Mar 26 12:05 libexec
drwxr-xr-x   2 root  wheel      512 Sep 26  2013 media
drwxr-xr-x   2 root  wheel      512 Sep 26  2013 mnt
dr-xr-xr-x   1 root  wheel        0 Apr  5 11:47 proc
drwxr-xr-x   2 root  wheel     2560 Mar 26 12:03 rescue
drwxr-xr-x  22 root  wheel     1024 Apr  5 11:47 root
drwxr-xr-x   2 root  wheel     2560 Mar 26 12:04 sbin
lrwxr-xr-x   1 root  wheel       11 Mar 26 12:01 sys -> usr/src/sys
drwxrwxrwt  22 root  wheel     1024 Apr  5 11:45 tmp
drwxr-xr-x  17 root  wheel      512 Apr  3 00:52 usr
drwxr-xr-x  26 root  wheel      512 Apr  4 01:46 var
```
I'd like to expand it further. But it's a start. Here's what I'm using (octal):

```
awk '{k=0;for(i=0;i<=8;i++)k+=((substr($1,i+2,1)~/[rwx]/) *2^(8-i));if(k)printf("%0o ",k);print}'
```

--Chris


----------



## wblock@ (Apr 5, 2014)

*Re: Is there any way to coerce ls(1) to produce Octal output*


```
% stat -f "%Sp = %Lp" .cshrc
-rw-r--r-- = 644
```


----------



## fonz (Apr 5, 2014)

*Re: Is there any way to coerce ls(1) to produce Octal output*

I was going to suggest (or offer to give it a whirl myself) hacking the source code for ls to add an option for this, but from the looks of it you're already getting (close to) what you want 

Edited to add: I did something similar with top: in my ongoing efforts to recreate the desktop experience of 4Dwm + Indigo Magic as found on e.g. SGI Indy boxes I added an option to top to produce colourised output for use in a program called gr_top as seen in the screenshot below. Since I quite like your idea I thought that would be the way to go.


----------



## Chris_H (Apr 5, 2014)

*Re: Is there any way to coerce ls(1) to produce Octal output*



			
				wblock@ said:
			
		

> ```
> % stat -f "%Sp = %Lp" .cshrc
> -rw-r--r-- = 644
> ```


Hello, @wblock@. I actually experimented with stat(). But it seemed a bit more cumbersome. I was hoping for something "simpler". Maybe I'm just lazy. 

Thank you, very much for your reply. I'll experiment with your stat() suggestion.

--Chris


----------



## Chris_H (Apr 5, 2014)

*Re: Is there any way to coerce ls(1) to produce Octal output*



			
				fonz said:
			
		

> I was going to suggest (or offer to give it a whirl myself) hacking the source code for ls to add an option for this, but from the looks of it you're already getting (close to) what you want
> 
> Edited to add: I did something similar with top: in my ongoing efforts to recreate the desktop experience of 4Dwm + Indigo Magic as found on e.g. SGI Indy boxes I added an option to top to produce colourised output for use in a program called gr_top as seen in the screenshot below. Since I quite like your idea I thought that would be the way to go.


Greetings, @fonz.

Thanks for the thought(s). I _love_ your top() modification. I've been threatening to get an SGI box, but can't decide on a model. Maybe an Indy. I'll have to think about it.

Thanks again, for the reply, @fonz.

--Chris


----------



## ralphbsz (Apr 6, 2014)

*Re: Is there any way to coerce ls(1) to produce Octal output*

GNU find has a printf action, which allows printing all manner of information about a file, in all manners of formats.  But the default find on FreeBSD is not GNU find.  To get GNU find, you'll have to install the findutils port or package.


----------



## Chris_H (Apr 6, 2014)

*Re: Is there any way to coerce ls(1) to produce Octal output*

Thanks for the reply, and information, @ralphbsz.

That's good to know. But _ultimately_, I had hoped to accomplish this, with commands already available in BASE. In the "big picture", I'd _really_ like to figure out a way to modify src/bin/ls in such a way, as to provide octal output, via a switch. Such as `ls -O`, or something. While I have only currently managed the desired output by use of external filters (awk()). With any luck, or additional help, I'll manage it within the ls() source, itself. 

Thanks again, @ralphbsz.

--Chris


----------



## Chris_H (Apr 6, 2014)

*Re: Is there any way to coerce ls(1) to produce Octal output*

OK. I've been experimenting further with what I have managed previously. It's a bit more informative now. But I think my math may be a bit off. Here's the updated version of octal:

```
awk '{k=0;s=0;for(i=0;i<=8;i++){k+=((substr($1,i+2,1)~/[rwxst]/)*2^(8-i))}j=4;for(i=4;i<=10;i+=3){s+=((substr($1,i,1)~/[stST]/)*j)j/=2}if(k){printf("%0o%0o ",s,k)}print}'
```

Following is the filtered output, via `ls -la / | octal`:

```
3200 total 93
3755 drwxr-xr-x  20 root  wheel      512 Apr  5 16:32 .
3755 drwxr-xr-x  20 root  wheel      512 Apr  5 16:32 ..
3644 -rw-r--r--   1 root  wheel      963 Mar 26 12:09 .cshrc
3600 -rw-------   1 root  wheel      122 Mar 25 16:58 .history
3644 -rw-r--r--   1 root  wheel      251 Mar 26 12:09 .profile
3775 drwxrwxr-x   2 root  operator   512 Apr  2 14:27 .snap
3444 -r--r--r--   1 root  wheel     6199 Sep 27  2013 COPYRIGHT
3755 drwxr-xr-x   2 root  wheel     1024 Mar 26 12:02 bin
3755 drwxr-xr-x   9 root  wheel     1024 Apr  4 07:54 boot
3755 drwxr-xr-x   3 root  wheel      512 Mar 26 17:51 compat
3555 dr-xr-xr-x   6 root  wheel      512 Apr  5 09:44 dev
3755 drwxr-xr-x   2 root  wheel      512 Mar 25 17:14 dvd
3755 drwxr-xr-x  21 root  wheel     2048 Apr  4 08:53 etc
3755 lrwxr-xr-x   1 root  wheel        8 Mar 25 16:43 home -> usr/home
3755 drwxr-xr-x   3 root  wheel     1536 Mar 26 12:03 lib
3755 drwxr-xr-x   3 root  wheel      512 Mar 26 12:05 libexec
3755 drwxr-xr-x   2 root  wheel      512 Sep 26  2013 media
3755 drwxr-xr-x   2 root  wheel      512 Sep 26  2013 mnt
3555 dr-xr-xr-x   1 root  wheel        0 Apr  6 14:41 proc
3755 drwxr-xr-x   2 root  wheel     2560 Mar 26 12:03 rescue
3755 drwxr-xr-x  22 root  wheel     1536 Apr  5 16:46 root
3755 drwxr-xr-x   2 root  wheel     2560 Mar 26 12:04 sbin
3755 lrwxr-xr-x   1 root  wheel       11 Mar 26 12:01 sys -> usr/src/sys
15777 drwxrwxrwt  10 root  wheel      480 Apr  6 14:41 tmp
3755 drwxr-xr-x  17 root  wheel      512 Apr  3 00:52 usr
3755 drwxr-xr-x  26 root  wheel      512 Apr  5 09:44 var
```

_Do_ feel free to comment/critique/contribute. 

--Chris


----------



## wblock@ (Apr 6, 2014)

*Re: Is there any way to coerce ls(1) to produce Octal output*

`stat -f "%1Mp%3Lp %10Sp %3l %8Su%8Sg %11z %12Sa %N" *`

If you really want to do this in a script, I found some online that didn't work, but one clever suggestion that the way to do this is to convert "rwx" to 1, and "-" to 0, and then convert the resulting binary number to octal:

```
% echo '-rw-r--r--' | tr "rwx" "1" | tr "-" "0"
0110100100
```


----------



## fonz (Apr 6, 2014)

*Re: Is there any way to coerce ls(1) to produce Octal output*



			
				Chris_H said:
			
		

> _Do_ feel free to comment/critique/contribute.


I've actually started hacking the source for ls. To be continued


----------



## Chris_H (Apr 7, 2014)

*Re: Is there any way to coerce ls(1) to produce Octal output*



			
				fonz said:
			
		

> Chris_H said:
> 
> 
> 
> ...


Excellent news @fonz!

I've been looking at it, as well. While it _does_ have references regarding "octal". They appear to be related to stat()ish related functions, like device names, etc. extern.h, and print.c, they looked like the best choice of targets for me.

Please keep me abreast of your progress. I'd love to see your results.  Thanks!

--Chris


----------



## Chris_H (Apr 7, 2014)

*Re: Is there any way to coerce ls(1) to produce Octal output*

OK. I found some more time to try and find the problem regarding the output of the last version of octal. I've found the issue, and it now produces correct output. Should anyone be interested, here's the latest version of octal:


```
awk '{k=0;s=0;for(i=0;i<=8;i++){;k+=((substr($1,i+2,1)~/[rwxst]/)*2^(8-i));};j=4;for(i=4;i<=10;i+=3){;s+=((substr($1,i,1)~/[stST]/)*j);j/=2;};if(k){;printf("%0o%0o ",s,k);};print;}'
```

This is the output of `ls -la / | octal`


```
0200 total 93
0755 drwxr-xr-x  20 root  wheel      512 Apr  5 16:32 .
0755 drwxr-xr-x  20 root  wheel      512 Apr  5 16:32 ..
0644 -rw-r--r--   1 root  wheel      963 Mar 26 12:09 .cshrc
0600 -rw-------   1 root  wheel      122 Mar 25 16:58 .history
0644 -rw-r--r--   1 root  wheel      251 Mar 26 12:09 .profile
0775 drwxrwxr-x   2 root  operator   512 Apr  2 14:27 .snap
0444 -r--r--r--   1 root  wheel     6199 Sep 27  2013 COPYRIGHT
0755 drwxr-xr-x   2 root  wheel     1024 Mar 26 12:02 bin
0755 drwxr-xr-x   9 root  wheel     1024 Apr  4 07:54 boot
0755 drwxr-xr-x   3 root  wheel      512 Mar 26 17:51 compat
0555 dr-xr-xr-x   6 root  wheel      512 Apr  5 09:44 dev
0755 drwxr-xr-x   2 root  wheel      512 Mar 25 17:14 dvd
0755 drwxr-xr-x  21 root  wheel     2048 Apr  4 08:53 etc
0755 lrwxr-xr-x   1 root  wheel        8 Mar 25 16:43 home -> usr/home
0755 drwxr-xr-x   3 root  wheel     1536 Mar 26 12:03 lib
0755 drwxr-xr-x   3 root  wheel      512 Mar 26 12:05 libexec
0755 drwxr-xr-x   2 root  wheel      512 Sep 26  2013 media
0755 drwxr-xr-x   2 root  wheel      512 Sep 26  2013 mnt
0555 dr-xr-xr-x   1 root  wheel        0 Apr  6 20:28 proc
0755 drwxr-xr-x   2 root  wheel     2560 Mar 26 12:03 rescue
0755 drwxr-xr-x  22 root  wheel     1536 Apr  5 16:46 root
0755 drwxr-xr-x   2 root  wheel     2560 Mar 26 12:04 sbin
0755 lrwxr-xr-x   1 root  wheel       11 Mar 26 12:01 sys -> usr/src/sys
1777 drwxrwxrwt  10 root  wheel      480 Apr  6 20:28 tmp
0755 drwxr-xr-x  17 root  wheel      512 Apr  3 00:52 usr
0755 drwxr-xr-x  26 root  wheel      512 Apr  5 09:44 var
```

--Chris


----------



## Chris_H (Apr 7, 2014)

*Re: Is there any way to coerce ls(1) to produce Octal output*



			
				wblock@ said:
			
		

> `stat -f "%1Mp%3Lp %10Sp %3l %8Su%8Sg %11z %12Sa %N" *`
> 
> If you really want to do this in a script, I found some online that didn't work, but one clever suggestion that the way to do this is to convert "rwx" to 1, and "-" to 0, and then convert the resulting binary number to octal:
> 
> ...


WOW, @wblock@. Thanks for all the hard work! Kind of like "bit twiddling", or "bit flipping". It reminds me of the exercises I used to do, when learning to count/add/subtract in binary, when I was learning assembler/assembly, _years_ ago. 

Thanks! That gives me some better ideas now. 

--Chris


----------



## fonz (Apr 8, 2014)

*Re: Is there any way to coerce ls(1) to produce Octal output*



			
				Chris_H said:
			
		

> Please keep me abreast of your progress. I'd love to see your results.  Thanks!


Feel free to try my patch. Enjoy  :h 

```
# sh -c 'for foo in /bin/ls /usr/share/man/man1/ls.1.gz; do cp $foo $foo.orig; done'
# cd /usr/src/bin/ls
# patch < /path/to/patchfile
# make
# make install
```

EDIT: I almost forgot, it's the -O option (when used in conjunction with -l of course). I even updated the man() page.


----------



## Chris_H (Apr 8, 2014)

*Re: Is there any way to coerce ls(1) to produce Octal output*



			
				fonz said:
			
		

> Chris_H said:
> 
> 
> 
> ...


@fonz, you rock!

I can't wait to try it. I'll post back my experience(s). Thanks for keeping me "in the loop"!

--Chris


----------



## fonz (Apr 8, 2014)

*Re: Is there any way to coerce ls(1) to produce Octal output*

To whomever it may concern: there has been a slight update to my patch:

I accidentally left part of a second new option in.
I accidentally removed the first line of the man page (which is just a comment anyway).
Neither mistake is harmful, it's just a neatness thing.


----------



## Chris_H (Apr 8, 2014)

*Re: Is there any way to coerce ls(1) to produce Octal output*

So the link has been updated to "correct" the above?

Thanks again.

--Chris


----------



## fonz (Apr 8, 2014)

*Re: Is there any way to coerce ls(1) to produce Octal output*



			
				Chris_H said:
			
		

> So the link has been updated to "correct" the above?


Yup. If you care about it, just re-download the patch.


----------



## Chris_H (Apr 8, 2014)

*Re: Is there any way to coerce ls(1) to produce Octal output*

Excellent job, @fonz! It works like a champ. So when can we expect to see this in BASE? Thanks for doing this, @fonz. I love it.

--Chris


----------



## fonz (Apr 8, 2014)

*Re: Is there any way to coerce ls(1) to produce Octal output*



			
				Chris_H said:
			
		

> Excellent job, @fonz! It works like a champ. So when can we expect to see this in BASE?


I'm glad you like it. If enough others like it too and no obvious bugs are found, I suppose I can submit a message to e.g. freebsd-hackers@ and let them take it from there.


----------



## Chris_H (Apr 8, 2014)

*Re: Is there any way to coerce ls(1) to produce Octal output*

Excellent. I can't wait. But then again. who cares? I've already got a copy.  :h 

--Chris


----------



## fonz (Apr 8, 2014)

*Re: Is there any way to coerce ls(1) to produce Octal output*

Update: @naali spotted a bug in my code, which confuses the size of a buffer with the size of a pointer to it. This may cause failures if the size of a pointer is less than 6 (bytes) on your system. The patch has been updated again. Sorry for the inconvenience. And kudos to @naali for spotting the bug, of course.


----------



## wblock@ (Apr 8, 2014)

*Re: Is there any way to coerce ls(1) to produce Octal output*



			
				wblock@ said:
			
		

> ```
> % echo '-rw-r--r--' | tr "rwx" "1" | tr "-" "0"
> 0110100100
> ```



It occurred to me that the simpler and clearer way of doing this is with only one call to tr(1):

```
% echo '-rw-r--r--' | tr -- '-rwx' '0111'
0110100100
```


----------



## Chris_H (Apr 9, 2014)

*Re: Is there any way to coerce ls(1) to produce Octal output*



			
				wblock@ said:
			
		

> wblock@ said:
> 
> 
> 
> ...


HAH! If I hadn't been so busy with $work, and a documentation project, I might have thought of that myself... nah, who am I kidding. Nice catch, @wblock@. Thanks for pointing it out. 

--Chris


----------

