-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatdir
executable file
·84 lines (76 loc) · 2.57 KB
/
catdir
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
#!/bin/bash
function display_help() {
echo "Usage: catdir [OPTIONS]"
echo
echo "Utility to display contents of files in the current directory based on specific patterns."
echo
echo "Options:"
echo " -d, --directory <directory> Specify the directory to operate on. If not provided, use the current directory."
echo " -e, --exclude <pattern> Exclude files matching the pattern."
echo " -i, --include <pattern> Include only files that match the pattern. Multiple patterns can be specified."
echo " -g, --git Include .git/*"
echo " -h, --help Display this help message and exit."
echo " --logo Output a cat emoji and a folder emoji."
echo
echo "Examples:"
echo " catdir --directory '/home/user/documents' Operate on /home/user/documents directory."
echo " catdir --include '*.txt' --include '*.md' Only include .txt and .md files."
echo " catdir --exclude './.git/*' --include '*.txt' Exclude .git directory and only include .txt files."
echo " catdir --git Include .git/*"
echo
}
exclusions="! -path '**/.git/**'"
inclusions=""
include_only=false
directory="."
while [[ "$#" -gt 0 ]]; do
case "$1" in
-d|--directory)
shift
directory="$1"
shift
;;
-e|--exclude)
shift
exclusions="${exclusions} ! -path '$1'"
shift
;;
-i|--include)
include_only=true
shift
[ -z "${inclusions}" ] && inclusions="-path '$1'" || inclusions="${inclusions} -o -path '$1'"
shift
;;
-g|--git)
exclusions=""
shift
;;
-h|--help)
display_help
exit 0
;;
--logo)
echo "🐱📁"
exit 0
;;
*)
# Handle unexpected input
echo "Unexpected argument: $1"
echo "Use -h for help."
exit 1
;;
esac
done
if $include_only && [ -z "${inclusions}" ]; then
echo "You specified -i without any pattern."
exit 1
fi
find_cmd="find ${directory} -type f"
[ -n "${exclusions}" ] && find_cmd="${find_cmd} ${exclusions}"
$include_only && find_cmd="${find_cmd} \( ${inclusions} \)"
echo "Constructed find command: $find_cmd" # Let's print out the constructed command
eval "${find_cmd}" | while IFS= read -r file; do
echo "=====${file}====="
cat "$file"
echo
done