Linux Tutorial for new users
Last updated June 27, 2005
- This is more a brief tutorial for new users of linux.
- Linux is generally a more text based interface operating system. This has the advatage of being very efficient for users who are familiar with the system but can cause a lot of confusion for new users. This tutorial assumes you know about directory structures.
- This tutorial only covers the general commands or things you may run into in order to be brief. Details can be found in the Linux Unleashed Manual or websites.
- To enter the command prompt (where you enter commands) you must right click the Linux screen and click New terminal.
General format of most commands
- Most commands follow a general structure which is COMMAND -OPTIONS SOURCE OUTPUT/DESTINATION
- Each of these 4 parts are separated by a single space (nothing more or less). Each part is explained below
- COMMAND tells the computer generally what to do like copying or moving files for example or running a specific program. Not all commands need the other 3 parts of a general command (like with running applications).
- OPTIONS follow the COMMAND preceeded by a - and multiple options can be specified by typing the letter of the options with no space. For example COMMAND -hr will specify the h and r option (doing -h -r does the same thing). You can use multiple options as long as it relates/usable by that command. Some options are also whole words.
- SOURCE is the input for the command or what file/directory the command should work on. If the file to be worked on is in the same directory as where your typing the command. If it is not then you must specify the directory. This can be done in 2 ways. (removing filename in examples for specifying directories)
1) The first directly specifies where the file is by typing the whole directory structure beginning with / and separated with / for each level of the directory. For example COMMAND /home/fMRI/welcome.doc telling the command the precise location of the file.
2) The second method is to specify relative to where you are already by using a ./ (period slash). Say you are in /home/fMRI/ and you type in COMMAND ./subjects/202.doc. This tells the computer to work on the file 202.doc in /home/fMRI/subjects/202.doc.
- OUTPUT/DESTINATION specifies how the file after being processed by the command should be named (OUTPUT) or the directory is should be put in (DESTINATION). This follows the same directory rules as SOURCE.
Commonly used commands (with commonly used options) (commands are encased in [])
- Changing the directory you are working in [cd SOURCE] where SOURCE is the subdirectory name (but you can specify directories by placing the total directory structure). If you don't specify a SOURCE and just go cd then it will go to your home directory. If you put .. as source it will go down a directory level
eg cd /home/fMRI/ or cd fMRI while in /home/ will do the same thing
- Checking space on hard drives [df] shows all the hard drives and the amount of space used and available to them.
- Copying one file to a different directory [cp SOURCE DESTINATION]
- Erase/Removing a file/wholedirectory tree [rm -OPTIONS SOURCE]
- OPTIONS ( -r ) removes a whole directory with all of its subdirectories where SOURCE is the name of the sub-directory current found in the current directory. Be very careful using this command as there is no confirmation box. If you do not include this option then SOURCE should specify a file.
- Erasing a directory [rmdir DESTINATION] which only works if the directory with all of it's subdirectories are empty.
- Listing files of directory [ls -OPTIONS SOURCE] a SOURCE does not need to be specified but is useful if you are looking for a certain naming of files in this directory (explained better in Wildcards). This is will be listed in alphabetical order.
- OPTIONS ( -l ) gives details of the files (permissions,when created,etc)
- Making a new directory [mkdir NAME] where NAME is the directory name that will be created attached to this directory.
eg while in /home/fMRI/ if you type mkdir subjects, you will create a directory in fMRI called subjects.
- Mounting CDs or floppy discs [mount /mnt/DEVICE]. In order for linux to use CDs or discs, you have to mount it. DEVICE can either be cdrom or floppy. When you are done with your floppy/CD then you must unmount it by using [umount /mnt/DEVICE].
- Moving or Renaming files [mv SOURCE DESTINATION/OUTPUT] where you move when you specify a directory/DESTINATION or rename the file if you specify an OUTPUT (or filename).
- Remotely running a computer on a different machine [ssh COMPUTERNAME] where COMPUTERNAME should specify a computer name. It should ask for a password if it can connect. Any commands/programs/etc will be run as if you were on that machine.
- Where am I? [pwd] tells you which directory you are working in now
Wildcards/Shortcuts
- This section is on wildcards that you can use so you don't need to precisely specify file names. This is only used in the SOURCE part of commands. Wildcards can be anything that the computer can find ONLY in the directory you are working in (unless you specify one). This is useful in listing, copying, and removing files.
- ? is when you being ambiguous about one character. Say you are in a directory that has multiple similarly named files like subject001.doc subject002.doc subject003.doc subject011.doc subject1011.doc. When want to list the files with a single subject number, you can go ( ls subject00?.doc ) which would list subject001.doc subject002.doc subject003.doc but not the other two. You can use this ambiguity with any commands but it should specify one file in the end for commands.
- * specify any amount of characters. For example a ls *.* lists all files with a period anywhere in the filename regardless of the name. This is useful copying all files in a directory (assuming they all have a period) like cp /home/fMRI/*.* ./ copies all files from /home/fMRI/ to your current directory. For another example using the above pool of files, ls sub*1* would list subject001.doc and subject011.doc subject1011.doc. This is very powerful but easy to make mistakes so be very careful with using this with commands like rm.
- When typing commands you can also use the TAB key on the keyboard to let the computer fill in the blanks (based on whether the command should specify a file or directory). Say you have files gift.doc car.doc girl.doc and you want to use one file from this set for a command. For example you start typing cp c but if you press TAB, it will fill the rest for you (in this case car.doc). If more than one file could be implied by this, it will give a beep or fill in as much as possible before it isn't sure what to do. For example in the same file pool, if you start typing cp g and TAB, it will give you a beep as both girl.doc and gift.doc fill this criteria of beginning with a g. For the above subject files, you could go s then TAB which will fill subject for you but you must specify more letters after.
Applications
- Section for commonly used programs. You can just type the command for it in. Add a space and & [PROGRAM &] will run the program and keep the terminal usable. Many of these programs can be run from the Redhat menubar. The advantage of running applications by command however is that when remotely on a computer, running applications by command line runs the programs as if you were on that machine (an therefore allow you to save in that computer as well).
- [afni] runs afni and if you add directory names to the end separated by spaces each then you can quickly change subdirectories in a directory by just changing session eg afni 100 101 102 will allow you to change to the contents of subdirectory 100 101 102 by changing session in afni.
- [gedit] is the text editor program
- [gftp] used to transfer files for computer to computer. Just change the upper right drop box to SSH2 and type in the computer name for host. It will ask for the username and password to the machine and then you can files back and forth between computers. The left file window is the computer you are on and the right is the computer you are connected to. Just highlight the file and use the appropriate arrow in the middle to start the transfer.
- [gimp] graphical program used like a paint program to alter aspects of your image
- [mozilla] web browser to the internet
- [ooimpress] is the powerpoint package on linux called openoffice
- [oowriter] is the word processor of the openoffice package
Scripts
- Scripts are used to run a whole series of commands one after another without having the user to type and run them all individually. This can be used to save a lot of time.
- This is done to assign certain variables that represent commonly repeating patterns and providing conditional/loop statements to the script.
- Scripts are written in the text editor (gedit). Once it is completed and saved in the directory, you have to change the permission of the file (so you can run it) by chmod a+x SCRIPTNAME (which tells the computer to give (+) all users (a) to execute it (x)) and run it by typing ./SCRIPTNAME.
- Variables: are used to represent a string of text or numbers. This is done by [set VARIABLENAME=STRING]. eg set stim=time. These variables can be recalled using the ${VARIABLENAME}. Say you have similarly named files and need to do the same command on them like copying subject001, subject002, subject003 to a temp directory. you could set sub=subject and then just do cp ${sub}001 ./temp cp ${sub}001 ./temp cp ${sub}001 ./temp in a script. It doesn't seem that useful here but if you commonly name your files a certain way, all you would have to do is change certain variables (like project names) and run the script. You could even get a directory to be represented by a string. This would be useful if we had to save to ./temp/work/subjectpool for multiple commands. It could easily be done by set directory=./temp/work/subjectpool and using cp ${sub}001 ${directory}
- Loops: are used to set variables to different strings until a "loop" ends. The way variables are defined in a loop are a bit different. The syntax is [foreach VARIABLENAME ( VARIABLE1 VARIABLE2 VARIABLEn )] [End]. Therefore you can recall VARIABLENAME and it will spit out VARIABLE1 until the script runs into the End, go back up to the foreach line and the VARIABLENAME will be represented by VARIABLE2 and keep on going until it has gone through all the variables in the list. For example
foreach sub ( 50 52 53 55 57 60 61 62 71 77 78 82 83 84 88 90 91 92 94 )
cp ./${sub}/${sub}.doc ./analysis
end
This will copy all these subjects from their own subject directories and put them in the analysis directory.
- Other commands in scripts (which can be used are regular commands but are more useful in scripts
[echo OUTPUT] outputs whatever text out on the terminal screen. For example, echo Hello bob would display Hello bob on the screen. This can be used to place loop variables in to make sure it is using the right variables like echo ${sub} will spit out the subject.
[expr 'MATH'] used mainly in variables to do some math. For example set count=0 and set count1=`expr ${count} + 1` will add the value of count with 1 and store it in variable count1.
- Quotation Marks
These are very useful if you have strings that have spaces (which would normally disrupt a command)or a long complicated filename/equations. Different quotation marks do different things. A double quotation mark "" is used to hide the white space from the shell (essentially ignore it) but will read special characters still (like recalling of variables). A single quotation '' hides the spaces but takes everything literally. To illustrate the command echo "Hello ${name}" would result in outputing of Hello and whatever is stored in the name variable while a single quotation would result in output Hello ${name}. The backslash \ hides only a single character from the shell. This is useful for commands extending to more that one line for organization. By using a \ before the hard return, the computer will ignore the hard return and move to the next line. Without it, the computer assumes that is the end of the command and likely give an error.
- Other tips for scripts
If your script does not work it is usually something obvious like an extra space, missed character, missing quotation mark. Another common mistake with scripts is to forget to put an End for each loop statement. If you need to ever need to stop a script from running (like if you find and error) then you can press ctrl z and it will stop it. Be warned that you should erase any output files it creates as you cannot automatically overwrite that already exist.
- File permissions
Explanation of file permissions will not be covered as you rarely have to worry about it (as mostly you are the one who creates the file/directory giving you permission already to read and write). This is only a problem if you cannot save or write files in a directory (ie. you do not have the permission to write in that directory). Details are in any linux book if required.
Other Comments
- Remember that all things are case sensitive including commands, options, file/directory names so you should watch out for that.
- Also do not use spaces when saving file names (use _ ) as the computer expects the next part of a command when seeing a space.
by
Francis Shih 2005
Back to U of S fMRI web page.