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)
Related
My goal is to write an app that lets you quickly assign aliases to long directory paths and change to them. I wrote an app that manages them in a file in the user's appdata directory, but I can't find a way to change the directory of the shell I run the program in from my app. My goal is to have it work from git bash, cmd.exe, and powershell. I want something like this:
cd /c/vsts/some-long-project-name-reports
g -a reports
Now I have an alias 'reports' for that directory. What I want to do get to that directory next time I open a console is:
g reports
I'm using dotnet core, though looking through questions it seems like there isn't a way to do this at all. With Directory.SetCurrentDirectory(path); or Environment.CurrentDirectory = path; it changes the working directory of the g.exe process, but when it exits the shell goes back to it's working directory when I ran the command.
I've come up with a solution for git bash, I changed my g app to output the path instead and have this as go in my path:
OUTPUT="$(g $1)"
cd $OUTPUT
Then I just need to use . or source to run the script in the current shell:
. go reports
And batch file go.bat doesn't need the . or source to work:
for /F "tokens=*" %%i in ('g %1') do set OUTPUT=%%i
cd %OUTPUT%
I guess I'll have to live with typing the extra characters, but is there a similar way to do this with powershell?
Define a wrapper function in PowerShell (assuming that g.exe outputs the target path):
function g { Set-Location (g.exe $args) }
Generally, as eryksun points out in a comment, an executable - which by definition runs in a child process - cannot change its parent process' working directory.
Therefore, the only solution is to output the target directory's path and let the parent process change to it.
Is it possible to assign a directory path to a shortcut variable name that can be used to access quickly over and over again through commands like cd?
I am navigating consistently between several directories and I would like to avoid typing out the full directory path every time. I recall having the capacity to enter the shortcut for a path to access a commonly used directory in Linux. I was wondering if it is possible to use the same cd [SHORTCUT_DIRECTORY_NAME] in Windows Git-Bash or if there is an alternative permanent solution that would limit typing out the full directory paths.
Here is an example of such command to access C:\Users\[NAME]\Documents\common directory in a linux machine shortened to com:
[USER]#DESKTOP /c/Users
$ cd com
[USER]#DESKTOP /c/Users/[NAME]/Documents/common
$
I have mostly found ways to use .bat files and I'm not sure this applies to Windows. I was thinking my next best bet would be trying to create a Shell script, but any input on the most convenient method would be greatly appreciated.
Thank you!
Environment: Windows 10, Git Bash v. 4.4.12
is this what you require:
you can assign the directory path to a string and do cd $string. Example:
sh-4.4$ dir="/home/cg/root/abc/xyz/tyh/"
sh-4.4$ pwd
/home/cg/root
sh-4.4$ cd $dir
sh-4.4$ pwd
/home/cg/root/abc/xyz/tyh
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.
I'm a newbie. I am trying to figure out how to use the command line. Please could you tell me what command I should enter so that I can get a list of all the exe files on my computer. thanks.
You can use the dir functionality to search the directory and all of its children directories while filtering on a particular file type.
dir /s /b *.exe | findstr /v .exe.
Source
If you want to find all the executable files that are on the path and/or in the current directory, i.e., all the files you can run from the command line without specifying a path, this should work:
where *.exe
To get names of all .exe files , that are currently running then type tasklist in cmd.
http://ss64.com/nt/tasklist.html
Here's another method I use a lot for tasks like this.
Open powershell and navigate to your root directory by entering the command
cd c:/
cd stands for change directory, and is an alias for the command "Set-Location". We are setting the location to C:/
Next run the following command:
Get-ChildItem -Filter "*.exe" -Recurse
Get-ChildItem is a function that gets the files and folders in a file system drive, and runs on whatever directory you're current at by default.
-Filter "*.exe" is an argument that specifies to only find filenames which end in ".exe". (The * is a type of regular expression notation).
-Recurse is an argument that specifies to search all child directories. This will make your function run on "C:/", but also all child directories of C:/, and all child directories of those directories and so on. This will allow you to search the entire drive.
I want to start a process (from bash script) whose executable is inside current directory, in another directory $dir (nohup analog for windows cmd START /D). How to do such thing in bash?
If you want the process to execute from $dir, just do:
( cd $dir; ~-/cmd)
where cmd is the name of the executable in the current directory you wish to execute. The parentheses cause the two commands to run in a subshell so that your current shell does not change directory, and the ~- references the previous directory. Using ~- is not necessary if your current directory is in your PATH, and you may prefer to use a full path instead. Note that it is generally considered bad practice to put . in your PATH.