Shell Script

Forum rules
Before you post please read how to get help
Post Reply
jrustad24
Level 1
Level 1
Posts: 1
Joined: Sat Nov 21, 2020 3:31 pm

Shell Script

Post by jrustad24 »

I need to create a variable script that uses the "find" utility to search the home directory for files. Example; (./findFile.sh data) The search also needs to be case-insensitive. The results should look like what is in the attachment.
Attachments
Capture6.PNG
Rootbeer
Level 2
Level 2
Posts: 72
Joined: Sun Nov 24, 2019 3:22 am

Re: Shell Script

Post by Rootbeer »

jrustad24 wrote:
Sat Nov 21, 2020 3:33 pm
I need to create a variable script that uses the "find" utility to search the home directory for files. Example; (./findFile.sh data) The search also needs to be case-insensitive. The results should look like what is in the attachment.

Code: Select all

#!/bin/bash

find $HOME -iname $1
chmod 700 findfile.sh
./findfile.sh notes


or make an alias and put it into /home/user/.bashrc

Code: Select all

alias findfile='find $HOME -iname'
reload the bashrc:

Code: Select all

source .bashrc
and then just type

Code: Select all

findfile notes
User avatar
Termy
Level 5
Level 5
Posts: 917
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Shell Script

Post by Termy »

You might want to try using a function for this instead, if it suits your needs.

Code: Select all

# Quickly find notes in user's HOME.
notefind(){
	for Str in "$@"; {
		printf "Searching for '%s' ...\n" "$Str"
		find "$HOME" -xdev -type f -iname "$Str" \
			-printf '~/%P\n' 2> /dev/null
	}
}
This function will let you supply any number of strings (paths/files), and will restrict the results to files only. You might also like its output, as it's more user-friendly, I guess you could say. It'll also stay within the one filesystem, in-case you for some reason have mounts in your HOME. Lastly, any errors will not be displayed, to avoid permission-related ones.

This is BASH syntax, btw.
I use Linux Mint 18.3 with Cinnamon in a VirtualBox VM for testing & sandboxing.

I'm LearnLinux (LL) on YouTube: https://www.youtube.com/channel/UCfp-lN ... naEE6NtDSg
I'm also terminalforlife (TFL) on GitHub: https://github.com/terminalforlife
Post Reply

Return to “Scripts & Bash”