creating an alias and then store? - linux

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.

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.

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 ...

Referencing the HOME path in Cygwin aliases

I use a lot of aliases in my Cygwin .bash_profile as a way to cd to various lengthy network drives. I am also modifying this .bash_profile file frequently as new drives need to be accessed.
I'm trying to create an alias that will open my .bash_profile in a text editor and allow me to quickly edit it from anywhere. Let's use the following as an example:
alias editbash='subl ~/.bash_profile'
"subl" is another alias for the path to my Sublime Text executable.. this works fine.
My main issue is that when I execute this alias, it attempts to open a file at the following path:
C:\home\username\.bash_profile
... instead of...
C:\cygwin\home\username\.bash_profile
I have attempted to modify this alias to use the following:
alias editbash='subl /cygwin/home/username/.bash_profile'
This works, but only if I'm currently viewing something in my C: drive, which I am often not.
How can I update this alias to always reference my Cygwin user home directory, rather than the Windows home directory? Or, if it's easier, how can I always reference the C: drive in my latter example?
Try referencing the home directory as /cygdrive/c/cygwin/home/username
That should solve the problem.
You can edit you home variable in the /etc/passwd file. Use pwd to find out where you are. Then the tilde can point where you actually want it to.
The shellslash option seemed worth a try in spite of the warnings about it breaking things.
So before that, I tried a few different aliases in .bashrc - AND ONE SIMPLE THING WORKED by prepending a backslash:
alias vib='vi "\$HOME/.bashrc"'
... and works just as I wanted - Thanks!

alias - cd followed by ls

How can I define an alias so that when I do cd Abcd, where 'Abcd' is the name of a directory, the directory is changed to 'Abcd' and is followed by ls to show the contents of the directory?
I believe you can't use an alias to accomplish that, but you could define a function to do it:
#print contents after moving to given directory
cl()
{
cd $#
ls
}
You could stick this in your ~/.bashrc file.
If you were hoping to override the builtin cd command, then you could do:
#print contents after moving to given directory
cd()
{
builtin cd $#
ls
}
UNIX
Creating an alias
Your Linux distribution will most likely not have the .bash_aliases file created in your home, or you can even create it manually. To create the file, type in the following command:
touch ~/.bash_alisaes
Now that file will be executed automatically every time you fire off a new Terminal.
What you can do now is create a list of aliases and add them to that file for later uses.
create an alias and update the ~/.bash_aliases file to make it permanent.
Generic approach: Creating a custom script
Create a bash script in your /usr/bin folder, it should look something like this
#!/bin/bash
Whatever combination of commands you want to run when you type this thing.
Its really that easy.
Just name the bash script what you want to type in to the terminal, and make it excecutable: chmod +x filename and you're good to go!
WINDOWS
You can use DOSKEY command:
From Wikipedia:
DOSKey is a utility for MS-DOS and Microsoft Windows that adds command
history, macro functionality, and improved editing features to the
command line interpretersCOMMAND.COM and cmd.exe. It was included as a
TSR program with MS-DOS and PC-DOS versions 5 and later, and with
Microsoft's Windows 95/98/Me.
For example: To create a macro that quickly and unconditionally formats a disk, type:
doskey qf=format $1 /q /u
To quickly and unconditionally format a disk in drive Z, type:
qf Z:
To define a macro with multiple commands, use $t to separate commands, so the solution to your problem follows:
doskey cd=cd $1$tdir
Now, this will work only in your currently open command window. To make it permanent simply create a batch file and set the value of the absolute path of the file to the regedit
HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun
Source for the regedit: superuser.com/a/238858

Where are alias' stored in Ubuntu 10.04

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.

Resources