2/6/2017 Lecture 2- the Linux operating system CLI (Command line Interface) vs GUI (Graphical User Interface) PHYS 5P10— Introduction To Scientific Computing PHYS 5P10— Introduction To Scientific Computing • Linux is a Unix clone written from scratch by Linus Torvalds with assistance from a loosely-knit team of hackers across the Net. • Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs. • 64% of the world’s servers run some variant of Unix or Linux. The Android phone and the Kindle run Linux. PHYS 5P10— Introduction To Scientific Computing What is Linux? Linux + GNU Utilities = Free Unix • Linux is an O/S core written by Linus Torvalds and others AND What is Linux? • a set of small programs written by Richard Stallman and others. They are the GNU utilities. http://www.gnu.org/ Linux Has Many Distributions The computers that we use have redhat. PHYS 5P10— Introduction To Scientific Computing 1 2/6/2017 Linux/Unix Architecture What is Linux? “Small programs that do one thing well” These are all programs/tasks that can be run from the command line: • • Kernel: The heart of the operating system It interacts with hardware. Memory management, task scheduling and file management. • Shell: The utility that processes your requests. the shell interprets the command and calls the program that you want. • Commands and Utilities: Eg: cp, mv, cat and grep etc. • Files and Directories: All data in UNIX is organized into files. All files are organized into directories. These directories are organized into a treelike structure called the filesystem. PHYS 5P10— Introduction To Scientific Computing In order to interact with the system using the command line, one must open a terminal window : • Network: ssh, scp, ping, telnet, nslookup, wget • Shells: BASH, TCSH, alias, watch, clear, history, chsh, echo, set, setenv, xargs • System Information: w, whoami, man, info, which, free, echo, date, cal, df, free, man, info • Command Information: man, info • Symbols: |, >, >>, <, &, >&, 2>&1, ;, ~, ., .., $!, !:<n>, !<n> • Filters: grep, egrep, more, less, head, tail • Hotkeys: <ctrl><c>, <ctrl><d> • File System: ls, mkdir, cd, pwd, mv, ln, touch, cat, file, find, diff, cmp, /net/<hostname>/<path>, mount, du, df, chmod, find • File Editors: vim,emacs, gedit PHYS 5P10— Introduction To Scientific Computing In order to interact with the system using the command line one must open a terminal window : Window in which commands are entered PHYS 5P10— Introduction To Scientific Computing PHYS 5P10— Introduction To Scientific Computing 2 2/6/2017 UNIX: The Shells • The “Shell” is simply another program which provides a basic human-OS interface. • Shells can run interactively or as a shell script • Two main ‘flavors’ of Shells: • Bourne created what is now known as the standard shell: “sh”, or “bourne shell”. It’s syntax roughly resembles Pascal. It’s derivatives include “ksh” (“korn shell”) and now, the most widely used, “bash” (“bourne shell”), which is what we use. • One of the creators of the C language implemented the shell to have a “C-programming” like syntax. This is called “csh” or “C-shell”. Today’s most widely used form is the very popular “tcsh”. PHYS 5P10— Introduction To Scientific Computing The Linux File System UNIX Shell The shell sits between you and the operating system, acting as a command interpreter. It reads your terminal input and translates the commands into actions taken by the system. The shell is analogous to command.com in DOS. When you log into the system you are given a default shell. When the shell starts up it reads its startup files and may set environment variables, command search paths, and command aliases, and executes any commands specified in these files. PHYS 5P10— Introduction To Scientific Computing Unix/Linux File System NOTE: Unix file names are CASE SENSITIVE! • file system is a hierarchical directory structure • The structure resembles an upside down tree • Directories are collections of files and other directories. The structure is recursive with many levels. • Every directory has a parent except for the root directory. • Many directories have children directories. • Unlike Windows, with multiple drives and multiple file systems, a Linux system only has ONE file system. • The Linux Standard Base (LSB) specifies the structure of a Linux file system. /home/mary/ /home/john/portfolio/ The Path PHYS 5P10— Introduction To Scientific Computing PHYS 5P10— Introduction To Scientific Computing 3 2/6/2017 Typical Linux directories Pathnames • /bin System binaries, including the command shell bin • /boot Boot-up routines boot • /dev Device files for all your peripherals dev etc • /etc System configuration files home • /home User directories lib • /lib Shared libraries and modules • /lost+found Lost-cluster files, recovered from a disk-check lost+found / misc • /mnt Mounted file-systems mnt • /opt Optional software opt •/proc Kernel-processes pseudo file-system proc • /root Administrator’s home directory • /sbin System administration binaries • Absolute Pathnames • In the previous tree /users/usern/file1 is an absolute pathname • Relative pathnames • If you are already in the users directory, the relative pathname for file1 is usern/file1 root sbin tmp •/usr User-oriented software usr • /var Various other files: mail, spooling and logging var PHYS 5P10— Introduction To Scientific Computing More on UNIX paths “~” (tilda) points to the user’s home directory. Useful if you are logging into a workstation with many users. Many of the other paths are inaccessible and unimportant to you ~ is the default working directory when you log in. If you are user “usern”, then /users/usern/file1 is the same as ~/file1. “.” refers to the current directory “..” refers to the parent directory. PHYS 5P10— Introduction To Scientific Computing UNIX Commands A command is a program which interacts with the kernel to provide the environment and perform the functions called for by the user. A command can be: a built-in shell command; an executable shell file, known as a shell script; or a source compiled, object code file. The shell is a command line interpreter. The user interacts with the kernel through the shell. You can write ASCII (text) scripts to be acted upon by a shell. If you are in /users/usern/, then ../ refers to /users/. PHYS 5P10— Introduction To Scientific Computing PHYS 5P10— Introduction To Scientific Computing 4 2/6/2017 Basic Commands Command Anatomy of a Linux Command Meaning ls (-artlh) list files and directories mkdir make a directory cd directory change to named directory pwd display the path of the current directory mv file1 file2 move or rename file1 to file2 cp file1 file2 copy file1 and call it file2 rm file remove a file rmdir remove a directory cat file display a file less file display a file one page a time head/tail file display the first/last few lines of a file PHYS 5P10— Introduction To Scientific Computing $ ls -l -r -s /tmp ls (command or utility or program) -l -r -s (options, or flags –control the flavors of the command) /tmp (argument – what is been operated on) PHYS 5P10— Introduction To Scientific Computing Navigation Directory and file operations • Commands: cd, ls, and pwd • Create a new directory cd / cd /tmp ls ls q* pwd ls -l cd ~ • mkdir mydir1 • Create a new file in a directory • cd mydir1 • gedit file1.txt • Copy a file • cp file1.txt file1_copy.txt • Delete a file or directory • rm file1_copy.txt • rm -r folder1 PHYS 5P10— Introduction To Scientific Computing PHYS 5P10— Introduction To Scientific Computing 5 2/6/2017 Directory and file operations • Rename a file or folder • mv file1.txt file12.txt • mv folder1 folder2 • Move file from one folder to another • mv folder1/file1.txt folder2 • Compress files • gzip, and gunzip Command History and Simple Command Line Editing • Try the history command • Try <Ctrl><r> (only works in BASH shell) • Choose from the command history by using the up ↑ and down ↓ arrows • What do the left ← and right → arrow do on the command line? • Try the <Del> and <Backspace> keys PHYS 5P10— Introduction To Scientific Computing PHYS 5P10— Introduction To Scientific Computing Command: ls Command: ls • ls has many options • More useful options for the “ls” command: • • • • • -l long list (displays lots of info) -t sort by modification time -S sort by size -h list file sizes in human readable format -r reverse the order • “man ls” for more options • Options can be combined: “ls -ltr” PHYS 5P10— Introduction To Scientific Computing • ls -a List all file including hidden file beginning with a period “.” • ls -ld * List details about a directory and not its contents • ls -F Put an indicator character at the end of each name • ls –l Simple long listing • ls –lh Give human readable file sizes • ls –lS Sort files by file size • ls –lt Sort files by modification time PHYS 5P10— Introduction To Scientific Computing 6 2/6/2017 Wildcards Creating files in Unix/Linux • You can substitute the * as a wildcard symbol for any number of characters in any filename. • If you type just * after a command, it stands for all files in the current directory: lpr * will print all files • You can mix the * with other characters to form a search pattern: ls a*.txt end in “.txt” will list all files that start with “a” • • Requires the use of an Editor Various Editors: Can open these from the GUI 1) gedit as well as from the command 2) emacs line 3) vi and • The “?” wildcard stands for any single character: cp draft?.doc will copy draft1.doc, draft2.doc, draftb.doc, etc. PHYS 5P10— Introduction To Scientific Computing Opening gedit from the GUI: PHYS 5P10— Introduction To Scientific Computing Displaying a text file • Various ways to display a file in Unix • • • • • PHYS 5P10— Introduction To Scientific Computing cat more head tail less PHYS 5P10— Introduction To Scientific Computing 7 2/6/2017 Command: cat • Dumps an entire file to standard output • Good for displaying short, simple files Command: more • Dumps an entire file to standard output, but one screen at a time • Press on the tab to display the next screen of output PHYS 5P10— Introduction To Scientific Computing Command: head • “head” displays the top part of a file • By default it shows the first 10 lines • -n option allows you to change that • “head -n50 file.txt” displays the first 50 lines of file.txt Command: tail • “tail” displays the bottom part of a file • Works similarly to head PHYS 5P10— Introduction To Scientific Computing Command: less File Commands • “less” displays a file, allowing forward/backward movement within it • Copying a file: cp • Move or rename a file: mv • Remove a file: rm • return scrolls forward one line, space one page • y scrolls back one line, b one page • use “/” to search for a string • Press q to quit PHYS 5P10— Introduction To Scientific Computing PHYS 5P10— Introduction To Scientific Computing 8 2/6/2017 Command: rm File permissions • To remove a file “recursively”: rm –r • Used to remove all files and directories • Be very careful, deletions are permanent in Unix/Linux • Each file in Unix/Linux has an associated permission level • This allows the user to prevent others from reading/writing/executing their files or directories • Use “ls -l filename” to find the permission level of that file PHYS 5P10— Introduction To Scientific Computing PHYS 5P10— Introduction To Scientific Computing File and Directory Ownership and Permissions File System Ownership and Permissions • Try • All files and directories have a individual and a group ownership. • All files and directories have read (r), write (w), and execute (x) permissions assigned as octets to the individual owner (u), the group (g) owner and all others (o) that are logged into the system. • You can change permissions if you are the individual owner or a member of the group. • Only root can change ownership. • • • • • • • • • • • • cd touch myfile (create file) mkdir mydir (create directory) ls –l myfile (examine file) ls –ld mydir (examine directory) chmod g+w myfile (add group write permission) ls –l myfile chmod ugo+x myfile (add user, group and other execute permission) ls –l myfile chmod ugo+w mydir (add user, group and other write permission) ls –ld mydir chmod a-w (a=ALL, remove user, group and other write permission) PHYS 5P10— Introduction To Scientific Computing PHYS 5P10— Introduction To Scientific Computing 9 2/6/2017 Permission levels • “r” means “read only” permission • “w” means “write” permission • “x” means “execute” permission Command: chmod • If you own the file, you can change it’s permissions with “chmod” • Syntax: chmod [user/group/others/all]+[permission] [file(s)] • Below we grant execute permission to all: • In case of directory, “x” grants permission to list directory contents A file must be converted to executable before it can be run as a program. PHYS 5P10— Introduction To Scientific Computing File Permissions PHYS 5P10— Introduction To Scientific Computing UNIX: More Standard Commands echo: print out a string echo “$HOME is where I want to be” whereis: Show where a file can be found printenv: Display all environment variables grep: Get Regular Expression and Print PHYS 5P10— Introduction To Scientific Computing PHYS 5P10— Introduction To Scientific Computing 10 2/6/2017 UNIX: More Standard Commands Whereis command is helpful to locate binary, source and manual pages of commands in the Linux system. It is very simple utility and provides several options which are given below with examples. $ whereis open open: /bin/open /usr/share/man/man1/open.1.gz /usr/share/man/man2/open.2.gz If we want to locate binary of Linux command, use “-b” option. $ whereis -b whereis whereis: /usr/bin/whereis /usr/bin/X11/whereis • wc: (word count) simply counts the number of words, lines, and characters in the file(s). wc [-clw] [file1 file2 ... fileN] • The three parameters, clw, stand for character, line, and word respectively, and tell wc which of the three to count. • cmp: compares two files. • The first must be listed on command line, while the second is either listed as the second parameter or is read in form standard input. • cmp is very simple, and merely tells you where the two files first differ. cmp file1 [ file2] PHYS 5P10— Introduction To Scientific Computing UNIX: More Standard Commands • One of the most complicated standard unix commands is called diff. • The GNU version of diff has over twenty command line options. It is a much more powerful version of cmp and shows you what the differences are instead of merely telling you where the first one is. diff file1 file2 • touch will update the time stamps of the files listed on the command line to the current time. PHYS 5P10— Introduction To Scientific Computing UNIX: More Standard Commands • • • • • • • • • time date test tee sdiff sort gzip gunzip strings How long your program took to run print out current date/time Compare values, existence of files, etc Replicate output to one or more files Report differences side-by-side Sort a file line by line Compress a file Uncompress it Print out ASCII strings from a (binary) • If a file doesn’t exist, touch will create it. PHYS 5P10— Introduction To Scientific Computing PHYS 5P10— Introduction To Scientific Computing 11 2/6/2017 The tar command The following command writes the output only to the file and not to the screen. $ ls > file The following command (with the help of tee command) writes the output both to the screen (stdout) and to the file. $ ls | tee file Multiple files $ ls | tee file1 file2 file3 PHYS 5P10— Introduction To Scientific Computing The tar command Once you have an archive, you can extract it with the tar command. The following command will extract the contents of archive.tar.gz to the current directory.: tar -xzvf archive.tar.gz It’s the same as the archive creation command we used above, except the -x switch replaces the -c switch. This specifies you want to extract an archive instead of create one. You may want to extract the contents of the archive to a specific directory. You can do so by appending the -C switch to the end of the command. For example, the following command will extract the contents of the archive.tar.gz file to the /tmp directory: tar -xzvf archive.tar.gz -C /tmp PHYS 5P10— Introduction To Scientific Computing Use the following command to compress an entire directory or a single file on Linux. It’ll also compress every other directory inside a directory you specify–in other words, it works recursively: tar -czvf name-of-archive.tar.gz /path/to/directory-or-file Here’s what those switches actually mean: -c: Create an archive. -z: Compress the archive with gzip. -v: Display progress in the terminal while creating the archive, also known as “verbose” mode. The v is always optional in these commands, but it’s helpful. -f: Allows you to specify the filename of the archive. PHYS 5P10— Introduction To Scientific Computing Control characters • You type Control characters by holding down the ‘control’ key while also pressing the specified character. • While you are typing a command: • ctrl-W erases the previous word • ctrl-U erases the whole command line • Control commands that work (almost) any time • ctrl-S suspends (halts) output scrolling up on your terminal screen • ctrl-Q resumes the display of output on your screen • ctrl-C will abort any program PHYS 5P10— Introduction To Scientific Computing 12 2/6/2017 Setting the path: Help with Commands At the shell prompt, type: PATH=$PATH:/path/to/dir1; export PATH Export works like set • Type • man command • help command • info command • And “Yes,” you can always Google it $ export MYAPP=1 Verify that the environment variable is set properly using the echo command. $ echo $MYAPP 1 To export variables permanently, you can add the export command in any of the following start-up files :~/.profile ~/.bash_profile /etc/profile PHYS 5P10— Introduction To Scientific Computing PHYS 5P10— Introduction To Scientific Computing 13
© Copyright 2026 Paperzz