Let say that we want to cd to a windows explorer directory and we don’t want manually to rewrite all the backslashes. We could write a bash cmd which does just that.
cd $(echo 'C:\laragon\www\docker\phpfpm-mysql-nginx' | sed 's/\\/\//g' | sed 's/C:/\/c/g')
Outputs:
/c/laragon/www/docker/phpfpm-mysql-nginx
Writing a bash script
Alternatively, we can create a bash script which after run returns or run some actions in a directory it was placed.
#!/bin/sh
# get currrent file directory full path
path=$(dirname $(readlink -f -- "$0"));
path=$(echo $path | sed 's/\\/\//g' | sed 's/C:/\/c/g')
echo "$path"
Create a global cmd
You can also make it accept a a given Windows explorer path and turn it in to Linux / WSL path style. Create a wlcon.sh file. Open terminal in this directory location. Move .sh file to /usr/bin/ folder.
cd
to file directory where a wlcon.sh
file resigns.
cp -v ./
.sh /usr/bin/
wlcon
.sh
wlcon
alias
wlcon
="bash somename.sh"
#!/bin/sh
# get currrent file directory full path
# path=$(dirname $(readlink -f -- "$0"));
# Accept windows style path and turns it in to unix type path
# if cmd arg is given replace current path
# when using a script with argument use "" liek so: mypcon "C:\laragon\www\docker\phpfpm-mysql-nginx\mysqldata", if you don't use pharentases slashes insted of replaced will get removed by the bash
if [ "$1" != "" ];
then
path=$(echo $1 | sed 's/\\/\//g' | sed 's/C:/\/c/g')
echo $path
else
echo "Converts Windows path to Linux like"
echo "No Windows path given"
echo "Always use \" around the path parameter"
fi
If no argument given, it will run the code with in the directory the script was placed. However, if we give it an argument (Windows Explorer path style) it will convert it to Unix path style, and will run the instructions from the given destination.