Automatically appending --exclude-dir=xxxx (multiple dirs) to grep bash command? - linux

I'd like to automatically exclude several 3rd party library folders so I don't have to retype it every time. How would one accomplish such a task?

You can either create an alias
alias grep='grep --exclude-dir=xxxx'
that you would add to your .bashrc, or setup an environment variable
export GREP_OPTIONS='--exclude-dir=xxxx'
that you would add to your .bash_profile.
Note that aliases only work at the beginning of a command line (start of line, or after a pipe, or an opening parenthese, etc.) Consequently, ... | xargs grep ..., for instance, won't use the alias. By contrast, the env variable will be honored, which can have its downsides as mentioned below.

You can alias grep to always include the parameter. On your ~/.bashrc:
alias grep="grep --exclude-dir=xxxx"

Related

How to make shortcut for frequently used terminal commands in ubuntu

I have this command :
$ anbox session-manager --single-window --window-size=400,650
and i need to run it frequently, is there any way to avoid rewriting it every time I need it ?
For permanently use
run this
echo 'alias customcommand="anbox session-manager --single-window --window-size=400,650"' >> /home/$(USER)/.bashrc
open new terminal
now you can run customcommand and get that response
You can set an alias like this,
alias sessmgr = "anbox session-manager --single-window --window-size=400,650"
Later, you can directly use sessmgr.
You can use the alias command, and if you want to make the change permanent you can append the alias command in your .bashrc file, so every time you launch a terminal you will have by default the defined alias that you set before in your .bashrc file.
e.g.
alias <name_of_the_alias>="<command>".
You can find more info regarding alias here
There are several ways you can do this, the most common is probably using an alias
A common alias is using 'll' for 'ls -al', one way you could do that is with the following command:
alias ll='ls-al'

Aliases are not executed in Shell script

In my bashrc file I have n number of alias. But, If I execute via shell script,
it will not give expected output. Why it will be like this. Is there any way to
solve this problem.
Thanks in advance.
Aliases (as set using alias name=value) are only used in an interactive context, i. e. when the user types something on the command line. They are never executed by a script (unless a non-interactive shell is explicitly tweaked to do this using the shopt -s expand_aliases):
#!/bin/bash
alias ttt=date
ttt # will fail!
Sourcing a configuration script which defines aliases will not change anything about this. Scripts simply will not execute aliases.
To achieve what you want, rewrite your aliases as shell functions:
#!/bin/bash
ttt() {
date
}
ttt # will succeed!
Shell functions can replace aliases completely but there are some more things to know and consider:
You can even export shell functions so that child shells also have them. Use export -f ttt for this.
Shell functions can override other commands so they can interfere in the behaviour of scripts (unlike aliases which are never executed in scripts). Keep this in mind in case you plan to override things like cd or ls.
An overridden built-in of the shell (e. g. cd) can still be reached by calling it as builtin cd /my/direc/tory.
Argument handling is quite different from aliases (and much more powerful).

How to use grep to find data conveniently and quickly

I am asking for general opinions on how to find files quickly.
One typical scenario is that I often need to grep some names from a PeopleNameFile.txt from Dir1. When I'm in a different directory, I am forced to grep the file with a long directory path. It would be nice to just do GREP "Warren Buffett" PeopleNameFile.txt.
BTW, I'm using Cygwin but I welcome any suggestions.
You can easily write a simple bash function in your ~/.bashrc:
function grnm() {
grep "$#" /path/to/peoplenamefile.txt
}
Then later on, at command line you can type:
$ grnm "Warren Buffet"
the nice thing is that you can actually include other grep parameters if you like as in:
$ grnm -i "warren buffet"
(The $ characters represent your shell prompt, not part of the command you type.)
When you edit the .bashrc file FOR THE FIRST TIME you may have to source it in your existing open cygwin windows:
$ source ~/.bashrc
But if you open a new window you should not have to do that.
Good luck, have fun
You can create script my_grep.sh and add it somewhere to you path, with content like this:
#!/bin/bash
grep $1 path/to/Dir1/PeopleNameFile.txt
than you just type
my_grep.sh "Warren Buffett"
also you can use alias and bash's function,
but this require to edit "~/.bashrc".
Simplest option would be to setup an alias which would grep for that file using the absolute path. Not sure whether cygwin allows aliases though.
Probably the most common way to do these kind of things is to use environment variables
e.g.
PNF='/very/long/path/PeopleNameFile.txt'
grep "Warren Buffett" $PNF

Linux alias chain commands (can recursion be avoided?)

I've been looking around for ways to alias clear and ls into one command.
Currently I've defined command x:
alias x="clear;ls"
Now is there any walkaround to avoid recursion and define:
alias ls='clear;ls'
If you put a backslash before the command name, that will disable any aliases.
alias ls='clear;\ls'
Or, like Arnaud said, just use the full path for ls.
Another way of doing this would be
alias ls='clear; command ls'
This is different from /usr/bin/ls, as it still searches ls in the $PATH, but will ignore shell functions or aliases.
Just do :
alias ls='clear;/usr/bin/ls'
When typing:
$ ls
First of all it will search an user defined function, it will launch it, else search in $PATH commands.
By giving the explicit path of the ls command, recursion will be avoided.
There is no direct recursion in alias. From man bash:
The
first word of the replacement text is tested for aliases, but a word
that is identical to an alias being expanded is not expanded a second
time. This means that one may alias ls to ls -F, for instance, and
bash does not try to recursively expand the replacement text.
I always use ls with --color=auto parameter ( -G Enable colorized output.) and like to use functions.
clear_and_ls() {
clear
command ls --color=auto
}
alias ls="clear_and_ls"

Creating permanent executable aliases

I have MySQL installed (MAMP, Mac OS X) but need to call it by the full path each time I access it from the shell. I created an alias: alias mysql='/Applications/MAMP/Library/Bin/mysql, but this only lasts as long as my terminal/Bash session.
What is an effective way for establishing permanent aliases that will work across users? (I need to be able to execute commands from PHP). Should I set up aliases in the Bash start up script (how is that done?), or is it better to edit the sudoers file? (Could use an example of that as well..)
Thanks--
EDIT- Based on answer:
I just tried creating a ~/.bashrc and wrote the following:
alias mysql='/Applications/MAMP/Library/bin/mysql'
But this doesn't seem to have any effect. Is there a special syntax for this file?
Add the command to your ~/.bashrc file.
To make it available to all users, add it to /etc/profile.
Different shell uses different dot file to store aliases.
For mac, the bash shell uses .bash_profile or .profile
For ubuntu, the bash shell uses.bashrc
If you are using zsh shell and ohmyzsh plugin, the dot file is .zshrc
Traditionally, to add a permanent alias, you need to open the dot file and write alias manually like:
alias hello="echo helloworld"
And remember to source the dot file to have it take effect. To source the dot file on ubuntu's bash, type source .bashrc To make the alias available to all users, write to /etc/profile instead of the dot file. Remember to type source /etc/profile for the new alias to take effect.
If you simply want a temporary alias, you do not need to write to dot file. Simply type that same command (alias hello="echo helloworld) on the terminal.
Note that a temporary alias created through the alias command will disappear once the shell is closed.
If you are looking for a single command to generate aliases without open the text editor, read on.
If you have ruby installed on ubuntu, you can create permanent alias with a single command using aka.
gem install aka2
For example:
aka generate hello="echo helloworld" #will generate a alias hello="echo helloworld"
aka destroy hello #will destroy the alias hello
aka edit hello #will prompt you to edit the alias.
With aka, there is no need to write to the dot file with a text editor. And no need to source the dot file too.
You're going about this the wrong way.
Either add /Applications/MAMP/Library/bin/ to your path, or create a script to invoke MySQL and place it in a bin directory which is already in your path.
On a mac the .bashrc file does not get sourced unless you put
source ~/.bashrc in the /etc/profile or /etc/bashrc.
Just thought I would mention that.

Resources