Tag Archives: Command line

Manage saved windows passwords

Logged into a windows share via explorer and hit the Remember Password option. Even though the login was accepted (and saved) it turned out I had used the wrong ad domain name…

Couldn’t find where to reset/change/”logout” again, but eventually found there’s a built-in command-line tool one can use for managing these saved passwords. Think I’ve seen a control panel type thing to do this too in the past, but just couldn’t find it today… Anyways, command-line is nice, so, using imaginary share \\foobar.int, here’s some stuff one can do:

> cmdkey /list
> cmdkey /list:foobar.int

> cmdkey /delete:foobar.int

> cmdkey /add:foobar.int /user:domain\username /pass

Heroku deploy sub-directory

Going through a couple of Udemy courses, and figured it was nice to keep all the code I wrote while doing them in a single github repository.

Was working nicely until I started the Node with React where part of the course is to deploy the app to Heroku which wants a full repository pushed to a certain git repo it creates for deployment. But in my case the app I want to deploy is of course a sub-directory…

Turns out there’s a git command called subtree one can use here:

# Setup
heroku login
heroku create

git remote add heroku/some-name https://git.heroku.com/{created-heroku-app-id}.git

# Deploy
git subtree push --force --prefix path/to/app heroku/some-name master

The subtree command needs to be run from the top-level directory of the git repository, but one can add a script command to the apps package.json, for example like this:

{
  "scripts": {
    "deploy": "cd ../../.. && git subtree push --prefix path/to/app heroku/some-name master"
  }
}

Unfortunately, subtree push doesn’t support --force, but a workaround for that is running it nested like this:

git push heroku/some-name `git subtree split --prefix path/to/app master`:master --force

Which unfortunately doesn’t work on Windows… but you can do it in two steps instead:

git subtree split --prefix path/to/app master
git push heroku/some-name {id-from-previous-command}:master --force

Touching files in Windows

In Unix you have the touch command which you can use to update a files timestamp. No such thing in Windows. But instead, apparently, one can do this:

REM Updates the timestamp of the file
copy /b filename.ext +,,

REM Creates a new empty file
echo $null >> filename.ext

If you need it more than seldom there are also some more tricks in the StackOverflow sources below.

Sources:
Windows equivalent of the Linux command ‘touch’?
Equivalent of Linux `touch` to create an empty file with PowerShell?

Unix: Remove lines from a file based on regular expression

Just a note to self. Useful for trimming away useless information from a log file for example.

# Output result
$ sed "/pattern/d" file.log

# Overwrite file
$ sed "/pattern/d" file.log > file.log

# Inplace deletion (requires GNU sed)
$ sed -i "/pattern/d" file.log

In the pattern, things like capturing groups and alternations needs to be escaped with a slash, \. If you have RegexBuddy you can use the GNU BRE flavor to help you construct the pattern.

Source: StackOverflow

Unix: Command history search

In a Unix Bash shell we can scroll through previous commands by using the up and down arrow keys, but we can also search for previous commands. I keep forgetting how, so time to write it down 😛

Ctrl+r

Start a command search

Run the command directly
Ctrl+e or Ctrl+a

Exit search and jump to the end or beginning of command.
or

Exit search and move through command as usual
Ctrl+k

Remove everything after the current cursor position and to the end of the command

Bat: Some simple things to make batch scripts more helpful

Tried to make a helpful batch script for something at work and learned some new things I thought I’d write down here so I know where to find it later.

Turn off echoing

Should be known by everyone really, but in case you don’t. Prevents the commands in the batch script and the command prompt to show up while the script is running. Beginning a line with @ does this for a single command.

@echo off

Comments

REM Some comment
REM Will still appear as a command if you haven't disabled echoing

Labels and goto

Trivial, but I keep forgetting about this since I never use it in other programming.

goto end
REM Will be skipped
:end

Set title of command prompt

title MyScript: Doing x now

Check command line arguments

if "%~1"=="" (echo Usage: test.bat path && goto end)
if not exist %~1 (echo %~1 does not exist && goto end)

Simpe choices

choice /c ny /n /d y /t 10 /m "Do you really want to do this? [y=10s,n]
if ERRORLEVEL 2 (
    echo Ok, I'll do it
)

Run other batch scripts

If you don’t use the call command, the other script will actually take over and your script will end.

call other.bat arg1 arg2 ...

Do the same thing with various subjects

for %%x in (
    a
    b
    c
) do (
    echo %%x
)

Unix: Useful and pretty prompt

Some servers at work has varying degrees of useful prompts when I connect to them through SSH. Usually they are quite annoying and for example showing the shell type and version, which I frankly don’t care much about. Here’s how to make it show the current user, hostname and working directory. In addition the hostname is made more visible, which is a nice effect.

$ bold=$(tput bold)
$ reset=$(tput sgr0)
$ export PS1="\u@\[$bold\]\h\[$reset\]:\w\$ "

Stick it in your .profile, .bash_profile or whatever else you’ve got going on to make it more permanent.