1+ #! /bin/bash
2+ 
3+ #  Default values
4+ STARTING_DIR=" " 
5+ SKIP_CONFIRMATION=false
6+ SKIP_TUTORIALS=false
7+ 
8+ #  Function to display usage information
9+ usage () {
10+     echo  " Usage: $0  [OPTIONS] DIRECTORY" 
11+     echo  " Delete markdown files from the specified directory and its subdirectories." 
12+     echo  " " 
13+     echo  " Options:" 
14+     echo  "   --skip              Skip confirmation prompt" 
15+     echo  "   --skip-tutorials    Preserve files in directories named 'tutorials'" 
16+     echo  "   -h, --help          Display this help message" 
17+     echo  " " 
18+     echo  " DIRECTORY argument is required." 
19+     exit  1
20+ }
21+ 
22+ #  Process command line arguments
23+ while  [ $#  -gt  0 ];  do 
24+     case  " $1 "   in 
25+         --skip)
26+             SKIP_CONFIRMATION=true
27+             shift 
28+             ;;
29+         --skip-tutorials)
30+             SKIP_TUTORIALS=true
31+             shift 
32+             ;;
33+         -h|--help)
34+             usage
35+             ;;
36+         -* )
37+             echo  " Unknown option: $1 " 
38+             usage
39+             ;;
40+         * )
41+             #  First non-option argument is the directory
42+             STARTING_DIR=" $1 " 
43+             shift 
44+             ;;
45+     esac 
46+ done 
47+ 
48+ #  Ensure a directory was specified
49+ if  [ -z  " $STARTING_DIR "   ];  then 
50+     echo  " Error: No directory specified." 
51+     usage
52+ fi 
53+ 
54+ #  Ensure the specified directory exists
55+ if  [ !  -d  " $STARTING_DIR "   ];  then 
56+     echo  " Error: Directory '$STARTING_DIR ' does not exist or is not accessible." 
57+     exit  1
58+ fi 
59+ 
60+ #  Script to find, print, and delete markdown files in the specified directory and subdirectories
61+ echo  " Searching for markdown files in: $STARTING_DIR " 
62+ if  [ " $SKIP_TUTORIALS "   =  true  ];  then 
63+     echo  " Note: Files in directories named 'tutorials' will be preserved." 
64+ fi 
65+ 
66+ #  Build the find command based on options
67+ if  [ " $SKIP_TUTORIALS "   =  true  ];  then 
68+     #  Find markdown files but exclude those in 'tutorials' directories
69+     files=$( find " $STARTING_DIR "   -type f \(  -name " *.md"   -o -name " *.markdown"   \)  |  grep -v " /tutorials/" ) 
70+ else 
71+     #  Find all markdown files
72+     files=$( find " $STARTING_DIR "   -type f \(  -name " *.md"   -o -name " *.markdown"   \) ) 
73+ fi 
74+ 
75+ #  Count the total number of markdown files that match our criteria
76+ total_files=$( echo " $files "   |  wc -l) 
77+ if  [ -z  " $files "   ];  then 
78+     total_files=0
79+ fi 
80+ echo  " Found $total_files  markdown files that meet deletion criteria." 
81+ 
82+ #  Safety confirmation (unless --skip is used)
83+ if  [ " $total_files "   -gt  0 ] &&  [ " $SKIP_CONFIRMATION "   =  false  ];  then 
84+     read  -p " Are you sure you want to DELETE these markdown files? (yes/no): "   confirmation
85+     if  [ " $confirmation "   !=  " yes"   ];  then 
86+         echo  " Operation cancelled." 
87+         exit  0
88+     fi 
89+ fi 
90+ 
91+ #  Process each file
92+ if  [ " $total_files "   -gt  0 ];  then 
93+     echo  " $files "   |  sort |  while  read  -r file;  do 
94+         echo  " Deleting: $file " 
95+         rm " $file " 
96+     done 
97+     echo  " Deletion complete." 
98+ else 
99+     echo  " No files to delete." 
100+ fi 
0 commit comments