A bit more readable date
[small-misc-bash] / ll-function
1 #!/bin/bash
2 # Copyright (C) 2014 Ian Kelling
3
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16
17 # ls -lA with enhanced formatting
18 # octal permissions
19 # omited acl type specifier
20 # better hard link count: number of subdirectories or number of linked files or omitted if 0
21 # better human readable size
22 # more natural date/time format for my American raised eyes
23 ll() {
24 local x y z q perm padding line binls sizePadding middle tail size
25 local -a lines
26 binls=$(type -P ls)
27 local first=true
28 local aclchar=false
29 # there's no way to tell if ls uses the acl specifier unless we loop over the data twice
30 # the 11th char is either
31 # . for selinux context
32 # + for any other kind of acl
33 # or blank for no other kind of acl
34 # I don't want to see this generally.
35 while read line; do
36 lines+=("$line")
37 [[ ! ${line:10:11} == " " ]] && aclchar=true
38 done< <( "$binls" -lAh --color=always "--time-style=+%m-%d %Y
39 %m-%d %I:%M %p" "$@" )
40
41 for line in "${lines[@]}"; do
42 # very first line starts with some non printing chars
43 if $first; then
44 line=${line#$'\E[00m'}
45 first=false
46 fi
47 if ! [[ $line == [-dscbl][-r][-w][-xsS][-r][-w][-xsS][-r][-w][-xtT]* ]]; then
48 printf "%s\n" "$line"
49 else
50 perm=0
51 for (( x=0; x<=8; x++ )); do
52 y=${line:$(( -1*x + 9 )):1}
53 [[ $y == [tT] ]] && perm=$(( perm + 512 ))
54 if [[ $y == [sS] ]]; then
55 [[ $x == 3 ]] && perm=$(( perm + 1024 ))
56 [[ $x == 6 ]] && perm=$(( perm + 2048 ))
57 fi
58 [[ $y != [-ST] ]] && perm=$(( perm + 2**x ))
59 done
60 if $aclchar; then
61 y="${line:11}"
62 else
63 y="${line:10}"
64 fi
65 t="${y#"${y%%[![:space:]]*}"}" # remove any initial spaces
66 hardLinks="${t%%[[:space:]]*}" # remove everything beyond first word
67 z=$(( ${#y} - ${#t} + ${#hardLinks} )) # length of hardlink string including padding
68 middle=${y#*[^ ]* }
69 size=${middle#*[^ ]* *[^ ]* }
70 middle=${middle%"$size"}
71 tail=${size#*[^ ]* }
72 size=${size%"$tail"}
73 declare -i sizePadding="${#size} - 1"
74 size=( $size ) # remove spaces
75 size=${size/.?/}
76 # ignore the hardlinks that files/dirs always have
77 if [[ ${line:0:1} == d ]]; then
78 hardLinks=$(( hardLinks - 2 ))
79 else
80 hardLinks=$(( hardLinks - 1 ))
81 fi
82 [[ $hardLinks == 0 ]] && hardLinks=
83 printf "%s%4o%${z}s%s%${sizePadding}s%s\n" \
84 "${line:0:1}" $perm "$hardLinks" " $middle" "$size" " $tail"
85 fi
86 done
87 }
88