Setting default windows terminal starting directory to folder within c drive WSL - windows-subsystem-for-linux

I am trying to set my default terminal for my WSL prompt in windows terminal to start at a specific folder under the C drive.
i am able to set the default to /mnt but it wont let me go /mnt/c/<myfolder> whenever i do this is sets it back to /mnt/c/Users/<myuser>

In settings.json if you are using the commandline property replace that line with:
"source": "Windows.Terminal.Wsl"
Then set the value of your startingDirectory to where ever you like in the C drive using the drive letter and forward slashes in the path, so it will look something like this:
"startingDirectory" : "C:/Windows/Fonts",

Related

Running J scripts via command prompt on Win 10

How can I run J scripts from the console (cmd.exe or Powershell).I tried to set the path to jconsole.exe in Environment Variables but when I tried to launch it via cmd.exe, I get an error message that 'jconsole' is not recognized as an internal command or... I have done this successfully in the past for different command line tools.
Also, how do I set the default directory in J so that I don't have to type the full directory path every time I run a file.
jconsole.exe works just like any other application in that regard. Once you've added the path to the file in your PATH variable, you can run it in a cmd session without specifying the whole path.
In my case, if I open cmd.exe and add the path to J binaries to my PATH (be sure not to add any quotes!):
set PATH=%PATH%;C:\Program Files\j901\bin;
Then check that it has been successfully added to the end by typing PATH and pressing Enter. Then I can successfully start J in that session by just typing jconsole.exe.
Note that this will only work for the current instance of cmd.exe. If I want it be available every time I open a new cmd.exe session, the I'd add it to the Path variable for my User using the Environment Variables Settings.
You mention that you've tried setting the path in Environment Variables, but it might be worth explicitly stating the directory where J is installed on your computer and what you've added to the PATH.
Have you confirmed in your cmd.exe session, that the path has been successfully amended?

How to change default directory in Windows Subsystem for Linux

I am setting up my development environment, so I just installed Windows Subsystem for Linux and it always seems to open a fresh terminal in my Windows home directory - /mnt/c/Users/dl and I'm trying to make it default to the linux home directory - /home/dl.
I checked to see what the home directory is in the Linux subsystem in /etc/passwd and it is correctly set:
dl:x:1000:1000:,,,:/home/dl:/bin/bash
Then I came across this solution, but it doesn't seem to have any affect:
// Set starting directory
"startingDirectory": "\\\\wsl$\\Ubuntu\\home\\dl\\"
I know I can just run cd ~ in my dot files (which is what I'm currently using), but I'm looking for a way where /home/dl is just the default and cd ~ isn't needed. Is this possible?
You should only change the startingDirectory for WSL (Ubuntu in this case) terminal sessions.
Open settings.json via CTRL+SHIFT+,
Make sure you are modifying startingDirectory under profiles/list/name: "Ubuntu"
Example below (the slashes need to be escaped):
....
{
"guid": "{2c4de342-xxx-xxx-xxx-2309a097f518}",
"hidden": false,
"name": "Ubuntu",
"source": "Windows.Terminal.Wsl",
"startingDirectory": "\\\\wsl$\\Ubuntu\\home\\windows_username_in_lower_case"
},
....
Documentation about startingDirectory including default values and expected values.
Inside settings.json you will also find an explanation of the json schema which is here
If you need to know how or where to edit Windows Terminal settings/preferences: https://learn.microsoft.com/en-us/windows/terminal/get-started
In Windows 10 21H2 or later and Windows 11, it's now much simpler. According to the Microsoft Doc:
On newer versions of Windows, startingDirectory can accept Linux-style paths.
That means you can simply use:
"startingDirectory": "/home/yourusername"
No need for any prefixes for Windows directory structure, nor escaped backslashes. Just plain old Linux forward-slash notation.
This works in both WSL1 and WSL2.
Note: I tried to use "~" and it failed. There may be some way to use {$USERPROFILE}, but haven't tried it.
Changing the home directory with WSL is done the same way as in Linux:
Enter bash
Type the command sudo vim /etc/passwd
Find your account's line, which might look like:
shadyar:x:1000:1000:"",,,:/home/shadyar:/bin/bash
Change the home directory, which above is /home/shadyar, to the new directory, using WSL
note: If you want to set Windows directory as home directory, you need to prepend it with /mnt/, like /mnt/c for C:/, /mnt/d for D:/, etc
Save the file and exit vim by typing :wq and press Enter
Exit bash and re-launch it
To test, use the commands:
cd ~
pwd
The other answers here (especially the latest one from #TomBoland) are great for starting in an arbitrary directory, but the example in your question was to start in your home directory. The easiest way to do that is simply to create or change the "commandline" property to wsl ~. This is an undocumented flag to wsl.exe, and it must be the first argument (e.g. wsl ~ -u root).
Current and Recent Windows Terminal Releases
Since Windows Terminal now has a GUI for Settings, you can just edit your profile to point to wsl ~ in the ->General->Command Line setting.
Older Windows Terminal Releases, or if you want to edit manually
If you are editing your settings.json directly (currently found in %userprofile%\AppData\Local\Packages\MicrosoftWindowsTerminal...\LocalState\settings.json, but this may change) ...
Remove the "source" attribute and replace it with "commandline":
"guid": "{2d5ef231-38b7-51cf-b940-2309a097f644}",
"hidden": false,
"name": "Ubuntu",
//"source": "Windows.Terminal.Wsl",
"commandline": "wsl ~",
"startingDirectory": "//wsl$/Ubuntu/",
"tabTitle": "Ubuntu"
Also, for the fun of it, here's an alternative (hacky) way to open WSL to ~/$HOME (without hardcoding as with the other answers). This is absolutely not needed since it's much easier to use wsl ~, but:
wsl -e sh -c 'cd $HOME; exec $SHELL'
This starts up sh, changes the directory to $HOME, and then exec's your $SHELL to replace the sh.
Should you use Windows Terminal with WSL, then the simplest solution is to configure the starting directory via the Settings menu:
and then:
startingDirectory Should be a windows path, not a nix path. Try D:\Folder\SubFolder instead
refer this link,worked for me
github
I tried many things here and none worked but I finally found a workaround.
After opening your ubuntu, you can set the default path by editing your .bashrc file.
I personally wanted to change it from the default /home/${my_username} to my current user directory (like command prompt C:/users/${my_username}), so I just ran this in my Ubuntu terminal
echo 'cd "../../mnt/c/users/${my_username}"' >> $HOME/.bashrc
Step 1: Open windows command prompt and type "bash"
or open Linux app directly .
Step 2: Type a route which is something like this : /mnt/c/Users/HP/..(You can enter your desired directory here).
For example : /mnt/c/Users/HP/Documents , and by this you will get inside Documents.
For WSL2 Ubuntu the syntax should now match the following example in the json:
"guid": "{2d5ef231-38b7-51cf-b940-2309a097f644}",
"hidden": false,
"name": "Ubuntu",
"source": "Windows.Terminal.Wsl",
"startingDirectory": "//wsl$/Ubuntu/",
"tabTitle": "Ubuntu"
To start in /: "startingDirectory": "//wsl$/Ubuntu/",
To start in /root: "startingDirectory": "//wsl$/Ubuntu/root/",
To start in /home: "startingDirectory": "//wsl$/Ubuntu/home/",
No need to do any of that, just open up the profile for Ubuntu under settings, then update the Command line to add the following option
C:\Windows\system32\wsl.exe -d Ubuntu --cd ~

ls doesn't show any output in ssh connected to an Debian 9 VM instance in Google Cloud Platform

this might be a dumb question, but I checked everywhere and there's no direct answer to it.
I set up both SSH keys successfully and I can connect to my instance via terminal, but when I do "ls", it doesn't show me any output. I am using iTerm2 with zsh on my Mac but I don't think this is an issue.
Can anybody give me a hint? Thanks!
When you access a VM through SSH, your working directory is the home directory of the user specified with the SSH command, i.e. /home/username. In case you access as root, the working directory will be /root.
You can check it through the command pwd
If it is a brand new machine, it is normal that the output of 'ls' is empty since in your home directory no file matches the filters of 'ls' with no parameters. The reason is that 'ls' doesn't show filenames starting with a dot ('.') because in the Linux convention they are hidden unless you run ls -al.
You can try again with $ ls -al and you will be able to see hidden files and directories as well.
On the other hand you can create as well first an empty file and then running again 'ls':
$ touch file
$ ls

Change putty settings using scripts

Is there any way to save the PuTTY output to a file using the command line? I know this is easily done using the GUI but in my case it has to be done automatically.
What I'm working on:
User clicks batch file -> starts PuTTY, automatically connects to my device over SSH and runs a bunch of commands -> PuTTY should save the output to a file.
This can be done by changing registry settings.
Entry point for you is to check what and where is stored for putty:
run regedit and go to HKEY_CURRENT_USER\Software\SimonTatham\PuTTY
you can export settings to file by:
regedit /e "%userprofile%\desktop\putty-registry.reg" HKEY_CURRENT_USER\Software\Simontatham
From here,edit putty-registry.reg to your needs and import on user machine.

How to set default application for specific file types in Mac OS X?

In Mac OSX lion, I'm trying to set default application for specific file types.
Using the below apple script, we can set the default application for the specific "file.abc".
tell application "System Events"
set default application of file "/Users/test/Desktop/file.abc" to "/Applications/TextEdit.app"
end tell
But I want to set the same application as default for all the files having the filetype or extension as "abc".
I have tried the following to get it done. It added an entry in <HOME>/Library/Preferences/com.apple.LaunchServices.plist. But the files are not opened with the specified application.
defaults write com.apple.LaunchServices LSHandlers -array-add "<dict><key>LSHandlerContentTag</key><string>abc</string><key>LSHandlerContentTagClass</key><string>public.abc</string><key>LSHandlerRoleAll</key><string>com.apple.textedit</string></dict>"
Hope somebody knows what i m missing to achieve it.
Answer Found :
defaults write com.apple.LaunchServices LSHandlers -array-add "<dict><key>LSHandlerContentTag</key><string>ugurugu</string><key>LSHandlerContentTagClass</key><string>public.filename-extension</string><key>LSHandlerRoleAll</key<string>org.videolan.vlc</string></dict>"
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -kill -r -domain local -domain system -domain user
Maybe you're doing nothing wrong but that the com.apple.launchservices file just needs to be reloaded. You can logout, wait a few minutes or force launchservices to restart. In the following example I say that public.comma-separated-values-text files (note:This doesn't mean that every CSV file is this content-type) must be opened with TextEdit instead of Excel.
do shell script "defaults write com.apple.LaunchServices LSHandlers -array-add '{ LSHandlerContentType = \"public.comma-separated-values-text\"; LSHandlerRoleAll = \"com.apple.TextEdit\"; }'"
do shell script "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -kill -r -domain local -domain system -domain user"
I'm not sure if you're only trying to do this programmatically. Are you?
Otherwise:
On the file, choose "get info", and under "open with" select the name of the application.
Click on the button "Change All"
You might want to take a look at RCDefaultApp and its source code. It's a program that lets you set which file types are opened by which apps in Launch Services.