Created
May 16, 2018 23:47
-
-
Save rawiriblundell/51fe77a7c51a94555db675cc8339a02a to your computer and use it in GitHub Desktop.
Function that streamlines the regex ability of 'locate'
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
| # flocate function. This gives a search function that blends find and locate | |
| # Will obviously only work where locate lives, so Solaris will mostly be out of luck | |
| # Usage: flocate searchterm1 searchterm2 searchterm[n] | |
| # Source: http://solarum.com/v.php?l=1149LV99 | |
| flocate() { | |
| if ! command -v locate &>/dev/null; then | |
| printf '%s\n' "[ERROR]: 'flocate' depends on 'locate', which wasn't found." | |
| return 1 | |
| fi | |
| if (( $# > 1 )); then | |
| display_divider=1 | |
| else | |
| display_divider=0 | |
| fi | |
| current_argument=0 | |
| total_arguments=$# | |
| while (( current_argument < total_arguments )); do | |
| current_file=$1 | |
| if (( display_divider == 1 )); then | |
| printf '%s\n' "----------------------------------------" \ | |
| "Matches for ${current_file}" \ | |
| "----------------------------------------" | |
| fi | |
| filename_re="^\\(.*/\\)*${current_file//./\\.}$" | |
| locate -r "${filename_re}" | |
| shift | |
| (( current_argument = current_argument + 1 )) | |
| done | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment