Where are alias' stored in Ubuntu 10.04 - linux

I'm using a command which I don't know where the information is stored.
alias nup='ps ax | grep "nginx"'
Where is this alias saved?

It depends upon your environment and configurations.
For bash, I would generally put it in a .bashrc file that in a home directory.

In ubuntu alias get stored in the .bashrc file.
If you are typing alias update_linux='sudo apt-get update' in the terminal, then it will create an alias temporarily. It works until you close your terminal.
To add an alias permanently you can edit ~/.bashrc and add the alias to it:
gedit ~/.bashrc
and add alias at the end
alias update_linux='sudo apt-get update'
Don't forget to refresh the .bashrc configuration, by running:
source ~/.bashrc
for more details on creating alias you can read following blog: Codebucket.

Try
grep alias ~/.*
grep alias /etc/*
to find most aliases. In /etc/default, /etc/environment, depending on your distribution (I read: ubuntu)/version there might be more in other /etc/ -subdirs.

I am using Ubuntu 14.04, and you may put your aliases directly in .bashrc, but you may also create a file in ~/.bash_aliases, which will hold your aliases separately and load them automatically.
By default, the .bash_aliases file is not there. You will need to create it, but first make sure you create it in the same directory as your .bashrc file
To find your .bashrc, you may use this:
sudo find / -name .bashrc -print
My output was:
/root/.bashrc
/home/ddropik/.bashrc
/etc/skel/.bashrc
As mentioned by OddityOverseer and ranendra, I am probably interested in the one in my home directory, that is /home/ddropik/.bashrc. So I navigate to my home directory, cd ~/
Now create the .bash_aliases file with touch .bash_aliases and then edit it with nano .bash_aliases. Add whatever aliases you want.
You won't be able to use your newly added aliases until you open a new terminal session, or reload your profile, --bash login

It's ussually in a file in your home directory, such as .aliases or something.

The question here is:
if I have an alias named 'shortcut,' how do I find out what file is defining that as an alias?
The best and most user-friendly way to do this is this:
sudo grep -roI alias\ nameOfAliasHere=\' /etc/ /home/yourUserName/
-r tells grep to read everything in the directory, and go through the directories recursively, Without it, grep will complain about what you want to do.
-o means print only the part of the line that matches your string
-I suppresses binary files in the results, because those will not help you find out where your alias is
The backslashes mean "treat the next character as part of the string, instead of the normal way you interpret it"
The command will go through everything, including subdirectories, in your home folder and in /etc
If you want to start with the most likely places, just do your home directory:
sudo grep -roI alias\ nameOfAliasHere=\' /home/yourUserName/
To search everywhere it's likely to be defined or mentioned, which can be handy, this:
sudo grep -oIr alias\ ls=\' / --exclude-dir={sys,proc,srv,media,tmp,sbin,bin,boot,mnt,recovery,run,backups,var}
A lot of things like to make the 'ls' command fancier, for example. Check out the comparison below. I also included 'time' at the beginning for kicks:
You can see that there are a couple places outside of your home dir and /etc that have that alias, and it's also defined in both .alias and .bashrc. Personally, I like to throw my custom aliases in a file called .alias, and then tell everything to source it. If you're having trouble with an alias you're trying to define, that's handy. The things you see in the ~/Downloads and .cache directories won't affect your active aliases. Same with the /usr directory.
The file in /etc/skel is used to create home directories for new users, so anything there doesn't affect you. If something shows up in /etc/profile though, that will.
You can also see that the root user has an alias for ls.

Related

Can I use source command in aliases?

I am trying to create bash scripts. I would like them to run in my current shell so that, for instance, when I create a directory I want to be redirected into the new directory without having to type cd and the path of the new directory.
All my scripts are saved in a bin folder in my home directory.
This is an example of a bash script called test.sh:
#!/bin/bash
mkdir /path/of/the/directory
cd /path/of/the/directory
Is it a good practice to create an alias and use source command in the alias like below?
alias ="source $HOME/bin/test.sh"
Thank you very much in advance for your help!!!
Thats absolutely fine. For example, I have an alias in my .bashrc that sources .bashrc:
alias rebash='source ~/.bashrc'
No, there is absolutely no reason to do that.
This sounds vaguely like you should be creating a function which contains the code, and not have an alias or an external file at all.
g () {
mkdir -p /path/of/the/directory
cd /path/of/the/directory
}
Put this in your .bashrc or similar. Maybe if you want it in a separate file, create a file $HOME/bin/interactive.bash and then just source $HOME/bin/interactive.bash from your .bashrc.
It is fine if you use source in an alias.
However, as a general practice, you should define all your aliases in ~/.bash_aliases and source them in ~/.bash_profile using source ~/.bash_aliases so that once a new shell is launched it will load all the available aliases.

creating an alias and then store?

I want to create an alias like cp, mv, and rm commands then somehow prompts the user for confirmation and store the commands in the bashrc file.
how would I do this? do I just create an alias and then copy them or append them into the ~/.bashrc file?
Yes, just copy the exact alias command like it was in the console, into the ~/.basrc file.
Remember that the new alias will take effect after you relaunch the terminal emulator (strictly speaking, the bash process).
Edit:
The obligatory link to the high-rated SO question on the topic.

Is it possible to edit the 'cd' ubuntu command or add functionality with bash?

So,
cd .. moves back into the parent directory.
I'd like to add functionality so that I could type...
cd ...
... and move into the parent directory's parent directory.
and consequently move up an additional tier for each extra .
The idea came from an SO answer about a script called 'up' which should do essentially the same thing. But I'm curious if it'd be possible to just add to the cd command.
After a quick search I've noticed that cd is a bash builtin so I don't think it'll be possible to edit any original code. Would it be possible to create a new cd(.sh) script that executes in place of the builtin cd command when valid arguments are provided? What other ways might this be accomplished by?
Note: this is more for learning than practical application, I just think it'd be a cool thing to do.
Thanks!
You can define an alias in your .bashrc file:
alias ...='cd ../..'
and after that you can issue ... to go up two directories. If you only want that alias for cd it will work.
You can add the following lines to the file ~/.bashrc
alias cd..='cd ..'
alias cd...='cd ../..'
and so on.
After adding this lines close the terminal and open an new one. There you can us cd.. to go one directory up, cd... to go two directories up ...

Add a bash script to path

I want to add a small script to the linux PATH so I don't have to actually run it where it's physically placed on disk.
The script is quite simple is about giving apt-get access through a proxy I made it like this:
#!/bin/bash
array=( $# )
len=${#array[#]}
_args=${array[#]:1:$len}
sudo http_proxy="http://user:password#server:port" apt-get $_args
Then I saved this as apt-proxy.sh, set it to +x (chmod) and everything is working fine when I am in the directory where this file is placed.
My question is : how to add this apt-proxy to PATH so I can actually call it as if it where the real apt-get ? [from anywhere]
Looking for command line only solutions, if you know how to do by GUI its nice, but not what I am looking for.
Try this:
Save the script as apt-proxy (without the .sh extension) in some directory, like ~/bin.
Add ~/bin to your PATH, typing export PATH=$PATH:~/bin
If you need it permanently, add that last line in your ~/.bashrc. If you're using zsh, then add it to ~/.zshrc instead.
Then you can just run apt-proxy with your arguments and it will run anywhere.
Note that if you export the PATH variable in a specific window it won't update in other bash instances.
You want to define that directory to the path variable, not the actual binary e.g.
PATH=$MYDIR:$PATH
where MYDIR is defined as the directory containing your binary e.g.
PATH=/Users/username/bin:$PATH
You should put this in your startup script e.g. .bashrc such that it runs each time a shell process is invoked.
Note that order is important, and the PATH is evaluated such that if a script matching your name is found in an earlier entry in the path variable, then that's the one you'll execute. So you could name your script as apt-get and put it earlier in the path. I wouldn't do that since it's confusing. You may want to investigate shell aliases instead.
I note also that you say it works fine from your current directory. If by that you mean you have the current directory in your path (.) then that's a potential security risk. Someone could put some trojan variant of a common utility (e.g. ls) in a directory, then get you to cd to that directory and run it inadvertently.
As a final step, after following the solution form proposed by #jlhonora (https://stackoverflow.com/a/20054809/6311511), change the permissions of the files in the folder "~/bin". You can use this:
chmod -R 755 ~/bin
make an alias to the executable into the ~/.bash_profile file and then use it from anywhere or you can source the directory containing the executables you need run from anywhere and that will do the trick for you.
adding to #jlhonora
your changes in ~./bashrc or ~./zshrc won't reflect until you do
source ~./zshrc or source ./bashrc , or restart your pc

extending default lib search path in ubuntu

How can i extend default lib search path in ubuntu(in a way that it is also persistent) ? no, I do not want export LD_LIBRARY_PATH based temporary solution, rather some way to extend the default lib search path ?
while google-ing, I cam across some info, that in ubuntu the default search path resides in /etc/ld.so.conf.d , but editing libc.conf does not extended the default path.. so i think either i am doing it wrong, or something is missing...
the edited libc.conf looks like...
# libc default configuration
/usr/local/lib:/path_to_my_libraries/lib
create (as root) a new file in /etc/ld.so.conf.d/ containing, the new path. For example:
sudo echo "/path-to-your-libs/" >> /etc/ld.so.conf.d/your.conf
after that run
sudo ldconfig
No need to change libc.conf.
Using sudo, without becoming root
This will create a your.conf file with a reference to /path-to-your-libs/:
$ echo '/path-to-your-libs/' |sudo tee -a /etc/ld.so.conf.d/your.conf
Do not forget to finish off with a dynamic link library cache refresh:
$ sudo ldconfig

Resources