350 likes | 494 Views
CSE4251 The Unix Programming Environment. Lecture 2 Shell environment I: - command line interface & basic commands; directories & files ; standard I/O & file descriptors;. Recap. Shell a non-graphic user interface to access Unix-like OSes a process a command interpreter
E N D
CSE4251 The Unix Programming Environment Lecture 2 Shell environment I: - command line interface & basic commands; directories & files; standard I/O & file descriptors;
Recap • Shell • a non-graphic user interface to access Unix-like OSes • a process • a command interpreter • Log into shell • on Linux machine: • open terminal via GUI (normally) • on Windows machine: • run PuTTY(w/ Pageant) to connect to remote a Linux server • run Linux locally via VirtualBox/VMware-Workstation
After you logged in ... user name: zhengm
After you logged in ... host machine name: eta
After you logged in ... current directory: ~ (i.e., /home/zhengm)
After you logged in ... bash command prompt: $
Choose your shell • Check your current shell • $ echo $SHELL • $ echo $0 • Check availabe shells • $ cat /etc/shells • $chsh –l • Switch to another shell • $ chsh –s /bin/dash (changing to dash) • won’t take effect until next login • directly type the shell name e.g.: $ dash • temporarily change current shell without re-login (may not be supported on some OSes) $ cat /etc/shells /bin/sh /bin/bash /sbin/nologin /bin/tcsh /bin/csh /bin/dash
Customize your shell • Every shell supports some customization • user prompt • shortcuts (alias) • paths • Customization takes place in startup files • configurations read by the shell automatically when it starts • system-wide config. file: /etc/profile (normally don’t touch) • your config. file: ~/.bashrc
Customize your shell • Change user prompt • E.g., add the following line to your ~/.bashrc export PS1="[\t][\u@\H:\w]\r\n+ " \t : the current time in 24-hour HH:MM:SS format \u : the username of the current user \H : the hostname \r : carriage return \n : newline [zhengm@sl2 ~]$ [20:26:06][zhengm@sl2:~] +
Customize your shell • Change user prompt • E.g., add the following line to your ~/.bashrc export PS1="[\t][\u@\H:\w]\r\n+ " \t : the current time in 24-hour HH:MM:SS format \u : the username of the current user \H : the hostname \r : carriage return \n : newline • More about customized alias and paths on later lectures [zhengm@sl2 ~]$ [20:26:06][zhengm@sl2:~] +
Create account & change password • Create an username “joe” (usually in lower case) • $ useraddjoe • Sets an initial password for “joe” • $ passwdjoe • (optional) Create a group called “students” • $ groupaddstudents • (optional) Add “joe” to group “students” • $ useradd –G students joe • “joe” changes password after logged in using the initial password • $passwd
Some commonly used commands • $ lssomedir# list files in dir • $ cdsomedir # enter some directory • $ uname –a # check your platform info • $mvoldfilenamenewfilename# rename a file • $ cpsrcfilenamedstfilename# copy a file • $ catfilename # display the content of a file • $ tail -n filename# display the last n lines of file • $ head -n filename # display the first n lines of file • $rmfilename # delete file/directory • $mkdirdirname# create a directory • $rmdirdirname # delete empty directory • $ mancmdname# lookup cmd usage
File system 101 • A way to organize data/resource • “everything is a file” on Unix • Conceptually a tree structure
The Structure of Directories • A file cc.txt in directory bb under directory aa under the root directory can be represented as /aa/bb/cc.txt • . means current directory $ cd . #still in current directory $ ./prog #run ‘prog’ in current dir • .. means parent directory $ cd .. #change to parent directory
Several Important Directories • / • the root directory • /bin • executable/binary files • /dev • device files • /home • personal work directory, default place after logged in
Several Important Directories • /etc • configuration files & some executable shell files • /usr • /usr/inlcude #header files • /usr/lib #libraries • /usr/bin/ #more executables • /sbin • executables which can only be executed by the super user (root)
Some directory-related commands • $pwd • display current full directory path • $mkdirsomedirand $ rmdirsomedir • create and delete a directory • $ls • list the files in a directory • -l,-t • $cdsomedir • enter a directory
Unix is multi-users/multi-tasks • Allow multiple users to log into a shell simultaneously • Allow multiple programs running concurrently • Need protection! • protect different users • protect different programs • Basic protection: access rights of file/directory
Access rights for file/directory • Check access rights (permissions) • $ ls –l - rw- rw- r--. ... usenix.sty_.txt drwxrwxr-x. ... WinLog • d: directory
Access rights for file/directory • Check access rights (permissions) • $ ls –l - rw- rw- r--. ... usenix.sty_.txt drwxrwxr-x. ... WinLog • access rights (permissions)
Access Rights Groups • Three groups of Access rights • Owner (u) • The members within a group (g) • Others (o) - rw- rw- r--. ... usenix.sty_.txt drwxrwxr-x. ... WinLog • u • g • o
Access rights representation • Each group has permissions represented by rwx or 0-7 • r mean read • w means write • x means executable • translated to 0-7 via binary values
Access rights representation • What is permission 5? • 5 in binary format is : 101 5 = 101 = r--x Can read No write Can run
Access rights representation • Three groups of access rights, three numbers • Owner (u) • The members within a group (g) • Others (o) = 7 6 4 = 111 110 100 = rwxrw-- r---- 764 owner can: read, write, run Others: can only read group can: read, write
Commands related to the access right • chown: change the owner of a file • e.g., $ chownuser_namefile_name • chgrp: change the group of a file • e.g., $ chgrpgroup_namefile_name
Commands related to the access right • chmod: change permissions for owner, group, or others • access groups: u, g, o, and a (all groups) • access rights: r, w, x. • e.g.: • $ chmodu+rwx, g+rx, o+rxfile_name • $ chmod 755 file_name • $ chmod o-x file_name
Wildcards / Metacharactersfor filename abbreviation • Shell understands wildcards • Metacharacters: * ? [ ] ~ • * matches anything $ ls*.doc # list all files ending with .doc • ? matches single character $ ls?.doc # list a.doc, b.doc, but not ab.doc
Wildcards / Metacharactersfor filename abbreviation • […] matches any of the enclosed charactors $ ls [ab].doc # only list a.doc or b.doc $ ls *[cx].doc # list files ending with c.doc or x.doc • ~ is a shortcut of your home directory $ cd ~ # change to home directory $ cd ~/music # change to /home/username/music directory
Programs and Standard I/O Program • Standard Output (stdout) • Screen • Input to another program • A file • Standard Input (stdin) • Keyboard • Output from another program • A file Standard Error (stderr)
File Descriptor • A file descriptor (fd) is an abstract indicator for accessing a file. It’s an Integer in Unix