linux-survival
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| linux-survival [2024/01/16 15:00] – [Linux basics] jonas | 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 15: | Line 17: | ||
| ===== Logging in to a remote Linux server ===== | ===== Logging in to a remote Linux server ===== | ||
| - | | + | UNDER CONSTRUCTION |
| + | ***// | ||
| - | 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 30: | Line 36: | ||
| ***// | ***// | ||
| - | 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 // | ||
| + | |||
| + | Below are some useful tips and tricks for using the terminal that will help you use its full potential. | ||
| + | |||
| + | ==== The shell ==== | ||
| + | |||
| + | 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. | ||
| + | |||
| + | === 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. | ||
| + | |||
| + | #: 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 " | ||
| + | |||
| + | 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 | ||
| + | |||
| + | <code sh> | ||
| + | |||
| + | which means "run in background" | ||
| + | |||
| + | <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 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 //bin// which is located within a subfolder of the top-level directory called //usr//. The full path to this folder is therefore written | ||
| + | |||
| + | /usr/bin | ||
| + | |||
| + | |||
| + | . A single period (.) means " | ||
| + | |||
| + | <code sh>cp / | ||
| + | |||
| + | .. Two periods mean "the folder above your current location" | ||
| + | |||
| + | <code sh> | ||
| + | # Change directory to the folder above | ||
| + | cd .. | ||
| + | </ | ||
| + | |||
| + | ~ The tilde means your home directory. If your user name is jonas, your home directory will be / | ||
| + | <code sh> | ||
| + | # List contents of home directory | ||
| + | ls /home/jonas | ||
| + | # List contents of home directory | ||
| + | ls ~ | ||
| + | </ | ||
| + | |||
| + | |||
| + | === 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. | ||
| - | ***//The shell//** | + | To run them, just type <code sh> |
| - | The shell is a program that takes commands from the keyboard | + | Two more modern |
| - | On most Linux systems a program called **bash** (which stands for Bourne Again SHell, an enhanced version of the original Unix shell program, **sh**, written by Steve Bourne) acts as the shell program. Besides bash, there are other shell programs available for Linux systems. These include: ksh, tcsh and zsh. | + | < |
| - | On the psycomp server, we will use **bash** | + | ==== Useful |
| + | == cd - change directory == | ||
| - | ***// | + | Syntax: cd < |
| - | In order to run **bash** commands, a particular tool called | + | <code sh> |
| + | cd /home/jonas | ||
| + | # Move up one directory | ||
| + | cd .. | ||
| + | # Move to home directory | ||
| + | cd | ||
| + | </ | ||
| + | == pwd - show current directory == | ||
| - | ***//Command-line completion//** | + | Syntax: pwd |
| + | <code sh> | ||
| + | cd /home/jonas | ||
| + | pwd | ||
| + | /home/jonas | ||
| + | </ | ||
| - | *Writing a shell script | + | == ls - list files == |
| - | *Navigating the file system | + | |
| + | 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 == | ||
| - | ===== Essential commands ===== | + | 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 / | ||
| + | mv myfile1 / | ||
| + | </ | ||
| + | == cp - copy files == | ||
| - | **//Finding where you are //** pwd | + | Syntax: cp <input file> <output file name or directory> |
| + | <code sh> | ||
| + | </code> | ||
| + | == rm - remove files == | ||
| - | After logging in the psycomp server either through the command line interface or through GUI, it will be important to know which location you areon the computer. | + | 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 == | ||
| - | **//List files://** ls | + | Syntax: |
| + | <code sh> | ||
| + | </code> | ||
| + | == grep - find text in a file == | ||
| - | **//Change directory://** cd | + | Syntax: |
| + | <code sh> | ||
| + | </code> | ||
| + | == sort - sort text == | ||
| - | **//Rename or move files: //** mv | + | Syntax: |
| + | <code sh> | ||
| + | </code> | ||
| + | == man - show help for a program == | ||
| - | **//Create files and directories: //** touch, mkdir | + | Syntax: |
| + | <code sh> | ||
| + | </code> | ||
| + | == ps, top - show CPU usage and running processes == | ||
| - | **//Delete files and directories: //**rm, rmdir | + | Syntax: |
| + | <code sh> | ||
| + | </code> | ||
| + | == 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 |
| - | **//Check CPU usage: //**top, ps | + | <code bash> |
| + | # To kill a program if you know it's process number (see above), use kill -9: | ||
| + | kill -9 <process number> | ||
| - | **//Text editing: //**pluma, kate, nano | + | #To kill a program whose name you know, use pkill -9: |
| + | pkill -9 firefox | ||
| - | **//PDF viewer: //**atril | + | #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 | ||
| + | </code> | ||
| - | **//Desktop applications: | + | == ssh - remote login to another computer == |
| - | **//Common neuroimaging applications://** FSL, Matlab, AFNI | + | Syntax: pwd |
| + | <code sh> | ||
| + | </code> | ||
| + | == scp - copy files to another computer == | ||
| - | **// | + | Syntax: |
| + | <code sh> | ||
| + | </code> | ||
| + | == rsync - copy large numbers of files fast and efficiently == | ||
| - | **// Getting help: //** man | + | Syntax: |
| + | <code sh> | ||
| + | </code> | ||
linux-survival.1705417218.txt.gz · Last modified: 2024/01/16 15:00 by jonas