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 filemyscript.py
namedmyscript_new.py
in the current directorymkdir oldscripts
make a new directory namedoldscripts
mv myscript.py oldscripts/myscript_old.py
move the filemyscript.py
into the directoryoldscripts
and at the same time rename it tomyscript_old.py
cp hello.py oldscripts/hello_old.py
copy the filehello.py
into the directoryoldscripts
and at the same time rename it tohello_old.py
cp -r oldscripts/ unused
copy the directoryoldscripts
and all its contents recursively into a new directory namedunused
, the directoryunused
is created if it does not already existrm hello.py
remove the filehello.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 directoryoldscripts
and all its contentsrmdir oldscripts
removeoldscripts
only if it is an empty directory
Displaying files
cat .bashrc
prints the whole contents of the file named.bashrc
to the screenless .bashrc
an interactive way to view files, especially useful for long filenamesNote the manual entry, i.e.,
man less
, details the navigation shortcuts, some of the most useful are as follows.d
andu
to scroll down and up by half a window respectivelyf
andb
to scroll forwards and backwards by a one window respectivelyg
andG
scroll to the beginning and end of the file respectively/pattern
then pressENTER
to search forward in the file for occurrences ofpattern
n
andN
to repeat the search in the forward and backward direction respectivelyAll matches of
pattern
are highlighted
q
to quit and return to the command line prompt
Editing files
nano myscript.py
edit the filemyscript.py
using thenano
programvi myscript.py
edit the filemyscript.py
using thevi
programNote the
nano
editor is more similar to editing with a GUI based text editor and it displays the key commands on the screen. Thevi
editor is more powerful but has a steeper learning curve for internalising its commands.
Executing files
python myscript.py
executes the python filemyscript.py
chmod +x myscript.py
make the filemyscript.py
executableNote the command
chmod
is used for modifying file permissions, seeman chmod
Note see this tutorial on using chmod for more details
./hello.py
run the executable file namedmyscript.py
Installing programs
sudo apt install figlet
install thefiglet
programfiglet ASClinic
run thefiglet
program with the argumentASClinc
, which prints out the following:
_ ____ ____ _ _ _
/ \ / ___| / ___| (_)____ (_) ___
/ _ \ \___ \| | | | | _ \| |/ __|
/ ___ \ ___) | |___| | | | | | | (__
/_/ \_\____/ \____|_|_|_| |_|_|\___|
sudo apt install
install thetree
programtree
run thetree
program to display the file structure of the working directory
Secure shell and secure copying
ssh username@ip_address
log in asusername
to a terminal on the remote machine atip_address
, .i.e., log in to a secure shellscp myscript.py username@ip_address:Documents
copy the filemyscript.py
from your local machine to the remote machine atip_address
into theDocuments
sub-folder ofusername
’s home directoryNote see this tutorial on using scp for more details
Mosh - mobile shell connection
mosh username@ip_address
log in asusername
to a terminal on the remote machine atip_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 brokenssh
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