Created
February 18, 2026 10:35
-
-
Save steve-core/b1544822fe1e13f1124703a49fbc10d2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # Author: S Firth | |
| # Created: 21/06/23 | |
| # Last modified: 28/09/23 | |
| # Version: 0.3 | |
| # Description: List files and output to file: simple list, recursive, by file type | |
| # Usage: alf [-r|h|t] | |
| echo "loaded list function" | |
| alias alf='anastasias_listing_function' | |
| function alf_help() { | |
| echo "Syntax: alf [-r|h|t]" | |
| echo "no switch = list all in current directory" | |
| echo "r = recursive" | |
| echo "t STRING = file type" | |
| echo "h = this help" | |
| } | |
| function anastasias_listing_function() { | |
| OPTIND=1 | |
| recursive=false | |
| file_type='' | |
| all_files='' | |
| output_file="FILE_LIST.txt" | |
| #set some shiz | |
| while getopts "rt:h" options; do | |
| case "${options}" in | |
| r) | |
| recursive=true | |
| ;; | |
| t) | |
| file_type=${OPTARG} | |
| ;; | |
| h) | |
| alf_help | |
| ;; | |
| :) | |
| echo "Error: -${OPTARG} requires an argument." | |
| alf_help | |
| ;; | |
| *) | |
| alf_help | |
| ;; | |
| esac | |
| done | |
| alf_write | |
| } | |
| # run in subshell to look cool | |
| alf_write() ( | |
| #reset file_count | |
| file_count=0 | |
| # save Internal Field Seperator | |
| oldIFS=$IFS | |
| # swap IFS to use line breaks, not spaces | |
| IFS=$'\n' | |
| # search type | |
| if [[ $recursive == true && -n "${file_type}" ]] ; then | |
| all_files=$(find . -name "*.${file_type}") | |
| echo "$(tput setaf 4)Recursive$(tput sgr0) search for $(tput setaf 4)${file_type}$(tput sgr0)" | |
| elif [[ $recursive == false && -n "${file_type}" ]] ; then | |
| all_files=$(ls | grep ${file_type}) | |
| echo "Search for $(tput setaf 4)${file_type}$(tput sgr0)" | |
| elif [[ $recursive == true ]] ; then | |
| all_files=$(find . -name "*") | |
| echo "$(tput setaf 4)Recursive$(tput sgr0) search for $(tput setaf 4)all file types$(tput sgr0)" | |
| else | |
| all_files=$(ls) | |
| echo "Search for $(tput setaf 4)all file types$(tput sgr0)" | |
| fi | |
| # clear old FILE_LIST.txt | |
| if [[ -f "./${output_file}" ]]; then | |
| echo "" > ${output_file} | |
| fi | |
| # write each file name to file | |
| for eachfile in $all_files | |
| do | |
| if [[ -f "$eachfile" ]]; then | |
| # hide output_file from results | |
| if [[ ! "$output_file" == *"$eachfile"* ]]; then | |
| echo ">>$eachfile<<" >> ${output_file} | |
| # echo ">>$eachfile<<" | |
| ((file_count+=1)) | |
| fi | |
| fi | |
| done | |
| printf "$(tput setaf 2)$file_count$(tput sgr0) files listed in $(tput setaf 2)${output_file}$(tput sgr0) \n" | |
| # restore IFS etc | |
| IFS=$oldIFS | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment