LoginSignup
4
3

More than 3 years have passed since last update.

windows10のコマンドプロンプトで、linuxのコマンドを使う。

Last updated at Posted at 2020-01-20

環境

windows10

準備

設定 > アプリ (アプリと機能) > プログラムと機能 > Windows機能の有効化または無効化

に移動する。

その後、

Windows Subsystem for Linux

の項目にチェックを入れて、OKをクリックする。

スタートメニューの「Microsoft Store」で「Linux」を検索して好きな Linux のディストリビューションをインストールする。(この場合はUbuntu)

本番

C:\Users\xxx
などに

linuxcmd.bat というファイルを、以下の内容で保存する。

linuxcmd.bat
@echo off
doskey /MACROFILE=%~dp0.doskeys

そして、同じディレクトリに
.doskeys
という名前のファイルを、以下の内容で作成する。

alias=if "$1"=="" (doskey /macros) else for /f "delims== tokens=1,*" %i in ("$*") do @if "%j" neq "" (  doskey $* ) else (  doskey /macros $b findstr /b /c:"%i=" )

pwd=cd
traceroute=tracert
ifconfig=ipconfig
awk=wsl awk $*
cat=wsl cat $*
clear=wsl clear $*
cp=wsl cp $*
cut=wsl cut $*
df=wsl df $*
diff=wsl diff $*
dig=wsl dig $*
du=wsl du $*
find=wsl find $*
grep=wsl grep $*
gzip=wsl gzip $*
head=wsl head $*
ip=wsl ip $*
join=wsl join $*
kill=wsl kill $*
less=wsl less $*
ls=wsl ls $*
man=wsl man $*
more=wsl more $*
mv=wsl mv $*
ps=wsl ps $*
rm=wsl rm $*
sed=wsl sed $*
sort=wsl sort $*
sudo=wsl sudo $*
tail=wsl tail $*
touch=wsl touch $*
tr=wsl tr $*
uniq=wsl uniq $*
whoami=wsl whoami $*
wc=wsl wc $*
wget=wsl wget $*

これで準備は完了しました。

実行

コマンドプロンプト上で、linuxcmd.batを実行し、好きなlinuxコマンドを実行します。

コマンドプロンプト起動時にlinuxcmd.batを実行するようにする。

linuxcmd.batと同じディレクトリに、linuxcmdreg.bat という名前で、以下のファイルを作成する。

linuxcmdreg.bat
@echo off

reg add   "HKEY_CURRENT_USER\Software\Microsoft\Command Processor" /v AutoRun /d %~dp0linuxcmd.bat
reg query "HKEY_CURRENT_USER\Software\Microsoft\Command Processor" /v AutoRun

pause

制限

パイプが使えない。
日本語のファイルが文字化けする。

対処法:アプリケーションのubuntuのコマンドプロンプトを使用する。

コマンドの追加

.doskeysに、以下のようにすることでコマンドを追加できる。

Linuxの方で、sudo apt installなどを用いてImagemagickなどをインストールしてから、

<追加したいコマンド>=wsl <追加したいコマンド> $*

と追記することで、コマンドを追加できる。

Linuxの方で、

sudo apt install tree

.doskeys に、

tree = wsl tree $*

と追記すると、Windowsのコマンドプロンプトでも、treeコマンドがつかえる。

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3