Upload
annabelle-mcdaniel
View
247
Download
2
Embed Size (px)
Citation preview
Linux Commands
C151 Multi-User Operating Systems
Expressions in CommandsCurrent directory: ..Parent directory: ....Home directory: ~~Root directory: //
PathAbsolute pathAbsolute path: complete sequence of
directories to a file starting from the root directory //:/home/yul/c151/cat.jpg
Relative pathRelative 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 /bin/bin executable for single user mode /dev/dev devices that can be mounted by the system /etc/etc local configuration, administrative things /home/home home directories for most users /lib/lib shared libraries /mnt/mnt temporary mount point for mountable devices /proc/proc kernel and process information /root/root home of the superuser (root) /sbin/sbin system binaries (executables) /tmp/tmp temporary files /usr/usr lots of information used by the system
Linux UsersThere is one special user that has
administrative powers - the superuser. It's name is rootroot. Every user has a home directory that can be found under /home/username/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 VariablesThe 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 CommandsSmall utilities/programs under Linux that can
take options and arguments.ArgumentArgument: what the command applies to.
lsls /usr/usrOptionOption: how do we want the command to be
executed
lsls –l–lThe options and arguments can be combined:
ls ls -l-l /usr/include/usr/includeSome commands take more than one argument
cp requires a source file and a destination file.
More On CommandsSome useful commands: cd, ls, man, pwd, cat, find, grep, wc
Manipulating files and directories: mv, rm, cp, mkdir, rmdir
Linux Commandsls: list files
Example: ls ~cd: switch working directories
Example: cd \pwd: display current working directory
Example: pwdman: display manual of command
Example: man lscat: display the entire content of a file
Example: cat lab1.txt
Linux Commandsmkdir: create a directory
Example: mkdir C151rm:delete files
Example: rm *.txtrmdir: delete a directory
Example: rmdir C151
Linux CommandsViewing the Contents of a File
The more commandDisplays the entire contents of a file one page
at a timeSpacebar moves through file one page at a
timeEnter key moves through file one line at a time Only moves forward through a file, not
backwardExit: qExample 1: more lab1.txtExample 2: man ls | more
Linux CommandsViewing 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 CommandsViewing the Contents of a File
(continued)The head command
Displays just the first ten lines of a fileThe tail command
Displays just the last ten lines of a fileUsage: same as more and less
Linux CommandsThe find command: find files
The argument is a path. The name of the file must be specified with the option –name.
There more optionsFind file with name “hw1.txt” in current
directory (including sub directories)
find ./ -name hw1.txtFind file with name extension “.txt” in root
(including all sub directories)
find / -name “*.txt”
11 - 15
Linux Commandsmv: 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 Commandscp: 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: PicoProvides a series of commands at the
bottom of the screenBackspace, Delete, and other keys work
as expectedEasy to UseStart Pico
Pico file_nameExit Pico
[ctrl]+x
Linux ProcessesA 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 processkill pid
The signal 9 is the most powerful. Kill a process with id 1258
Kill 1258 kill 9 1258
Launching Jobs on LinuxA jobjob 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 fgfg.
PipePipe (|) enables you to pass the output
of one command through the input of another command
ls -l | moreThe 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 RedirectionRedirecting the input: command < filenamecommand < 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 > filenamecommand >> filenamecommand >> filename
Combine input and ouput redirectionsExample:
wc < my_text_file.txt > output_file.txt
Compressing FilesThe most common utilities for compressing and
decompressing files on Linux are tartar, gzipgzip, and gunzipgunzip.
tartar allows us to compress several files (directories) into one archive. Options: cfcf for compressing, xfxf for decompressing.Example: tar –cf C151.tar C151
gzipgzip compresses one file at a time.Options: none to compress, -d-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.gz.tar.gz or simply .tgz.tgz.
Decompressing FilesSuppose we have a compressed file
called C151.tar.gz, how could we decompress it?1. gzip –d C151.tar.gz (or gunzip
C151.tar.gz)We get C151.tar
2. tar –xf C151.tarWe get uncompressed files (folders)
File Ownership and PermissionsEvery file belongs to a particular user, generally
the creator of that file. This is the ownerowner of the file. Users may be organized in groupsgroups and a group can have special permissions to a file.
A file can be accessible for reading rr, writing ww, or executing xx. 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: chmodchmod
File Ownership and PermissionsLinux file and folder attributes seen with
ls -lColumn of 10 characters on left
First character: file (-), directory (d), or link (l)2nd, 3rd, and 4th characters show permissions of
owner5th, 6th, and 7th characters show permissions of
group8th , 9th, and 10th 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 numberFile or directory name to change
Examplechmod 644 reports
Reading AssignmentTextbook: Chapter 3, Chapter 4, and
Chapter 5