linux-survival
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| linux-survival [2023/03/27 15:08] – [The Linux terminal] ayan | linux-survival [2025/10/09 15:11] (current) – [Logging in to a remote Linux server] jonas | ||
|---|---|---|---|
| Line 3: | Line 3: | ||
| ===== Why Linux? ===== | ===== Why Linux? ===== | ||
| - | Most of the software used for processing and analysing neuroimaging data runs under the Linux operating system (OS). There are good reasons for this - Linux is fast and powerful (all of the 500 fastest supercomputers in the world run Linux and most CGI effects in films are made on Linux computers), widely used (major online providers like Amazon and Google run Linux and 96% of the top 1 million most visited websites are run by Linux, as are 85% of smartphones - Android being a derivative of Linux) and highly customisable. It's also free and open source, | + | Linux is the //de facto// operating system for scientific computing thanks to its high performance and extensive customisability, |
| + | |||
| + | Equally importantly, | ||
| However, Linux can seem an unfamiliar environment for individuals used to working on Windows or Mac. Below are some tips to quickly master essential Linux skills necessary for running most neuroimaging software. | However, Linux can seem an unfamiliar environment for individuals used to working on Windows or Mac. Below are some tips to quickly master essential Linux skills necessary for running most neuroimaging software. | ||
| Line 11: | Line 13: | ||
| Linux was initially created as a hobby project by the Finnish software engineer Linus Torvalds in 1991. The name Linux is a portmanteau of his first name and Unix, the OS from which Linux is derived (in fact, Linux is a version of Unix). **Very important note:** Linux is pronounced as in Finnish Swedish (Linus Torvalds being part of the Swedish-speaking minority in Finland), with a long i as in " | Linux was initially created as a hobby project by the Finnish software engineer Linus Torvalds in 1991. The name Linux is a portmanteau of his first name and Unix, the OS from which Linux is derived (in fact, Linux is a version of Unix). **Very important note:** Linux is pronounced as in Finnish Swedish (Linus Torvalds being part of the Swedish-speaking minority in Finland), with a long i as in " | ||
| - | Unlike Windows or MacOS, there are many varieties of Linux, so-called distributions, | + | Unlike Windows or MacOS, there are many varieties of Linux, so-called distributions, |
| ===== Logging in to a remote Linux server ===== | ===== Logging in to a remote Linux server ===== | ||
| - | | + | UNDER CONSTRUCTION |
| + | ***//SSH//** | ||
| - | SSH stands for Secure Shell. It is a command-line protocol which provides strong password authentication and public key authentication, | + | SSH stands for Secure Shell. It is a command-line protocol which provides strong password authentication and public key authentication, |
| + | |||
| + | < | ||
| + | To be able to connect to the server // | ||
| Line 24: | Line 30: | ||
| - | *RDP | + | ***//RDP//** |
| RDP stands for Remote Desktop Protocol. The // | RDP stands for Remote Desktop Protocol. The // | ||
| - | *VNCViewer | + | ***//VNCViewer//** |
| + | |||
| + | Open the VNCViewer app and enter // | ||
| - | Open the VNCViewer app and enter // | ||
| ===== The Linux desktop ===== | ===== The Linux desktop ===== | ||
| There are a variety of desktops available on Linux distributions but most common distributions (such as Ubuntu or Debian) have a desktop that looks fairly familiar to a Windows or Mac user - there will be a menu bar at the top or the bottom of the screen which you can use to launch different programs, and a desktop with some folders in it that you can click on to open and navigate through the file system. For our purposes we will focus upon the Plasma KDE desktop. | There are a variety of desktops available on Linux distributions but most common distributions (such as Ubuntu or Debian) have a desktop that looks fairly familiar to a Windows or Mac user - there will be a menu bar at the top or the bottom of the screen which you can use to launch different programs, and a desktop with some folders in it that you can click on to open and navigate through the file system. For our purposes we will focus upon the Plasma KDE desktop. | ||
| + | |||
| ===== The Linux terminal ===== | ===== The Linux terminal ===== | ||
| + | Although modern Linux versions have a look and feel that is similar to Windows or Mac, a lot of the power of Linux resides in the use of the // | ||
| - | *The shell: | + | Below are some useful tips and tricks for using the terminal that will help you use its full potential. |
| - | Simply put, the shell is a program that takes commands from the keyboard and gives them to the operating system to perform. In the old days, it was the only user interface available on a Unix-like system such as Linux. Nowadays, we have graphical user interfaces (GUIs) in addition to command line interfaces (CLIs) such as the shell. | + | ==== The shell ==== |
| - | On most Linux systems | + | The terminal window runs a program called the //shell// to interpret |
| - | bash, sh, csh | + | === Special characters === |
| + | The command prompt: | ||
| + | The terminal prints a " | ||
| + | <code sh> | ||
| + | jonas@psych01:/ | ||
| + | # Change to home folder - note you only input " | ||
| + | jonas@psych01:/ | ||
| + | jonas@psych01: | ||
| + | </ | ||
| - | **// | + | In this guide, the command prompt is not shown in any of the examples below to make it easy for you to copy and paste commands into the terminal. |
| - | *Writing | + | #: In the terminal, any line that starts with a hash character # is treated as a comment and ignored. |
| - | | + | <code sh> |
| + | # You can write anything after a hash character, it is ignored by the shell | ||
| + | </ | ||
| + | &: The ampersand character & //after// a command means "run the program in the background" | ||
| + | Normally, when you run a program that doesn' | ||
| + | <code sh> | ||
| + | firefox | ||
| + | </ | ||
| + | the terminal is locked (you can't enter any new commands) until that command has finished (in this case, when you close the browser window). | ||
| + | If instead you type | ||
| + | <code sh> | ||
| + | firefox & | ||
| + | </ | ||
| + | the terminal will keep the browser running in the " | ||
| - | ===== The Linux file system ===== | + | If you've started a program without the & character you can put it in the background by pressing **CTRL+Z** - this will halt (suspend) the program. You can then start it again as a background process by typing |
| - | **// The home directory: | + | <code sh> |
| - | **// The root directory: //**/ | + | which means "run in background" |
| - | **//System directories: | + | <code sh> |
| - | **// | + | CTRL+C: if you are running a program in foreground mode, you can stop it by pressing CTRL+C - it will stop executing more or less immediately and not gracefully (i.e. it wont save any files or make a controlled exit; it will just stop). |
| - | **//File permissions | + | / The forward slash is used to describe the location of files and folders and is used to delimit folders. In Linux the top-level directory is / and directories below are located by relation to this top level directory. For example, programs commonly used by users (rather than the system itseld) will be in a folder called |
| - | **// Recursive commands //** | + | /usr/bin |
| - | ===== Essential commands ===== | + | |
| - | **//Finding where you are //** pwd | ||
| - | **//List files://** ls | + | . A single period (.) means " |
| - | **//Change directory://** cd | + | <code sh> |
| - | **//Rename or move files: //** mv | + | .. Two periods mean "the folder above your current location" |
| - | **//Create files and directories: | + | <code sh> |
| + | # Change directory to the folder above | ||
| + | cd .. | ||
| + | </code> | ||
| - | **//Delete files and directories: | + | ~ The tilde means your home directory. If your user name is jonas, your home directory will be /home/jonas, but you could use ~ instead. |
| + | <code sh> | ||
| + | # List contents of home directory | ||
| + | ls /home/jonas | ||
| + | # List contents of home directory | ||
| + | ls ~ | ||
| + | </ | ||
| - | **//Find files and directories: | ||
| - | **//Check CPU usage: //**top, ps | + | === Wildcards === |
| - | **//Text editing: | + | You often want to be able to find or list files of a particular type or name but don't want to have to type out each file individually. What you can use then is what is known as //wildcards// - special characters that can stand for any text. To make this clearer, suppose you want to list all files that end with the extension IMA in your current folder (this is how the raw data from the scanner is stored). If you go into the directory / |
| - | **//PDF viewer: | + | <code sh> |
| + | cd /MRIWork/MRIWork04/jl/msc2425/ | ||
| + | ls | ||
| + | </ | ||
| - | **//Desktop applications: // | + | you will see the following: |
| + | <code sh> | ||
| + | 20030215AAPU.MR.HOLLOWAY_JONAS.0001.0001.2025.02.18.12.47.03.0.136922180.IMA | ||
| + | 20030215AAPU.MR.HOLLOWAY_JONAS.0001.0003.2025.02.18.12.47.03.0.136922202.IMA | ||
| + | </code> | ||
| - | **//Common neuroimaging applications:// | + | Suppose you just want to list the IMA files. You can then type |
| + | <code sh> | ||
| + | ls *IMA | ||
| + | </code> | ||
| - | **//copying files and directories: | + | and the output will now be |
| + | <code sh> | ||
| + | 20030215AAPU.MR.HOLLOWAY_JONAS.0001.0001.2025.02.18.12.47.03.0.136922180.IMA | ||
| + | </code> | ||
| - | **// Getting help: //** man | + | The asterisk |
| + | <code sh> | ||
| + | ls *s001a* | ||
| + | </code> | ||
| + | you will see | ||
| + | <code sh> | ||
| + | 20250218_120320s001a1001A.hdr | ||
| + | </code> | ||
| + | - that is, every file whose name contains the string " | ||
| + | Most of the time you only need to use the * wildcard, which means "any numbers of characters" | ||
| + | |||
| + | <code sh> | ||
| + | ls m?.IMA | ||
| + | </ | ||
| + | which means "list the files whose names begins with m, followed by an arbitrary single character, followed by .IMA" and this would only list m1.IMA, m2.IMA.. etc but not mm1.IMA etc. | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | ==== Command and file name completion ==== | ||
| + | |||
| + | If you don't remember the exact name of a command, start typing the first letter(s) of the command and press the Tab key - this will list all the commands that begin with those letters - if nothing appears then there is no command that matches. | ||
| + | If you start typing a command followed by a space, you will instead be shown the files in the current directory - if there are many files a prompt will appear saying " | ||
| + | |||
| + | <code sh> | ||
| + | ls myf | ||
| + | </ | ||
| + | and press the Tab key rather than Enter, you will hear a beep and a list will be shown of the two files, like so: | ||
| + | <code sh> | ||
| + | ls myf # pressing Tab | ||
| + | myfile1 | ||
| + | myfile2 | ||
| + | ls myf | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Command history ==== | ||
| + | |||
| + | Use the up and down arrows on the keyboard to step through past commands, and hit enter to re-run them (or Ctrl-C to quit). | ||
| + | |||
| + | To list recent commands, type | ||
| + | |||
| + | <code sh> | ||
| + | history | ||
| + | </ | ||
| + | |||
| + | which will produce a list of the last (normally around 1000) commands preceded by a number. To re-run any of these command, just type an exclamation mark followed by the number, e.g. if the command numbered 1500 was ls -ltRr (a recursive full listing of all directoris and subdirectories in reverse time order), typing | ||
| + | |||
| + | <code sh> | ||
| + | !1500 | ||
| + | </ | ||
| + | |||
| + | will have the same effect as re-typing | ||
| + | |||
| + | <code sh> | ||
| + | ls -ltRr | ||
| + | </ | ||
| + | |||
| + | Often it is handy to use the history function to search for a particular command, e.g. you might not remember how you connected to another computer by ssh, so you want to search all commands including ssh. For this you can use the grep command, which searches for a string: | ||
| + | |||
| + | <code sh> | ||
| + | history | grep ssh | ||
| + | </ | ||
| + | |||
| + | The vertical bar | is a //pipe// - this means that the output from the first command (history) is sent as input to the following command (grep) rather than to the terminal output. Pipes are very useful constructs in Linux that allow you to combine multiple commands into one, whcih can be very handy. For example, the following command will find all instances of ssh in the command history, and then print them out sorted according to the name of the server (the third field of the output of history) in reverse order - here, the output of history is piped to grep, which in turn is piped to sort: | ||
| + | |||
| + | <code sh> | ||
| + | history |grep ssh|sort -k 3,3 -r | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Text editors ==== | ||
| + | |||
| + | Because so much in Linux is done by manipulating text files (as well as writing code), you will often need to use a program to edit text - a text editor. Standard word processing programs are unsuitable for this purpose as they save their output in a non-text format and are not intended for writing simple text commands (or computer code). Much better is to use text editors that are designed for this specific purpose. Two of the most popular editors in Linux are emacs and vi (or vim). Emacs is a lot easier to use and has a more intuitive interface - vi seems to be designed with the express purpose of frustrating novice users and tends to lead to a lot of beeping and error messages. Which one you prefer is a personal decision; they both accomplish the same task. | ||
| + | |||
| + | To run them, just type <code sh> | ||
| + | |||
| + | Two more modern and user-friendly editors are pluma and gedit - these may or may not be installed on your system, but try: | ||
| + | |||
| + | <code sh> | ||
| + | |||
| + | ==== Useful commands & programs ==== | ||
| + | |||
| + | == cd - change directory == | ||
| + | |||
| + | Syntax: cd <path to directory> | ||
| + | <code sh> | ||
| + | cd /home/jonas | ||
| + | # Move up one directory | ||
| + | cd .. | ||
| + | # Move to home directory | ||
| + | cd | ||
| + | </ | ||
| + | |||
| + | == pwd - show current directory == | ||
| + | |||
| + | Syntax: pwd | ||
| + | <code sh> | ||
| + | cd /home/jonas | ||
| + | pwd | ||
| + | /home/jonas | ||
| + | </ | ||
| + | |||
| + | == ls - list files == | ||
| + | |||
| + | Syntax: ls [< | ||
| + | <code sh> | ||
| + | # List files in current directory | ||
| + | ls | ||
| + | # List files in /home/jonas | ||
| + | ls /home/jonas | ||
| + | # List files in current folder in long format showing size and ownership | ||
| + | ls -l | ||
| + | # List files in reverse order by time (last first) | ||
| + | ls -tr | ||
| + | # List files in long format and reverse order by time (last first) | ||
| + | ls -ltr | ||
| + | # List files recursively in current folder and subfolders | ||
| + | ls -R | ||
| + | # List files beginning with myf | ||
| + | ls myf* | ||
| + | # List files ending with ile | ||
| + | ls *ile | ||
| + | # List files containing the string yfi | ||
| + | ls *yfi* | ||
| + | </ | ||
| + | |||
| + | == mv - move or rename files == | ||
| + | |||
| + | Syntax: mv <input file> <output file name or directory> | ||
| + | <code sh> | ||
| + | # Rename a file in current directory | ||
| + | mv myfile1 myfile2 | ||
| + | # Move a file to the directory above | ||
| + | mv myfile1 .. | ||
| + | # Move a file to /home/jonas | ||
| + | mv myfile1 /home/jonas | ||
| + | </ | ||
| + | == cp - copy files == | ||
| + | |||
| + | Syntax: cp <input file> <output file name or directory> | ||
| + | <code sh> | ||
| + | </ | ||
| + | == rm - remove files == | ||
| + | |||
| + | Syntax: rm < | ||
| + | <code sh> | ||
| + | # Remove a single file | ||
| + | rm myfile1 | ||
| + | # Remove multiple files matching pattern myf* | ||
| + | rm myf* | ||
| + | # Remove a folder and all of its contents (beware!) | ||
| + | rm -rf myfolder | ||
| + | </ | ||
| + | == mkdir - make a directory (folder) == | ||
| + | |||
| + | Syntax: mkdir <new folder name> | ||
| + | <code sh> | ||
| + | # Make a new folder in current directory | ||
| + | mkdir mynewfolder | ||
| + | # Make a new folder in another directory | ||
| + | mkdir / | ||
| + | # Make a new folder containing a subfolder | ||
| + | mkdir -p / | ||
| + | |||
| + | </ | ||
| + | == rmdir - remove a directory == | ||
| + | |||
| + | Syntax: rmdir < | ||
| + | <code sh> | ||
| + | # Remove an empty folder (folders with contents need to be removed with rm -rf, see above) | ||
| + | rmdir myfolder | ||
| + | </ | ||
| + | == more and cat - show the content of text files == | ||
| + | |||
| + | Syntax: more < | ||
| + | cat < | ||
| + | <code sh> | ||
| + | # List contents of a text file, one page at a time (step through using space, press q to quit) | ||
| + | more myfile1 | ||
| + | # List full contents of a text file | ||
| + | cat myfile1 | ||
| + | </ | ||
| + | == find - find files == | ||
| + | |||
| + | Syntax: | ||
| + | <code sh> | ||
| + | </ | ||
| + | == grep - find text in a file == | ||
| + | |||
| + | Syntax: | ||
| + | <code sh> | ||
| + | </ | ||
| + | == sort - sort text == | ||
| + | |||
| + | Syntax: | ||
| + | <code sh> | ||
| + | </ | ||
| + | == man - show help for a program == | ||
| + | |||
| + | Syntax: | ||
| + | <code sh> | ||
| + | </ | ||
| + | == ps, top - show CPU usage and running processes == | ||
| + | |||
| + | Syntax: | ||
| + | <code sh> | ||
| + | </ | ||
| + | == kill, pkill, xkill - stop a program or process == | ||
| + | |||
| + | If you come from Windows, you will be familiar with the task manager which can be handy when wanting to stop a program that has crashed or is non-responsive. Linux programs tend to be more stable than Windows ones, but you might still from time to time need to stop an out of control program. Unlike the Windows task manager, which seems to meekly ask programs whether they might be so kind as to stop and can take a while to have any effect, the Linux equivalents show no mercy and are instanteneous and uncompromising - the program or process will stop instantly without saving files etc. Beware that if you kill a process used by the system, you could cause unintended consequences for yourself (e.g. if you stop the window system X). However, the inherently secure nature of Linux means you cannot affect the system as a whole or other users, and logging out and logging in will bring you back to a normal state. The bottom line is, in general don't kill processes you don't know what they are or do. | ||
| + | |||
| + | <code bash> | ||
| + | # To kill a program if you know it's process number (see above), use kill -9: | ||
| + | kill -9 <process number> | ||
| + | |||
| + | #To kill a program whose name you know, use pkill -9: | ||
| + | pkill -9 firefox | ||
| + | |||
| + | #To kill a window program you can use xkill; you will then need to click the window of the program you want to stop: | ||
| + | xkill | ||
| + | </ | ||
| + | |||
| + | == ssh - remote login to another computer == | ||
| + | |||
| + | Syntax: pwd | ||
| + | <code sh> | ||
| + | </ | ||
| + | == scp - copy files to another computer == | ||
| + | |||
| + | Syntax: | ||
| + | <code sh> | ||
| + | </ | ||
| + | == rsync - copy large numbers of files fast and efficiently == | ||
| + | |||
| + | Syntax: | ||
| + | <code sh> | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | ===== The Linux file system ===== | ||
| + | |||
| + | **// The home directory: | ||
| + | |||
| + | **// The root directory: //**/ | ||
| + | |||
| + | **//System directories: | ||
| + | |||
| + | **// | ||
| + | |||
| + | **//File permissions //** | ||
| + | |||
| + | **// Recursive commands //** | ||
linux-survival.1679929718.txt.gz · Last modified: 2023/03/27 15:08 by ayan