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