C 151 MultiUser Operating Systems Linux Commands Expressions





























- Slides: 29

C 151 Multi-User Operating Systems Linux Commands

Expressions in Commands Current directory: . Parent directory: . . Home directory: Root directory: ~ /

Path Absolute path: path complete sequence of directories to a file starting from the root directory /: /home/yul/c 151/cat. jpg Relative path: path the sequence of directories to reach a file starting from the current directory: c 151/cat. jpg The absolute paths have the same meaning no matter what the working directory is. The relative paths depend on the current working directory. An absolute path is the concatenation of the current working directory and the relative path: /home/yul/ + c 151/cat. jpg

Linux File Systems / /bin /dev /etc /home /lib /mnt /proc /root /sbin /tmp /usr root directory executable for single user mode devices that can be mounted by the system local configuration, administrative things home directories for most users shared libraries temporary mount point for mountable devices kernel and process information home of the superuser (root) system binaries (executables) temporary files lots of information used by the system

Linux Users There is one special user that has administrative powers - the superuser. It's name is root Every user has a home directory that can be found under /home/username They have an allocated disk space. Every user’s home directory contains configuration (resource) files that define their environment. They all start with a dot. Example: . signature. For example, the file ". cshrc" will always be executed whenever the user logs in or opens a terminal window.

Environment Variables The environment variables are global variables that are defined within the shell and that can be used for many purposes. Their names are in general in all uppercase. Display the value of an environment variable: echo $PATH lookup list for binaries to be executed. $HOME user's home directory

Linux Commands Small utilities/programs under Linux that can take options and arguments. Argument: Argument what the command applies to. ls /usr Option: Option how do we want the command to be executed ls –l The options and arguments can be combined: ls -l /usr/include Some commands take more than one argument cp requires a source file and a destination file.

More On Commands Some useful commands: cd, ls, man, pwd, cat, find, grep, wc Manipulating files and directories: mv, rm, cp, mkdir, rmdir

Linux Commands ls: list files Example: ls ~ cd: switch working directories Example: cd pwd: display current working directory Example: pwd man: display manual of command Example: man ls cat: display the entire content of a file Example: cat lab 1. txt

Linux Commands mkdir: create a directory Example: mkdir C 151 rm: delete files Example: rm *. txt rmdir: delete a directory Example: rmdir C 151

Linux Commands Viewing the Contents of a File The more command Displays the entire contents of a file one page at a time Spacebar moves through file one page at a time Enter key moves through file one line at a time Only moves forward through a file, not backward Exit: q Example 1: more lab 1. txt

Linux Commands Viewing the Contents of a File (continued) The less command Nearly equivalent to the more command Allows you to move forward and backward in the file Usage: same as more

Linux Commands Viewing the Contents of a File (continued) The head command Displays just the first ten lines of a file The tail command Displays just the last ten lines of a file Usage: same as more and less

Linux Commands The find command: find files The argument is a path. The name of the file must be specified with the option –name. There more options Find file with name “hw 1. txt” in current directory (including sub directories) find. / -name hw 1. txt Find file with name extension “. txt” in root (including all sub directories) find / -name “*. txt”

Linux Commands The grep command: match a regular expression against text in a file Display the lines that contain ligyu in file 1. txt grep “ligyu” file 1. txt

Linux Commands mv: rename or move files Requires two parameters: name of original file and new name or location of file Examples: Move file “a. cpp” from home directory to current working directory: mv ~/a. pp. / Rename file “a. cpp” in home directory to “b. cpp” mv ~/a. pp ~/b. cpp 11 - 16

Linux Commands cp: copy files Requires two parameters: path and name of the original file and path and name or the new file Examples: Copy file “a. cpp” from home directory to current working directory (without changing name): cp ~/a. pp. / copy file “a. cpp” in home directory to “b. cpp” cp ~/a. pp ~/b. cpp 11 - 17

Text Editor: Pico Provides a series of commands at the bottom of the screen Backspace, Delete, and other keys work as expected Easy to Use Start Pico file_name Exit Pico [ctrl]+x 11 - 18

Linux Processes A process is any running program. Under Linux a process has an identity (PID), a parent process (PPID), an owner (UID). To view the active processes: ps Useful options: -A (all), -l (long list) Stop the execution of a program launched from a terminal: Ctrl-c Stop the execution of any process kill pid The signal 9 is the most powerful. Kill a process with id 1258 Kill 1258 kill 9 1258

Launching Jobs on Linux A job is any executable run from the terminal. The jobs are launched as foreground tasks by default - they will block the terminal until they are done. To launch a job in the background, use the symbol & at the end of the command line. A job can be intentionally brought to the foreground with the command fg. fg

Pipe (|) enables you to pass the output of one command through the input of another command ls -l | more The way this works is that when the shell sees the pipe symbol, it creates a temporary file to store the result of the first command Then this file becomes the input to second command

Input and Output Redirection Redirecting the input: command < filename Any input will read from that file (must be file). Redirecting the output. The simple > rewrites the output file, while the double one >> appends to the file (must be file). command > filename command >> filename Combine input and ouput redirections Example: wc < my_text_file. txt > output_file. txt

Compressing Files The most common utilities for compressing and decompressing files on Linux are tar, tar gzip, gzip and gunzip tar allows us to compress several files (directories) into one archive. Options: cf for compressing, xf for decompressing. Example: tar –cf C 151. tar C 151 gzip compresses one file at a time. Options: none to compress, -d to decompress. Example: gzip C 151. tar To archive more than one file, most people use a combination of tar and gzip in that order. Those archives have the extension. tar. gz or simply. tgz

Decompressing Files Suppose we have a compressed file called C 151. tar. gz, how could we decompress it? 1. gzip –d C 151. tar. gz (or gunzip C 151. tar. gz) We get C 151. tar 2. tar –xf C 151. tar We get uncompressed files (folders)

File Ownership and Permissions Every file belongs to a particular user, generally the creator of that file. This is the owner of the file. Users may be organized in groups and a group can have special permissions to a file. A file can be accessible for reading r, writing w, or executing x. To view the content of a directory, the directory should be executable. Usually the owner has read and write permission to a file, also execute permission if applicable. The root (super user)has read and write permissions for any file. The command ls -l displays the permissions of a file for the owner, the group, and everyone else (all). Change permissions: chmod

File Ownership and Permissions Linux file and folder attributes seen with ls -l Column of 10 characters on left First character: file (-), directory (d), or link (l) 2 nd, 3 rd, and 4 th characters show permissions of owner 5 th, 6 th, and 7 th characters show permissions of group 8 th , 9 th, and 10 th characters show permissions of all others

Managing Files and Directories with Shell Commands File and Folder Permissions (continued) • Permissions • R = read • W = write • X = execute • - =disabled • Permission mode values • read=4 • write=2 • execute=1

File Ownership and Permissions Change permissions with chmod Requires two parameters Access mode number File or directory name to change Example chmod 644 reports

Reading Assignment Textbook: Chapter 3, Chapter 4, and Chapter 5