Introduction to the Linux Command Line. Sabry Razick (RCS Nov 2015) What Is a Terminal
Total Page:16
File Type:pdf, Size:1020Kb
Introduction to the Linux command line. Sabry Razick (RCS Nov 2015) What is a terminal ● The terminal is an interface in which you can type and execute text based commands. ● It can be much faster to complete some tasks using a Terminal than with graphical applications and menus. ● Allowing access to many more commands and scripts. ● When making your own program, it is easier and faster to design it to work on the terminal than with the mouse. ● More transparency on what is going on ● More control on what you do ● ……. How to access the terminal ● Unix based systems (Redhat, Ubuntu….Mac OS) it is there as a program already. ● Windows - Windows command line (cmd) can not do the thing Unix terminal do. So you need a Virtual environment or get access a Unix machine ● Virtual environment ○ Oracle Virtual Machine - www.oracle.com/us/technologies/virtualization/oraclevm/ ○ Cygwin - https://www.cygwin.com/ ○ Boot with a live CD/Falsh drive ● Access a Unix machine ○ Putty - http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html Windows ● abel.uio.no ● freebee.abel.uio.no ● Use UIO username and password Unix ● Locate the terminal The shell ● Shell provides an interface between you and the operating system of the computer ● Use the keyboard to tell the computer what you want of it ● Where is my home ○ echo $HOME ● Go to my home ○ cd $HOME ● What files or folders have in my home ○ ls ● Give me more details about the files ○ ls -lh The shell ● Find out more about a command ○ man <COMMAND> ○ e.g. man ls ● List them in sorted ○ ls -lhrt (list oldest first) ○ ls -lh --sort=size ● Set permissions ○ chmod a+r test.txt ○ chmod g+r test.txt ○ chmod u+x test.txt ○ chmod +x test.txt The shell ● Create a directory ○ mkdir rcs2015 ● Changed to that directory ○ cd rcs2015 ● I am not sure where I am ○ pwd ● Create file ○ touch test.txt ○ echo “some text” > test2.txt ○ nano test3.txt (Ctrl + o -- Save, Ctrl -x to get out) ● list the files that ends with txt ○ ls -lh *txt Path ● Path specifies a position in the directory tree. There are two kinds of paths. ○ absolute ○ relative ● Absolute path always starts at at the top of the tree (at the root directory), for example “/home/username”. The absolute path always starts with a “/” (root). ● The relative path is the path to the destination from your current position and it does not start with a “/”. The shell ● Delete a file ○ rm test2.txt ● Delete a directory ○ mkdir tmp ○ rm -r tmp ○ tmdir tmp ● Create another directory and copy the file test.txt ○ mkdir $HOME/rcs2 ○ cp test.txt $HOME/rcs2 ○ cd $HOME/rcs2 ○ cp ../rcs2/test.txt . ○ cp <FULLPATH>/test.txt <FULLPTH>/ ● Move a file (keep the copy delete the original, rename) ○ mv test2.txt mv test_new.txt The shell ● Which machine am I in ○ hostname ● Who else is logged in ○ who ● How am I logged in ○ who am i ● What are the things running at the moment ○ top ● What are the thing I am running ○ top -u <USER_NAME> ● Do I have internet connection ○ ping uio.no FIles ● Create a new file and add a content ○ nano filetest.txt ○ AAAACCTTGCCGGTCCC… ○ Ctrl + o, Ctrl +x ● Display the content ○ less filetest.txt ○ less filetest.txt | more ○ cat filetest.txt ● Find whether there is a specific string in the file ○ grep “AA” filestest.txt ● Find out the file with the name filetest.txt ○ cd .. ○ find -name filetest.txt Files ● Find the file containing the string AAA, look everywhere starting from current directory ○ grep -Rni “AA” ○ grep -RH --include='*txt "AA" . ● Peak ○ head test.txt ○ tail test.txt ● nano table.txt A B C D E A1 B1 C1 D1 E1 A2 B2 C2 D2 E2 A2 B3 C2 D3 E3 A4 B4 C2 D4 E4 awk , sort, uniq , tr ● Print the first column ○ awk '{print $1}' table.txt ● Create a new file by shifting column one and two ○ awk '{print $2"\t"$1"\t"$3"\t"$4}' table.txt > table2.txt ● Display unique values on first column ○ cat table.txt |awk '{print $1}'| sort | uniq ● Collapse raws ○ tr '\n' ';' < emails.txt Programs ● Where the program is installed ○ which ls ● Create a small program ○ echo "hi \$1" > p1.sh ● Make it executable ○ chmod +x p1.sh ● Run it ○ ./p1.sh AAA ● Add it to path so you can run it from anywhere ○ echo $PATH ○ export PATH=$PATH:$HOME/rcs2015/ Extra ● sed ○ sed -n '2,3p' table.txt ● Processes ○ ps -ef | grep bash ● Replace a string in a file (no undo !) ○ find . -type f -name "3.txt*" -exec sed -i "s/A/B/g" {} + Extract 2 columns and join them with a dot. (column separated with a space) file: ProjectID PID@104555 SentrixBarcode SentrixPo 000388 1045550292122 4515597045 R01C01 000388 1045550667913 4515597045 R02C01 000388 1045550442282 4535791163 R01C01 000388 1045550660020 4535791163 R01C0 command cat Sentrix_104555_Case_3642_.txt | awk -F' ' '{print $3,".", $4}'| sed -e 's/ //g'.