The command line shell and git

Using the command line

Powershell and Bash common commands

  • To go to your $HOME folder:

cd ~

or

cd $HOME
  • To open explorer or finder for the current folder:

windows explorer do:

   start .

MacOs finder do:

   open .
  • To move up one folder:

cd ..
  • To save typing, remember that hitting the tab key completes filenames

To configure powershell on windows

  • first start a powershell terminal with admin privileges, then type:

    set-executionpolicy remotesigned

  • then, in your miniconda3 powershel profile, do:

    Test-Path $profile

    to see whether you have an existing profile.

  • if you don’t have a profile, then do the following (this will overwrite an existing profile, so be aware):

    New-Item –Path $Profile –Type File –Force

  • To add to your profile, open with:

    start $profile

To configure bash or zsh on MacOS

  • open a terminal then type either

    open .bash_profile

    or for Catalina

    open .zshenv

Bash and powershell command reference

Cmdlet

Alias

Bash Equivalent

Description

Get-ChildItem

gci

ls

List the directories and files in the current location.

Set-Location

sl

cd

Change to the directory at the given path. Typing .. rather than a path will move up one directory.

Push-Location

pushd

pushd

Changes to the directory.

Pop-Location

popd

popd

Changes back to the previous directory after using pushd

New-Item

ni

(touch)

Creates a new item. Used with no parameter, the item is by default a file. Using mkdir is a shortcut for including the parameter -ItemType dir.

mkdir

none

mkdir

Creates a new directory. (See New-Item.)

Explorer

start .

open .)

Open something using File Explorer (the GUI)

Remove-Item

rm

rm

Deletes something. Permanently!

Move-Item

mv

mv

Moves something. Takes two arguments - first a filename (i.e. its present path), then a path for its new location (including the name it should have there). By not changing the path, it can be used to rename files.

Copy-Item

cp

cp

Copies a file to a new location. Takes same arguments as move, but keeps the original file in its location.

Write-Output

write

echo

Outputs whatever you type. Use redirection to output to a file. Redirection with >> will add to the file, rather than overwriting contents.

Get-Content

gc

cat

Gets the contents of a file and prints it to the screen. Adding the parameter -TotalCount followed by a number x prints only the first x lines. Adding the parameter -Tail followed by a number x prints only the final x lines.

Select-String

sls

(grep)

Searches for specific content.

Measure-Object

measure

(wc)

Gets statistical information about an object. Use Get-Content and pipe the output to Measure-Object with the parameters -line, -word, and -character to get word count information.

>

none

>

Redirection. Puts the output of the command to the left of > into a file to the right of >.

\|

none

\|

Piping. Takes the output of the command to the left and uses it as the input for the command to the right.

Get-Help

none

man

Gets the help file for a cmdlet. Adding the parameter -online opens the help page on TechNet.

exit

none

exit

Exits PowerShell

Remember the keyboard shortcuts of tab for auto-completion and the up and down arrows to scroll through recent commands. These shortcuts can save a lot of typing!

Git

Pulling changes from the github repository

When we commit changes to the master branch and push to our github repository, you’ll need to download those changes to keep current. To do that:

  1. open a shell

  2. cd to the numeric repository

  3. fetch the changes with:

    git fetch origin
    
  4. make sure you aren’t going to clobber any of your own files:

    git status
    

    you can ignore “untracked files”, but pay attention to any files labeled “modified”. Those will be overwritten when you reset to our commit, so copy them to a new name or folder.

  5. Finally, get to our commit with:

    git reset --hard origin/master
    

    If that worked, then printing the most recent log entry:

    git log -1
    

    should tell you the most recent commit message, and it should agree with what you see at our github repository.

Books and tutorials

[ ]: