How to create ls in windows command prompt? - windows

I want to use ls in windows command prompt and make it run the dir command.
How can I do that?

You can solve this question with one simple command:
echo #dir %* > %systemroot%\system32\ls.bat
Make sure you run cmd.exe as admin first if you are on vista and up

You could:
create a batch file called ls.bat and have it contain the dir command only
add the directory where the ls.bat file exists to your PATH environment variable
You could then execute ls from a command prompt.

Its an old question but for the record:
http://gnuwin32.sourceforge.net/packages/coreutils.htm
Gives you ls and a whole lot more!

Easiest way I have found is:
Install Git for Windows
Add the bin directory of Git to your Path variable. Mine was located in C:\Program Files\Git\usr\bin.
Start a command prompt and enjoy ls in all its glory.

I have a solution but it's dirty:
Create a file named ls.bat containing only "dir".
Put it in C:\windows\system32 (or any directory in PATH env var).
That (should) works!
Edit: Something more consistent: https://superuser.com/questions/49170/create-an-alias-in-windows-xp

If you have Node.js installed on your system, you can install it from Cash, a library I wrote for Linux commands on Windows:
npm install cash-ls -g

Windows command prompt for Vista/7 will allow NTFS symbolic links, run cmd.exe as administrator then:
mklink ls %System%\dir.exe
Then set up your PATH environment variable to include the location of the link you just created.
If you want more than just the 'ls' command, you should look into cygwin.
EDIT- Just realized dir.exe is not a separate program, so this doesn't really work. But mklink and cygwin are good things to know about.

If you just want to have cmd recognize ls as an alias for dir, you can use the doskey command (from this answer on superuser).
This does not change the original command line parameter handling of the dir command.

+1 on the post above suggesting to install git for windows and add the directory bin to your path variables.
Another way I got touch, ls, and a lot of other UNIX commands working in cmd.exe on my Windows 8 and Windows 7 machines.
Go to the following site to install Cygwin
https://www.cygwin.com/install.html
Install the 32 or 64 bit version for your system. The default settings and packages should include what you need so you don't have to change anything once you get to the packages screen.
After installation, copy the Cygwin folder path to your environment path variables. For example; if you installed cygwin to C:\Cygwin, you will add the following to your environment system path variables:
;C:\Cygwin\bin
On my system I installed the 64bit version and the default folder name and path was C:\cygwin64. So i added the following to my system environment path variables:
;C:\cygwin64\bin
Restart your terminal if it's open. Then type ls and you'll see a directory listing.
See the following if you are not familiar with setting PATH environment variables:
Superuser Link 1
Superuser Link 2

my ls.bat was below
#dir %*
that can transfer cli args
ls /b
ls /w
put it in %windir% or any directory in your %PATH% variable.
Just make sure you save the file with ANSI encoding :)

you could also use cygwin and just use the ls command directly along with all the other unix command line tools you might be used to.

I recommend the following recipe.
Use DOSKEY and $* to create your ls command.
Make the command persistent by recording it in a .bat/.cmd file and add the path of the file to registry.
For example, your command may look like
DOSKEY ls=dir
DOSKEY sublime="C:\Program Files\Sublime Text 2\sublime_text" $*
$* is useful for commands that take on arguments. For example, here I like to be able to do sublime my_code.c.
The registry for cmd is at HKEY_CURRENT_USER -> Software -> Microsoft -> Command Processor. Create a string valued entry called AutoRun with the full path of the file (not the containing folder) such as %USERPROFILE%\custom_command.cmd. Then each time cmd is run, your command will be loaded!
You can add more useful stuffs to the batch file too. See here for an example template.

Another solution that worked for me is to use UnxUtils, which adds multiple utilities from executable files (including ls, sed, and grep).
To use: download source code. Unzip. Add the UnxUtils\usr\local\wbin path to the Windows PATH variable. Start a new CMD instance.

The most easiest way is
install git
add C:\Program Files\Git\usr\bin to your path variable
now you can use ls

Someone who uses Linux Subsystem for Windows could call ls from the Linux bash. The following Command creates the ls Command in System32:
echo #bash -c "ls %*" > %systemroot%\system32\ls.bat
(The Linux Subsystem feature must be enabled/installed first)

You could follow this guide:
https://gist.github.com/vladikoff/38307908088d58af206b
TL;DR: pass /K path/to/custom/init_cmd.bat to your "shell startup" command.
I'm using ConsoleZ as my shell wrapper, so in my case I can find the setup option in "tabs", then I set the shell path to "C:\Windows\System32\cmd.exe "/K C:\cmd_init.bat""
like this.
Where C:\cmd_init.bat is the batch script containing my macros, here's what I would go for:
#echo off
doskey ls=dir /b
rem other macro stuff..
Sorry for formatting and other mistakes, this is my first time answering here.
I hope it helps!

Create an alias in .bat or .cmd file using doskey key:
#echo off
title "ls command cmd bar"
doskey ls=echo off $T dir $* $T echo on
Enjoy =)

Surely ls would not work as a unix command for the batches. If you check %1 for -l or -a etc. and all combinations of them, it would work...

Here is my C# source code and binary.
Just add ls.exe somewhere and add the path to the path environment variable.

Related

How to call PowerShell script from WSL?

In my Windows directory
C:\Users\jholmes\pichak\analytics
I have run1.ps1 code.
I run wsl.exe, now my pwd is
/mnt/c/WINDOWS/system32
How to point to the first path and execute the script?
In WSL2, using the -File option worked for me:
powershell.exe -File path/to/script.ps1
When running wsl (or wsl.exe) from PowerShell, it should start in the same directory that you were in under PowerShell, just with the Linux version of it. For instance, if you are in PowerShell in C:\Users\jholmes\pichak\analytics, and you run wsl, you should end up in /mnt/c/Users/jholmes/pichak/analytics.
If not, then you may have something in your startup file already like the other answer recommend. You should remove any cd commands from your .bashrc, .zshrc, .profile, .bash_profile, or any other startup file you may have edited.
The wsl command provides a few handy arguments for specifying the starting directory:
wsl ~ will start in your Linux user's home directory (e.g. /home/jholmes)
wsl --cd c:\ will start in /mnt/c (or whatever the equivalent Linux directory is to the Windows version passed in).
wsl --cd \\wsl$\<distroname>\etc will start in your /etc directory (or whatever WSL path you passed in. Note that you will need to specific your distribution name (obtained from wsl -l -v).
Let's say that you are in /home/jholmes, and you want to execute the PowerShell script C:\Users\jholmes\pichak\analytics\run1.ps1. You first need to translate the Windows path to the Linux version:
/mnt/c/Users/jholmes/pichak/analytics/run1.ps
You can then run it via:
powershell.exe /mnt/c/Users/jholmes/pichak/analytics/run1.ps
It's also possible to set up a PowerShell script with a "shebang" line to execute it directly. If you add the following as the first line of run1.ps1:
#!/usr/bin/env -S powershell.exe -ExecutionPolicy Bypass
Then set it to be executable via chmod +x /mnt/c/Users/jholmes/pichak/analytics/run1.ps, then it becomes a "command" of sorts (technically, an "executable script") that you can execute directly by just typing the fully qualified name of the script at the command line:
/mnt/c/Users/jholmes/pichak/analytics/run1.ps
or
cd /mnt/c/Users/jholmes/pichak/analytics
./run1.ps
There are two ways to go about this:
You can change your working directory to that of your shell script and execute it normally. To do so, follow these steps:
Mount the relevant drive cd /mnt/c/.
Change directories according to the path of the script.
This approach is more of a hack that I use for the sake of convenience. I have created a folder in my Windows storage wherein I store all Ubuntu WSL related files. Say, D:\Ubuntu. To avoid changing the working directory every time you open WSL, you can modify the shell profile file (bashrc, zshrc etc.) to load the relevant directory at the end.
i.e., Add cd /mnt/d/Ubuntu/ at the end of your ~/.zshrc file if you use zsh or the relevant profile file otherwise.
Here is the evidence, that with the WSL2, our software can make the Powershell script encrypted and protected:
See this picture:
And it can detect and kill the process that uses fanotify because we think it can be used by attackers to hide critical file change (it also can find and kill dtrace/strace/stap/bpftrace/debuggers and process that opens /dev/kmem, /dev/mem, /proc/kcore, and the protected process' /proc/pid/mem etc):
This picture shows the encrypted PowerShell script now can detect and kill possible attackers' processes
For me this answer was almost the good one but when running for instance:
powershell.exe -File /mnt/c/path/to/script/script.ps1
I got following error:
The argument '/mnt/c/path/to/script/script.ps1' to the -File parameter does not exist. Provide the path to an existing '.ps1' file as an argument to the -File parameter.
I then have to:
cd /mnt/c/path/to/script
powershell.exe -File script.ps1

How can I open a cygwin terminal?

I'm using a windows 8 and have cygwin installed. However, when I navigate to the folder it's contained in, the only subfolders are usr and var, and neither contains something that looks like a terminal. How can I open a cygwin terminal?
You should use mintty executable for this. In case of a default install path it is located here: c:\cygwin\bin\mintty.exe
Cygwin installation puts a shortcut to the start menu, but perhaps this feature was broken in win8.
I checked this shortcut on my computer, and it contains this command as a "Target":
C:\cygwin\bin\mintty.exe -i /Cygwin-Terminal.ico -
You should create a shortcut like this. Replace the given path, if your cygwin is located anywhere else. -i command line argument is not necessary. For further info check the manual page of mintty, which is located here.
You can run
c:\program files\cygwin\bin\bash.exe
inside the Windows CMD.
When you install cygwin, it normally installs a ".bat" file at the top of the directory-tree containing cygwin, which can run a command-window. For example:
#echo off
C:
chdir C:\cygwin64\bin
bash --login -i
The exact location of the cygwin tree is relevant to the script, of course, and details change occasionally. But the bash executable, starting in cygwin's bin-directory is enough to start a window.
Outside cygwin, you will not see things such as mounted directories and symbolic links. So (from the Windows file explorer) it may look incomplete.

Windows equivalent of $export

I am trying to follow some instructions for creating a directory using the command line. The instructions are:
$ export PROJ_HOME=$HOME/proj/111
$ export PROJECT_BASEDIR=PROJ_HOME/exercises/ex1
$ mkdir -p $PROJ_HOME
Are these windows commands? Are there windows equivalents?
To translate your *nix style command script to windows/command batch style it would go like this:
SET PROJ_HOME=%USERPROFILE%/proj/111
SET PROJECT_BASEDIR=%PROJ_HOME%/exercises/ex1
mkdir "%PROJ_HOME%"
mkdir on windows doens't have a -p parameter : from the MKDIR /? help:
MKDIR creates any intermediate directories in the path, if needed.
which basically is what mkdir -p (or --parents for purists) on *nix does, as taken from the man guide
There is not an equivalent statement for export in Windows Command Prompt.
In Windows the environment is copied so when you exit from the session (from a called command prompt or from an executable that set a variable) the variable in Windows get lost. You can set it in user registry or in machine registry via setx but you won't see it if you not start a new command prompt.
you can use export if you have git-bash installed.
Windows 10+, it is preserved for an user forever, run as admin for global var
setx myvariable myvariablevalue

How do you run Vim in Windows?

I just installed gVim, and tried using the usual "vim myfile.java" technique that usually works for linux to open up a file and edit it. But unfortunately, this doesn't seem to work. I've also tried "gvim myfile.java", but that doesn't work either.
Does anyone know how to open up vim (and use it like you do in linux) using Windows Powershell, or some other technique?
When you install gVim:
Please make sure [✓] Create .bat files for command line use is checked.
It'll create several .bat files in C:\Windows\:
C:\>cd %windir%
C:\WINDOWS>dir /b *.bat
evim.bat
gview.bat
gvim.bat
gvimdiff.bat
view.bat
vim.bat
vimdiff.bat
vimtutor.bat
Notice that: C:\WINDOWS is already in the PATH environment variable.
When you type vim in command line, C:\WINDOWS\vim.bat will be launched.
If you leave the checkbox mentioned above unchecked, you need to modify PATH manually.
Just to supplement, I'm on a fairly highly controlled Windows workstation right now, and don't have access to much. Downloading the "executable installer" that I usually use did not create the bat files nor, for some reason, vim.exe, though gvim.exe was installed in the vim74 dir for me and worked fine. So though I also needed to set the PATH, that there was no bat file in C:\WiNDOWS nor any command line executable in my VIm runtime folder to call meant that callingvim from the command line (or Powershell) didn't work.
I'm guessing some portion of the install that's creating the command-line related stuff, apparently including vim.exe, isn't recovering gracefully when you don't have admin permissions.
Either way, the "right" thing to do appears to be to set your PATH to your vim executable folder as usual (note that this might be a little more difficult than usual if you don't have admin privs), then download the "Win32 console executable" from the usual download page that matches the version of gvim that you've already installed, dig vim.exe out of that zip you just downloaded, and place that into the same folder as gvim.exe.
Looking on another box where the executable installer did work "fully", there's some jive in the vim.bat file that wasn't installed for me about "collect the arguments in VIMARGS for Win95" and if .%OS%==.Windows_NT goto ntaction, etc etc, but not having any of that doesn't seem to be a problem on Win7, at least. ;^)
Windows 10 has linux subsystem for windows. So you can install bash in windows and from bash you can use vim.I found it more convenient.
Install gVim on your window and enable ".bat" when you install gvim and click next, done.
You can use vim on window.
Because of restrictions on my workstation, I had to install my instance of vim locally to the following folder: %LocalAppData%\Vim
But just changing an environmental variable like others suggest is insufficient because the batch files aren't in the installation directory (such as vimdiff). So I figured out where these come from, and made my own batch files to add them to an environmental variable. To help you make these batch files, I've provided the list of commands below for you to run in the vim installation directory (if saving the commands to an install.bat file first, replace all % characters with %% to escape it):
echo #echo off > evim.bat
echo gvim.exe -y %* >> evim.bat
echo #echo off > view.bat
echo vim.exe -R %* >> view.bat
echo #echo off > gview.bat
echo gvim.exe -R %* >> gview.bat
echo #echo off > vimdiff.bat
echo vim.exe -d %* >> vimdiff.bat
echo #echo off > gvimdiff.bat
echo vim.exe -d %* >> gvimdiff.bat
Then you have to go to Start > Search 'Edit environment variables for your account' > Find 'Path' > Edit > Add a new subentry for %LocalAppData%\Vim\vim82. Last you may need to close and open the command prompt, or kill explorer.exe and restart it.

cywin bash script command not found when called from batch

#!/bin/bash
echo "Testing"
cd "/cygdrive/x/Internal Communications/Riccardo/"
filename=htdocs-`date +%A`.tar.gz
tar cvzf $filename "/cygdrive/c/Program Files/Zend/Apache2/htdocs"
The above script is working when it is called inside cygwin console, but when I try to call it from a batch file I get "command not found" for date and tar command. I think that bash.exe does not have the PATH set up.
I need to run that script from that batch file because I want to add the script to the task scheduler.
As has already been said, you need to add the Cygwin binaries to your path. To do so, right click on "My Computer", click "Properties", then "Advanced", then "Environment Variables".
Create a new environment variable with name "CYGWIN_HOME" and value "C:\cygwin" (or wherever you installed cygwin. The default location is "C:\cygwin\" so this should probably work for you).
Then edit the environment variable named "PATH", and tack on the following to the end:
;%CYGWIN_HOME%\bin;%CYGWIN_HOME%\sbin;%CYGWIN_HOME%\usr\bin;%CYGWIN_HOME%\usr\sbin;%CYGWIN_HOME%\usr\local\bin;%CYGWIN_HOME%\usr\local\sbin
Close your command prompt, then reopen it. The cygwin binaries should now be available. You can double-check this by typing "which bash". It should report the location of your bash executable.
FWIW, Cygwin has cron.
Are you calling your script like this?
bash --login -i ./myscript.sh
Put your cygwin bin directory (likely C:\cygwin\bin) on your PATH environment variable.
This would also give you the benefit of being able to use commands like tar, ls, rm, etc. from a regular console windows and not just a Cygwin console.
If this script is invoked from a Windows command shell, the first line will result in an error since #!/bin/bash is not a recognized Windows command and # is not a valid comment delimiter in a batch file.
So, the bottom line is that this script runs as a regular batch file rather than from within Cygwin's bash. As noted by matt b, you likely do not have the Cygwin executable path in your PATH environment variable. Without this, the batch file cannot find the Cygwin utilities (tar and date).
I just had this problem.
Editing the environment variable works great. But if you are have no admin rights you can´t do that.
In this case you can execute your commands by using the absolute path like:
/usr/bin/tar cvzf $filename
/usr/bin/cat $filename
If you do so your bash script works even if you call it from a batch file.

Resources