Windows CMD Shorten file path - windows

I got Gnu Utilities that adds the command sed to windows, but to use it I have to type:
C:\ProgramFiles\GnuWin32\bin\sed.exe <args>
How do I shorten this to just sed <args>?

To run an executable without a full path, it needs to be either in the current directory, or in the PATH environment variable. In the CMD prompt, there are several ways to do this.
The first way is to put C:\ProgramFiles\GnuWin32\bin in your PATH variable, which makes every program in that directory available without a full path.
set "PATH=%path%;C:\ProgramFiles\GnuWin32\bin"
This updates PATH in the current command prompt. If you need to set it for other CMD windows, see How to persistently set a variable in Windows 7 from a batch file?
The second method is to have sed.exe in the current directory. The most obvious way to do that is to change directories.
cd C:\ProgramFiles\GnuWin32\bin
sed
Or you can copy it to your current directory.
copy C:\ProgramFiles\GnuWin32\bin\sed.exe .\
sed
(This works with sed.exe because it's a self-contained utility. Don't try this with a Windows application like excel.exe)
Finally, you can create a "redirect" somewhere in the current directory or the path.
>.\sed.bat echo C:\ProgramFiles\GnuWin32\bin\sed.exe %*
This creates a batch file in the current directory called sed.bat that calls the full sed.exe. You can drop this file into any directory in your PATH.
mklink .\sed.exe C:\ProgramFiles\GnuWin32\bin\sed.exe
This creates a symlink to the sed.exe in the current directory, much like a symlink in Unix or a shortcut in Windows.

Related

Analog of $PWD in Windows

I run windows command in particular directory and I need to pass files and subdirectories in this directory with full path. How to retrieve full path of current directory? What is analog of bash $PWD?
$pwd is indeed a valid command in PowerShell as well (if you're using PowerShell). $pwd in PowerShell is basically an alias for Get-Location - both of them will give you the full path of current directory.
In case you are using cmd, use cd (just cd without any parameters)

How do I get my Windows7 symlink to execute from command line?

I have a couple of batch files I want to use regularly, so I decided I would drop them as symlinks into a binaries folder in my path. The idea being that I would use them like I would any other command, without having to change directories. E.g.
> odbimport -u User -f filename
where odbimport is my symlink to the batch file odbimport.bat.
The process I used to make the symlinks is as follows:
C:\Users\user>mklink C:\utils\odbimport C:\util-files\odbimport.bat
symbolic link created for C:\utils\odbimport <<===>> C:\util-files\odbimport.bat
C:\Users\user>path
Path=C:\....;C:\utils\
C:\Users\user>where odbimport
C:\utils\odbimport
From what I've seen, it looks like I've made the symlink, and the path knows where to find it.
However, after I've made my symlink and attempt to execute, I get:
C:\Users\user> odbimport -u me -f somefile
'odbimport' is not recognized as an internal or external command,
operable program or batch file. "
I've been looking for an answer to this with no success. Everything I find seems to deal more with how to create working symlinks than addressing my issue. The closest thing I found was this. This is essentially my question, except kinda backwards because I don't really want to run the symlinks from Windows Explorer. I have also tried adding .LNK to my PATHEXT variable as in this question.
Add .bat extension to the symlink you create because on Windows .bat extension is necessary to tell the system it's actually an executable batch file. You will still be able to run the file by typing its name only.
mklink C:\utils\odbimport.bat C:\util-files\odbimport.bat
NTSF hardlinks can be used when source and target are on the same volume, the advantage is that such clones may be executed from Windows Explorer unlike symlinks:
mklink /h C:\utils\odbimport.bat C:\util-files\odbimport.bat
fsutil hardlink create C:\utils\odbimport.bat C:\util-files\odbimport.bat

Setting environmental variable in batch as a path (not working for me) on Windows

I want to set my folder C:\Users\scruff\Desktop\MinGW2\Cross as variable PREFIX
So I type:
set PREFIX=C:\Users\scruff\Desktop\MinGW2\Cross
But then when I do:
cd PREFIX
I get
"The system cannot find the specified path".
When I do
echo %PREFIX%
I get:
C:\Users\scruff\Desktop\MinGW2\Cross
when is do:
cd C:\Users\scruff\Desktop\MinGW2\Cross
it takes me to the cross directory fine.
:(
Try cd %prefix%.
DOS/Windows batch files aren't really very clever, you might want to investigate Powershell as an alternative: http://technet.microsoft.com/en-us/scriptcenter/dd742419

How to make a shell script global?

I am on Mac's OS 10.6, and I am trying to learn a thing or two about shell scripting. I understand how to save a shell script and make it executable, but I am wondering what I can do or where I can save the file to make it global (that is, accessible no matter what folder I am in).
For example, if I save a .sh file in the /Users/username/ directory and make it executable, I can only execute that script in that specific directory. If I navigate to /Users/username/Downloads, for example, I can't execute the script.
Also, any suggestions of resources for learning more about shell scripting would be helpful. Thanks
/usr/local/bin would be the most appropriate location. Mac OS X has it in the PATH by default
There are two ways to do it -
Put your script in usr/local/bin and make sure it is executable(chmod +x my_script)(This is already set in the path, you can check by doing an echo $PATH)
Create a folder in your home directory called bin. (For your personal scripts)
cd ~ (Takes you to your home directory)
mkdir bin (create a bin folder)
vim .bash_profile (to set path environment variable)
export PATH=~/bin:$PATH (Press i then add this line and then do esc and type :wq)
Now you can just type the name of your script and run it from anywhere you want.
** NOTE: If you want to run the script with a shortened command rather than typing your entire filename, add the following to your .bash_profile:
alias myscript='my_script.sh'
Then you can run the script by simply typing myscript. (you can sub in whatever alias you'd like)
Traditionally, such scripts either go in ~/bin (ie: the bin directory in your home directory) or /usr/local/bin/ The former means the script will only work for you, the latter is for scripts you want anybody on the system to be able to run.
If you put it in ~/bin, you may need to add that to your PATH environment variable. /usr/local/bin should already be on the path.
In mac operating system
Open bash ~/.bashrc file.
add path of your script in your bashrc file , using
export PATH="$PATH:/Users/sher.mohammad/Office/practice/practiceShell"
Open your ~./bash_profile file and add [[ -s ~/.bashrc ]] && source ~/.bashrc
open new terminal window
Now whenever you will open your terminal your script will be loaded
This one is super easy if you are familiar with your bashrc file! This will entirely use just your .bashrc file and takes 2 seconds to accomplish.
(I use Arch Linux Manjaro so I use .bashrc located in my home directory)
The code to be placed in your .bashrc file:
# Simple bashrc method to launch anything in terminal from any directory
YOURCOMMAND () {
cd /path/to/directory/containing/your/script/ && ./YOURSCRIPT
}
As you can see, first you use the simple 'cd' command and give it the directory of the scripts location, then use '&&' so that you can make the next command executed right after, and finally open your script just as you would normally! Super easy and saved right in your .bash file! :)
Hope I've helped someone!
Sincerely,
AnonymousX
On using bash shell, write that script as function and then put it to the .bashrc or source the file which containing that function by "source file_name"
Now execute the script by function call in the shell.
Either saving it in /usr/bin (or any other directory present in PATH) or editing PATH to include the directory you saved it in will basically make it run in any directory.
from the working directory of 'script.sh'" mv [script.sh] /usr/local/bin"( not tested but seems to be the least complex way IMO.)
You should put it in the global executable directory on your machine. I think that would usually be /usr/bin on Unix-based operating systems (this would however most often require super user privileges on that machine).
You could also put it in any other directory that is in the $PATH environment variable, although it would only work for those users who have that directory in that variable.
You can find the value of $PATH by typing echo $PATH in a shell. The directories are separated by :.

DOS system path

Is there a way to programmatically, through a batch file (or powershell script), put all folders in c:\Program Files into the system variable PATH? I'm dependent on the command line and really want to just start a program from the command line.
Yes, I'm jealous of Linux shells.
Passing in "C:\Program Files" as a parameter into this batch file:
#echo off
FOR /D %%G IN (%1\*) DO PATH "%%G";%path%
Doing this is very likely to break your computer, in the sense of invoking DLL Hell. As you invoke each executable, the OS will look through each directory in PATH to find each DLL or even EXE referenced by that executable. It becomes highly likely that the OS will find the wrong ones as you add more directories to the PATH.
So, a best practice is to avoid increasing the PATH, and even to decrease it. Rather than implicit dependencies, make them explicit.
Instead, I recommend this approach:
Create a bin directory within your user home directory
Add that bin directory to your user PATH variable
Create a Windows CMD script in the bin directory for each application that you want to invoke from the command line (same name as the executable that you would type)
In each script, invoke SetLocal, add the application's install directory (under %ProgramFiles%) to the PATH, then invoke the executable with the arguments from the command line
Remove the relevant directory from the PATH, so that this script becomes the only way to invoke the executable

Resources