3

Eg: Find command to search for txt files

find . -iname "*.txt" 

I want to create an alias of find command in .bash_profile so that upon sourcing .bash_profile I should be able to pass any arguments to find command to search the file types:

find txt find pdf find doc  

I tried below syntax to override find command as:

find ./ -iname "$1"

where $1 can be any argument file type.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
camelCase
  • 61
  • 5
  • I would recommend using a new name. Aliases don't handle positional arguments well; use a function. Your notation `find txt find pdf find doc` is odd; was that meant to be 3 separate commands or 1 compound command? (Before editing, they were all on one line, so I left them on one line, but it seems more likely that you intended 3 separate commands.) – Jonathan Leffler Feb 18 '15 at 00:44

3 Answers3

1

Why not write a bash script?

#!/bin/sh

for var in "$@"; do
    find . -iname "*.$var"
done

You can store this in a file, make it executable (chmod +x find-by-type), then copy it to a directory within your PATH variable. Executing would be as easy as calling:

[user@localhost ~]$ find-by-type pdf txt doc
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
  • 1
    I prefer standalone scripts to either aliases (in particular) or functions. There are occasions for the alternatives, but speed isn't going to be an issue with a `find` command, and the cleanliness of a separate script is a good idea. – Jonathan Leffler Feb 18 '15 at 00:46
  • 1
    You might think about using this: `command=( find . -iname "*.$1" ); shift; for opt in "$@"; do command+=( -o -iname "*.$opt" ); done; "${command[@]}"` which builds up a single `find` command to run all the searches in a single execution of `find`. That is more economical (especially on big trees) than running multiple `find` commands. You could prefix the final command with `exec` for a marginal (non-measurable) gain. The downside is that the file types would be interleaved in the output, rather than segregated by file suffix. – Jonathan Leffler Feb 18 '15 at 00:55
  • Good idea, @JonathanLeffler. In addition, you'd also get the files back ordered by folder/name instead of by type/folder/name. – Chris Forrence Feb 18 '15 at 00:58
1

Open your .bashrc file (it is usually hidden press Ctrl+H to see) in your home directory and append this line.

alias finder="find . -type f -regex "

Note I use -regex which will help you match your file extension patterns.

Then execute in a shell:

source .bashrc

Afterwards, call your alias named finder with your filename/extension.

For example:

finder '*\(txt\|py\|pdf\|lst\)'
erakitin
  • 11,437
  • 5
  • 44
  • 49
repzero
  • 8,254
  • 2
  • 18
  • 40
  • One thing to be careful about: This would match the file named "CntxtSnstv.png". You'd need to call it without the second asterisk at the end! – Chris Forrence Feb 18 '15 at 00:33
0

Thanks for the suggestions. I tried something like writing a function in .bash_profile:

findFile(){
    find $1 -iname "*.$2"
}

sourced .bash_profile

export -f findFile

findFile ~/Desktop txt
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
camelCase
  • 61
  • 5