290 likes | 312 Vues
Learn essential Linux commands, file systems, users, and environment variables. Practice commands such as ls, cd, man, cat, find, grep, and more for file manipulation efficiency.
E N D
C151 Multi-User Operating Systems Linux Commands
Expressions in Commands • Current directory: . • Parent directory: .. • Home directory: ~ • Root directory: /
Path • Absolute path: complete sequence of directories to a file starting from the root directory /:/home/yul/c151/cat.jpg • Relative path: the sequence of directories to reach a file starting from the current directory:c151/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/ + c151/cat.jpg
Linux File Systems /root directory /binexecutable for single user mode /devdevices that can be mounted by the system /etclocal configuration, administrative things /homehome directories for most users /libshared libraries /mnt temporary mount point for mountable devices /proc kernel and process information /root home of the superuser (root) /sbin system binaries (executables) /tmp temporary files /usr 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 • $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: what the command applies to.ls/usr • Option: how do we want the command to be executedls–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 lab1.txt
Linux Commands • mkdir: create a directory • Example: mkdir C151 • rm:delete files • Example: rm *.txt • rmdir: delete a directory • Example: rmdir C151
Linux Commands • Viewing the Contents of a File • The more command • Displays the entire contents of a file one page at a time • Spacebarmoves 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 lab1.txt • Example 2: man ls | more
Linux Commands • Viewing the Contents of a File (continued) • The less command • Nearly equivalent to the more command • Allows you to move forward and backwardin 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 “hw1.txt” in current directory (including sub directories) find./-name hw1.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 file1.txt grep “ligyu” file1.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
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
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 • Pico file_name • Exit Pico • [ctrl]+x
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: psUseful 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.
Pipe • 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 < filenameAny 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 > filenamecommand >> 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, gzip,and gunzip. • tar allows us to compress several files (directories) into one archive. • Options: cf for compressing, xf for decompressing. • Example: tar –cf C151.tar C151 • gzip compresses one file at a time. • Options: none to compress, -d to decompress. • Example: gzip C151.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.gzor simply .tgz.
Decompressing Files • Suppose we have a compressed file called C151.tar.gz, how could we decompress it? • gzip –d C151.tar.gz (or gunzip C151.tar.gz) • We get C151.tar • tar –xf C151.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) • 2nd, 3rd, and 4th characters show permissions of owner • 5th, 6th, and 7th characters show permissions of group • 8th , 9th, and 10th characters show permissions of all others
Managing Files and Directories with Shell Commands • File and Folder Permissions(continued)
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