alias - cd followed by ls - linux

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

Related

In my shell script I would like to list all the files in my current directory

In my shell script I would like to list all the files and directories in my current directory.
I know the command is ls, but I have no idea how to run it in a shellscript.
Thanks for anyhelp.
So you're basically asking: what is a shell-script, how do I create one, and how do I run it ....
Use your editor of choice to create a file, give it the following content:
#!/bin/sh # change to your preferred shell, sh being a low common denominator
command # in your immediate question that would be `ls`
Save the file.
Run chmod u+x file (not the word file, but what you called your saved script).
Then you can execute your file like so:
./file

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.

Cygwin bash files

About a year ago, I created a couple text files called "compile" and "pull." When I go into a cygwin prompt and type those names and hit enter (basically use them as a command), the cygwin terminal runs what is in those text files. For instance here is the contents of one:
git checkout master
git checkout -- .
I don't even remember how I did this. I'm pretty sure this is not a bash script.
I do remember that I had to not just create the file in notepad but also perform some linux command line operation on it, in order to use it. Once I did that I could basically use the file as a command.
In *nix, you have to make a file executable in order to be able to run it:
chmod u+x file
You also need to add the path to the file to the PATH variable
PATH=$PATH:/path/to/the/file
or, add . to always scan the current directory for commands (it's considered unsecure, though):
PATH=$PATH:.

Assign directory to variable in a source file

I am building a source file with some alias to executable files (these are working just fine) and assigning directories to variables in order to get to the directory quicker, with less typing. For example, if I source example.source:
#!/usr/bin/bash
mydir="/path/to/some/dir"
I can get to /path/to/some/dir with
cd $mydir
However, I am not being able to use tab complete to navigate through other sub-directories like I would do by typing the complete path. I mean, if I use the tab key to complete the variable I get cd $mydir but not cd $mydir/ (I have to delete the last space character and manually type the slash / to see the next sub-directories). Hope this is an understandable question. Is there any workaround for this?
EDIT: the linux distribution I'm using is Slackware Linux 3.2.31.c x86_64 GenuineIntel GNU/Linux
EDIT2: GNU bash, version 4.2.37(2)-release
Apparently this feature is starting to be implemented in bash 4.3, release 26-Feb-2014 09:25.
Reading the NEWS file in bash 4.3 I found this:
i. The word completion code checks whether or not a filename
containing a
shell variable expands to a directory name and appends `/' to the word
as appropriate. The same code expands shell variables in command names
when performing command completion.
Unfortunately I cannot do a de novo installation of bash (because I'm working on a server) but I hope this can help others.
If I understand your question, then I believe it can be solved by putting this at the top of your example.source. This will list your contents every-time that you cd.
#!/usr/bin/bash
# Make cd change directories and then list the contents
function cd() {
builtin cd $*;
ls;
}
mydir="/path/to/some/dir"
cd $mydir
My other suggestion is to try to put cd within your alias. Something like this:
mydir="cd /path/to/some/dir"
$mydir

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

Resources