-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdumpcmdpackinfo.sh
More file actions
executable file
·260 lines (222 loc) · 7.09 KB
/
dumpcmdpackinfo.sh
File metadata and controls
executable file
·260 lines (222 loc) · 7.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/bin/bash
#
# Script Name: dumpcmdpackinfo.sh
# Description: Generate the list of files and symlinks found in /usr/bin and determine the package used to install it.
# Author: David HEURTEVENT <david@heurtevent.org>
# Date: 2025-11-16
# Version: 1.0.1
# License: MIT
#
# Usage: ./dumpcmdpackinfo.sh [options] <arguments>
#
# Purpose: Will prepare an alphabetically sorted list of files and symlinks from /usr/bin.
# Will determine for each file the package used to install it.
# Will output it to console or as a semi-colon quoted CSV file.
# Excludes '.' and '..'
#
# Prerequisites: None
#
# Dependencies: coreutils, distribution package installer
#
# Notes: use dumpcmdpackinfo.sh --help
# AI assisted code generation (Deekseek v.3.2)
#
# Changelog:
# 2025-11-16 - Version 1.0.1 - Fixed Debian/Ubuntu bug in package recognition. tries with /bin if /usr/bin fails.
# 2025-11-16 - Version 1.0.0 - Initial release.
#
# Exit Codes:
# 0 - Success
#
####
# MIT License
#
# Copyright (c) 2025 David HEURTEVENT <david@heurtevent.org> (frua.fr)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
####
# Function to display help
show_help() {
cat << EOF
Usage: $0 [OPTIONS] [DIRECTORY]
Generate the list of files and symlinks found in /usr/bin and determine the package used to install it.
OPTIONS:
-P FILE Output to FILE instead of console
--help Display this help message and exit
ARGUMENTS:
None
OUTPUT FORMAT:
CSV with semicolon separator containing:
filepath;name;pathtype;target;package
EXAMPLES:
$0 -P output.txt # List /usr/bin to output.txt
$0 --help # Show this help
FIELDS INCLUDED:
filepath: file path
name: name
pathtype: Path type: file or symlink
target: Target for symlinks
package: Package used to install the utility
BEHAVIOR:
- Output format is CSV with semicolon delimiter and quoted fields
- Will use the package manager to attempt to determine the package (dpkg for Debian, rpm for RedHat, etc)
- Ignores '.' and '..'
EOF
}
# Default values
output_file=""
# Parse command line arguments
if [ $# -gt 0 ]; then
# Store arguments in array for easier processing
args=("$@")
# Check for help first
for arg in "${args[@]}"; do
case "$arg" in
--help)
show_help
exit 0
;;
esac
done
# Check for flags first
for arg in "${args[@]}"; do
case "$arg" in
--follow-symlinks-dir)
follow_symlinks_dir=true
;;
esac
done
# Check for -P flag
for ((i=0; i<${#args[@]}; i++)); do
if [ "${args[i]}" = "-P" ]; then
if [ $((i+1)) -lt ${#args[@]} ]; then
output_file="${args[i+1]}"
else
echo "Error: -P requires an output file path" >&2
exit 1
fi
fi
done
fi
# Function to determine package
determine_package() {
local filepath="$1"
# Check if it's executable
if [[ ! -x "$filepath" ]]; then
echo ""
return
fi
# Try dpkg (Debian/Ubuntu)
if command -v dpkg >/dev/null 2>&1; then
local pkg=$(dpkg -S "$filepath" 2>/dev/null | cut -d: -f1)
if [[ -n "$pkg" ]]; then
echo "$pkg"
return
fi
#bug: try with /bin in older version as an alternative
name=$(basename "$filepath")
filepath1="/bin/$name"
local pkg=$(dpkg -S "$filepath1" 2>/dev/null | cut -d: -f1)
if [[ -n "$pkg" ]]; then
echo "$pkg"
return
fi
fi
# Try rpm (RedHat/CentOS/Fedora)
if command -v rpm >/dev/null 2>&1; then
local pkg=$(rpm -qf "$filepath" 2>/dev/null)
if [[ $? -eq 0 && -n "$pkg" ]]; then
echo "$pkg"
return
fi
fi
# Try pacman (Arch Linux)
if command -v pacman >/dev/null 2>&1; then
local pkg=$(pacman -Qo "$filepath" 2>/dev/null | awk '{print $5}')
if [[ -n "$pkg" ]]; then
echo "$pkg"
return
fi
fi
# Try apk (Alpine Linux)
if command -v apk >/dev/null 2>&1; then
local pkg=$(apk info --who-owns "$filepath" 2>/dev/null | cut -d' ' -f1)
if [[ -n "$pkg" ]]; then
echo "$pkg"
return
fi
fi
echo "COULD-NOT-BE-DETERMINED"
}
###
echo ""
echo "Starting the analysis of /usr/bin"
echo ""
# Create arrays to store file information
declare -a filepaths
declare -a names
echo "Collecting all files and sorting by name"
# Collect all files and sort by name
while IFS= read -r -d '' filepath; do
name=$(basename "$filepath")
filepaths+=("$filepath")
names+=("$name")
done < <(find /usr/bin -maxdepth 1 \( -type f -o -type l \) -print0 | sort -z)
echo "Done"
echo ""
echo "Processing files to dermine package"
echo "Output:"
echo ""
# Print CSV header to the appropriate output
if [[ -n "$output_file" ]]; then
echo "\"filepath\";\"name\";\"pathtype\";\"target\";\"package\"" > "$output_file"
echo "Writing output to $output_file ..."
else
echo "\"filepath\";\"name\";\"pathtype\";\"target\";\"package\""
fi
# Now process in sorted order
for i in "${!filepaths[@]}"; do
filepath="${filepaths[i]}"
name="${names[i]}"
# Determine path type
if [[ -L "$filepath" ]]; then
pathtype="symlink"
target=$(readlink -f "$filepath")
elif [[ -f "$filepath" ]]; then
pathtype="file"
target=""
else
continue # Skip if not file or symlink
fi
# Determine package (only for executable files)
if [[ -x "$filepath" && "$pathtype" == "file" ]]; then
package=$(determine_package "$filepath")
else
package=""
fi
# Output the record to the appropriate destination
if [[ -n "$output_file" ]]; then
echo "\"$filepath\";\"$name\";\"$pathtype\";\"$target\";\"$package\"" >> "$output_file"
else
echo "\"$filepath\";\"$name\";\"$pathtype\";\"$target\";\"$package\""
fi
done
echo ""
echo "Task completed"
echo ""