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 14:08] – 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 psyclogin.rhul.ac.uk: | ||
===== 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 ===== | ||
- | **//The shell: | + | 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 //terminal// - the software tool used for running programmes from the command line. Almost everything in Linux can be done from the command line, and although it can seem daunting and cumbersome at first to have to enter commands by typing rather than pointing and clicking you will soon realise that it is both much faster and allows automating (scripting) tasks that would take a very long time to do using the mouse. Once you get the hang of it it is straightforward to write scripts and even programmes without having to be a seasoned software engineer. |
- | **// | + | Below are some useful tips and tricks for using the terminal that will help you use its full potential. |
- | *Writing a shell script | + | ==== The shell ==== |
- | *Navigating the file system | + | |
+ | The terminal window runs a program called the //shell// to interpret the commands you write. There are different types of shell programs but the most common ones that you'll encounter are //sh// or //bash//, or less commonly, //csh//. The information below refers to bash, which is the standard in most modern Linux distributions. | ||
- | ===== The Linux file system ===== | + | === Special characters |
- | **// The home directory: // | + | The command prompt: |
- | **// The root directory: //**/ | + | The terminal prints a " |
+ | <code sh> | ||
+ | jonas@psych01: | ||
+ | # Change to home folder - note you only input " | ||
+ | jonas@psych01:/ | ||
+ | jonas@psych01: | ||
+ | </code> | ||
- | **//System directories: | + | 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. |
- | **//Mounting external file systems//** | + | #: 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 | ||
+ | </code> | ||
- | **//File permissions | + | &: 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 " | ||
- | **// Recursive commands //** | + | If you've started a program without the & character you can put it in the background by pressing |
- | ===== Essential commands ===== | + | |
- | **//Finding where you are //** pwd | + | <code sh> |
- | **//List files://** ls | + | which means "run in background" |
- | **//Change directory:// | + | <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). |
- | **// | + | / 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 |
- | **//Delete files and directories: | + | /usr/bin |
- | **//Find files and directories: | ||
- | **//Check CPU usage: | + | . A single period (.) means " |
- | **//Text editing: | + | <code sh> |
- | **//PDF viewer: //**atril | + | .. Two periods mean "the folder above your current location" |
- | **//Desktop applications: | + | <code sh> |
+ | # Change directory to the folder above | ||
+ | cd .. | ||
+ | </code> | ||
- | **//Common neuroimaging applications: | + | ~ 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 ~ | ||
+ | </ | ||
- | **//copying files and directories: | ||
- | **// Getting help: //** man | + | === Wildcards === |
+ | 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 // | ||
+ | |||
+ | <code sh> | ||
+ | cd / | ||
+ | ls | ||
+ | </ | ||
+ | |||
+ | 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 | ||
+ | </ | ||
+ | |||
+ | Suppose you just want to list the IMA files. You can then type | ||
+ | <code sh> | ||
+ | ls *IMA | ||
+ | </ | ||
+ | |||
+ | and the output will now be | ||
+ | <code sh> | ||
+ | 20030215AAPU.MR.HOLLOWAY_JONAS.0001.0001.2025.02.18.12.47.03.0.136922180.IMA | ||
+ | </ | ||
+ | |||
+ | The asterisk * is a wildcard - it means "match any character" | ||
+ | <code sh> | ||
+ | ls *s001a* | ||
+ | </ | ||
+ | you will see | ||
+ | <code sh> | ||
+ | 20250218_120320s001a1001A.hdr | ||
+ | </ | ||
+ | - 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.1679926085.txt.gz · Last modified: 2023/03/27 14:08 by ayan