Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 2 Shell environment I: - command line interface & basic commands; - directories & files; - standard I/O & file descriptors; CSE4251 The Unix Programming.

Similar presentations


Presentation on theme: "Lecture 2 Shell environment I: - command line interface & basic commands; - directories & files; - standard I/O & file descriptors; CSE4251 The Unix Programming."— Presentation transcript:

1 Lecture 2 Shell environment I: - command line interface & basic commands; - directories & files; - standard I/O & file descriptors; CSE4251 The Unix Programming Environment 1

2 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 2

3 After you logged in... 3

4 user name: zhengm 4

5 host machine name: eta After you logged in... 5

6 current directory: ~ (i.e., /home/zhengm) After you logged in... 6

7 bash command prompt: $ After you logged in... 7

8 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 8

9 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 9

10 Customize your shell Change user prompt – E.g., add the following line to your ~/.bashrc file: 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 \w: current working directory \r : carriage return \n : newline [zhengm@sl2 ~]$ [20:26:06][zhengm@sl2:~] + 10

11 Customize your shell Change user prompt – E.g., add the following line to your ~/.bashrc file: 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 \w: current working directory \r : carriage return \n : newline More about customized alias and paths on later lectures [zhengm@sl2 ~]$ [20:26:06][zhengm@sl2:~] + 11

12 Create account & change password Create an username “joe” (usually in lower case) – $ useradd joe Sets an initial password for “joe” – $ passwd joe (optional) Create a group called “students” – $ groupadd students (optional) Add “joe” to group “students” – $ useradd –G students joe “joe” changes password after logged in using the initial password – $ passwd 12

13 Some commonly used commands $ ls somedir# list files in dir $ cd somedir# enter some directory $ uname –a# check your platform info $ mv oldfilename newfilename# rename a file $ cp srcfilename dstfilename# copy a file $ cat filename # 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 $ rm filename # delete file/directory $ mkdir dirname# create a directory $ rmdir dirname # delete empty directory $ man cmdname# lookup cmd usage 13

14 What you see via “ls” is part of a file system $ ls / 14

15 File system 101 A way to organize data/resource – “everything is a file” on Unix Conceptually a tree structure 15

16 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 16

17 Several Important Directories / – the root directory /bin – executable/binary files /dev – device files /home – personal work directory, default place after logged in 17

18 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) 18

19 Some directory-related commands $ pwd – display current full directory path $ mkdir somedir and $ rmdir somedir – create and delete a directory $ ls – list the files in a directory – -l,-t $ cd somedir – enter a directory 19

20 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 20

21 Access rights for file/directory Check access rights (permissions) – $ ls –l - rw- rw- r--.... usenix.sty_.txt drwxrwxr-x.... WinLog d: directory 21

22 Access rights for file/directory Check access rights (permissions) – $ ls –l - rw- rw- r--.... usenix.sty_.txt drwxrwxr-x.... WinLog access rights (permissions) 22

23 Three groups of Access rights – Owner (u) – The members within a group (g) – Others (o) Access Rights Groups - rw- rw- r--.... usenix.sty_.txt drwxrwxr-x.... WinLog u g o 23

24 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 rwxBinaryOctalpermission 0000000No read, no write, no run 0010011No read, no write, can run 0100102No read, can write, no run 1001004Can read, no write, no run 1111117Can read, can write, and can run 24

25 What is permission 5? – 5 in binary format is : 101 Can read No write Can run Access rights representation 5 = 101 = r-x 25

26 Three groups of access rights, three numbers – Owner (u) – The members within a group (g) – Others (o) 764 owner can: read, write, run group can: read, write Others: can only read Access rights representation = 7 6 4 = 111 110 100 = rwx rw-- r---- 26

27 Commands related to the access right chown: change the owner of a file – e.g., $ chown user_name file_name chgrp: change the group of a file – e.g., $ chgrp group_name file_name 27

28 chmod: change permissions for owner, group, or others – access groups: u, g, o, and a (all groups) – access rights: r, w, x. – e.g.: $ chmod u+rwx, g+rx, o+rx file_name $ chmod 755 file_name $ chmod o-x file_name Commands related to the access right 28

29 Wildcards / Metacharacters for 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 29

30 […] 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 Wildcards / Metacharacters for filename abbreviation 30

31 Programs and Standard I/O Program Standard Input (stdin) Keyboard Output from another program A file Standard Output (stdout) Screen Input to another program A file Standard Error (stderr) 31

32 File Descriptor A file descriptor (fd) is an abstract indicator for accessing a file. It’s an Integer in Unix IntegerName 0Standard Input (stdin) 1 Standard Output (stdout) 2Standard Error (stderr) 32


Download ppt "Lecture 2 Shell environment I: - command line interface & basic commands; - directories & files; - standard I/O & file descriptors; CSE4251 The Unix Programming."

Similar presentations


Ads by Google