LINUX COMMAND LINE

The list below provides basic commands for performing tasks from a Linux terminal, but it is by no means exhaustive. You can find many great command line tutorials on the web, for example:

Editing filenames and directories

  • cp myscript.py myscript_new.py make a copy of the file myscript.py named myscript_new.py in the current directory

  • mkdir oldscripts make a new directory named oldscripts

  • mv myscript.py oldscripts/myscript_old.py move the file myscript.py into the directory oldscripts and at the same time rename it to myscript_old.py

  • cp hello.py oldscripts/hello_old.py copy the file hello.py into the directory oldscripts and at the same time rename it to hello_old.py

  • cp -r oldscripts/ unused copy the directory oldscripts and all its contents recursively into a new directory named unused, the directory unused is created if it does not already exist

  • rm hello.py remove the file hello.py

    • Note that rm is permanent and cannot be undone, less the file is tracked using a git repository

  • rm -r oldscripts remove recursively the directory oldscripts and all its contents

  • rmdir oldscripts remove oldscripts only if it is an empty directory

Displaying files

  • cat .bashrc prints the whole contents of the file named .bashrc to the screen

  • less .bashrc an interactive way to view files, especially useful for long filenames

    • Note the manual entry, i.e., man less, details the navigation shortcuts, some of the most useful are as follows.

    • d and u to scroll down and up by half a window respectively

    • f and b to scroll forwards and backwards by a one window respectively

    • g and G scroll to the beginning and end of the file respectively

    • /pattern then press ENTER to search forward in the file for occurrences of pattern

      • n and N to repeat the search in the forward and backward direction respectively

      • All matches of pattern are highlighted

    • q to quit and return to the command line prompt

Editing files

  • nano myscript.py edit the file myscript.py using the nano program

  • vi myscript.py edit the file myscript.py using the vi program

  • Note the nano editor is more similar to editing with a GUI based text editor and it displays the key commands on the screen. The vi editor is more powerful but has a steeper learning curve for internalising its commands.

Executing files

  • python myscript.py executes the python file myscript.py

  • chmod +x myscript.py make the file myscript.py executable

  • ./hello.py run the executable file named myscript.py

Installing programs

  • sudo apt install figlet install the figlet program

  • figlet ASClinic run the figlet program with the argument ASClinc, which prints out the following:

    _    ____   ____ _ _       _
   / \  / ___| / ___| (_)____ (_) ___
  / _ \ \___ \| |   | | |  _ \| |/ __|
 / ___ \ ___) | |___| | | | | | | (__
/_/   \_\____/ \____|_|_|_| |_|_|\___|
  • sudo apt install install the tree program

  • tree run the tree program to display the file structure of the working directory

Secure shell and secure copying

  • ssh username@ip_address log in as username to a terminal on the remote machine at ip_address, .i.e., log in to a secure shell

  • scp myscript.py username@ip_address:Documents copy the file myscript.py from your local machine to the remote machine at ip_address into the Documents sub-folder of username’s home directory

Mosh - mobile shell connection

  • mosh username@ip_address log in as username to a terminal on the remote machine at ip_address using mosh, which stands for mobile shell.

    • If you are using ssh in areas of your campus with patchy WiFi connection, then a frequent occurrence of broken ssh pipelines can be frustrating. As described on the mosh website, mosh is a “remote terminal application that allows roaming and supports intermittent connectivity”, and hence is ideal for such situations where the WiFi connection is patchy.

    • To use mosh, it needs to be installed on both the robot (see Install mosh) and your personal computer.

Miscellaneous

  • Ctrl-C stop the currently active command, useful for when you get stuck