Linux¶
This is a beginner's guide to using Linux.
Navigation¶
cd - change directory¶
# Go to user's home directory (~ is a shortcut for the current logged in user's home folder)
cd ~
# Go to the /home/shared directory
cd /home/shared
# Move up one folder in the heirarchy, e.g. from /home/shared to /home
cd ..
ls - list files/folders¶
# List current directory's contents
ls
# list the contents of /home/shared
ls /home/shared
# recursively list all files and folders in /home/shared
ls -R /home/shared
# List files in the current directory with ownership and permissions information
ls -l
# An "alias" of ls -l for ease of use
ll
pwd - print working directory¶
# print current directory path
pwd
# print current directory path ignoring symbolic links (shortcuts)
pwd -P
Managing files and folders¶
cp - copy files or folders¶
# Copy script.R from current directory to /home/shared
cp script.R /home/shared
# Copy data folder from current directory to /home/shared
cp -r data/ /home/shared
mkdir - create new directory¶
# Make scripts/ directory
mkdir scripts
# Make the scripts folder in the /home/shared folder
mkdir /home/shared/scripts
mv - move files/folders¶
# Move script.R from current directory to /home/shared
mv script.R /home/shared
# Move data folder from current directory to /home/shared
mv data/ /home/shared
rm and rmdir - remove/delete files¶
# Delete script.R
rm script.R
# Delete data folder from current directory. This only works for empty directories.
rmdir data/
# Delete data folder from current directory. This is irreversible! There is no "Trash" or "Recycling Bin", so be careful!
rmdir -r data/
touch - update file access metadata¶
# Updates metadata for last accessed. If the file doesn't exist, it will create a blank file.
touch script.R
Editing Files¶
There are a few choices for text editing, but the simplest is nano.
nano¶
# Edit script.R using nano
nano script.R
Text input is simple. The controls are listed at the bottom on the screen. The ^ character is the
vi/vim¶
Vi/vim are advanced text editors. You start in control mode, where you can perform operations on the text by using letter and number keys
# Edit script.R using vim
vim script.R
- dd - delete current line
- d2d - delete 2 lines, starting with the current line
- :1,5d - delete lines 1-5
- u - undo last edit
- i - enter insert mode - this is the basic text entry mode.
- When in insert mode, press
on your keyboard to stop. - w - move to the start of the next word
- ^ - move to head of the current line
- $ - move to the end of the current line
- yy - "yank" (copy) the current line
- p - paste the yanked content on the next line
- :q - quit vim
- :q! - force quit vim, discarding any edits that have been made
- :w - write file but continue editing
- :x - write and exit
- :x! - force write and exit (overrides lack of write permission on file for files that you own)
Persistent terminal sessions with tmux¶
Normally when you exit the terminal, it will stop whatever processes you have running. To start a persistent shell, you can use tmux. (terminal multiplexer)
To make sure processes are left running, you can use tmux from the terminal.
- Log into Jupyterhub and select Terminal from the start page
- Run
tmuxto start a persistant shell - Or if you have a tmux session open already, run
tmux ato reattach to it - Run commands as you normally would to start a python script, e.g.
python myscript.py
tmux uses a series of control keypresses to move between panes, disconnect from a session, and much more. Hold Ctrl and press b to enter the command mode, then enter the command.
- Ctrl+b followed by d will disconnect
- Ctrl+b followed by c will create a new pane with a clean command prompt
- Ctrl+b followed by n will move to the next pane if there are multiple panes
- Ctrl+b followed by p will go back one pane if there are multiple panes
- Ctrl+b followed by [ will allow you to scroll through the terminal's past output (press q in this mode to stop scrolling and return to the command prompt)