small formatting fix, refactor, remove 1st line
[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 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 if $first; then
37 first=false
38 else
39 # if we did want the first line, it would need to be stripped of non-printing chars:
40 # line=${line#$'\E[00m'}
41 lines+=("$line")
42 [[ ! ${line:10:1} == " " ]] && aclchar=true
43 fi
44 done< <( "$binls" -lAh --color=always "--time-style=+%m-%d %Y
45 %m-%d %I:%M %P" "$@" )
46 for line in "${lines[@]}"; do
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 initial_space="${y%%[![:space:]]*}"
66 hardLinks="${y#$initial_space}" # remove any initial spaces
67 hardLinks="${hardLinks%%[[:space:]]*}" # remove everything beyond first word
68 hardLink_spacing=$(( ${#initial_space} + ${#hardLinks} )) # length of hardlink string including padding
69 middle=${y#*[^ ]* }
70 size=${middle#*[^ ]* *[^ ]* }
71 middle=${middle%"$size"}
72 tail=${size#*[^ ]* }
73 size=${size%"$tail"}
74 declare -i sizePadding="${#size} - 1"
75 size=( $size ) # remove spaces
76 size=${size/.?/}
77 # ignore the hardlinks that files/dirs always have
78 if [[ ${line:0:1} == d ]]; then
79 hardLinks=$(( hardLinks - 2 ))
80 else
81 hardLinks=$(( hardLinks - 1 ))
82 fi
83 [[ $hardLinks == 0 ]] && hardLinks=
84 printf "%s%4o%${hardLink_spacing}s%s%${sizePadding}s%s\n" \
85 "${line:0:1}" $perm "$hardLinks" " $middle" "$size" " $tail"
86 fi
87 done
88 }
89