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