7a1c62378fb0dd7d592e5f7282b51da8419c6dac
[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 output
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 perm line binls sizePadding middle tail size \
25 max_hl_digits hardlinks initial_space hardlink_spacing
26 local max_hl_digits=0
27 local -a lines hl
28 binls=$(type -P ls)
29 local first=true
30 local aclchar=false
31 # there's no way to tell if ls uses the acl specifier unless we loop over the data twice
32 # the 11th char is either
33 # . for selinux context
34 # + for any other kind of acl
35 # or blank for no other kind of acl
36 # I don't want to see this generally.
37 while read line; do
38 if $first; then
39 first=false
40 else
41 # if we did want the first line, it would need to be stripped of non-printing chars:
42 # line=${line#$'\E[00m'}
43 lines+=("$line")
44 [[ ! ${line:10:1} == " " ]] && aclchar=true
45
46 y="${line:11}"
47 initial_space="${y%%[![:space:]]*}"
48 hardlinks="${y#$initial_space}" # remove any initial spaces
49 hardlinks="${hardlinks%%[[:space:]]*}" # remove everything beyond first word
50 # ignore the hardlinks that files/dirs always have
51 if [[ ${line:0:1} == d ]]; then
52 hardlinks=$(( hardlinks - 2 ))
53 else
54 hardlinks=$(( hardlinks - 1 ))
55 fi
56 [[ $hardlinks == 0 ]] && hardlinks=
57 if (( ${#hardlinks} > max_hl_digits )); then
58 max_hl_digits=${#hardlinks}
59 fi
60
61 hl+=($hardlinks)
62
63 fi
64 done< <( "$binls" -lAh --color=always "--time-style=+%m-%d %Y
65 %m-%d %I:%M %P" "$@" )
66
67 hardlink_spacing=$((max_hl_digits + 1))
68
69 for index in "${!lines[@]}"; do
70 line=${lines[index]}
71 hardlinks=${hl[index]}
72 if ! [[ $line == [-dscbl][-r][-w][-xsS][-r][-w][-xsS][-r][-w][-xtT]* ]]; then
73 printf "%s\n" "$line"
74 else
75 perm=0
76 for (( x=0; x<=8; x++ )); do
77 y=${line:$(( -1*x + 9 )):1}
78 [[ $y == [tT] ]] && perm=$(( perm + 512 ))
79 if [[ $y == [sS] ]]; then
80 [[ $x == 3 ]] && perm=$(( perm + 1024 ))
81 [[ $x == 6 ]] && perm=$(( perm + 2048 ))
82 fi
83 [[ $y != [-ST] ]] && perm=$(( perm + 2**x ))
84 done
85 if $aclchar; then
86 y="${line:11}"
87 else
88 y="${line:10}"
89 fi
90 middle=${y#*[^ ]* }
91 size=${middle#*[^ ]* *[^ ]* }
92 middle=${middle%"$size"}
93 tail=${size#*[^ ]* }
94 size=${size%"$tail"}
95 declare -i sizePadding="${#size} - 1"
96 size=( $size ) # remove spaces
97 size=${size/.?/}
98
99 printf "%s%4o%${hardlink_spacing}s%s%${sizePadding}s%s\n" \
100 "${line:0:1}" $perm "$hardlinks" " $middle" "$size" " $tail"
101 fi
102 done
103 }
104