Introduction to Open Source Software Development Spring semester, 2017 School of Computer Science and Engineering, Pusan National University Joon-Seok Kim LINUX: COMMANDS
Review Lab #1 2
Create Directories Assume that username is cse. The user s home directory will be /home/cse. You may remember what the relative pathname for users home directory is: ~ $ pwd /home/cse/workspace/lab1 You need to create the Workspace directory in the home directory. Also, you should make Lab1 directory in the Workspace directory. The following are best practice: $ mkdir ~/Workspace $ cd ~/Workspace $ mkdir Lab1 $ cd ~ $ mkdir Workspace $ cd Workspace $ mkdir Lab1 $ cd $ mkdir Workspace $ cd Workspace $ mkdir Lab1 3
Copy Files Assume that the config file is copied from /etc/python/debian_config file. $ ls -l total 4 -rw-r--r-- 1 cse cse 94 3월 7 12:00 config lrwxrwxrwx 1 cse cse 16 3월 7 12:00 link ->../../201624000 The following are best practice: $ cp /etc/python/debian_config ~/Workspace/Lab1/config $ cd ~/Workspace/Lab1 $ cp /etc/python/debian_config config $ cd /etc/python/debian_config $ cp debian_config ~/Workspace/Lab1/config 4
Create Symbolic Links Assume that you created a directory with student id (e.g. 201624000) in the home directory. $ ls -l total 4 -rw-r--r-- 1 cse cse 94 3월 7 12:00 config lrwxrwxrwx 1 cse cse 16 3월 7 12:00 link ->../../201624000 You should create a symbolic link, named link, to the directory. $ cd ~/Workspace/Lab1 $ ln -s../../201624000 link $ cd ~ $ ln -s 201624000 Workspace/Lab1/link 5
Modify Timestamp Assume that the date is after 2017-03-07. Otherwise, a year will appear instead of time. (e.g. 2017) $ ls -l total 4 -rw-r--r-- 1 cse cse 94 3월 7 12:00 config lrwxrwxrwx 1 cse cse 16 3월 7 12:00 link ->../../201624000 Even though a symbolic link has a pointer to a file or directory, it has its own inode and data block. The default touch command may modify timestamp of the referenced file. Therefore, you have to add option -h into touch command to modify timestamps of a symbolic link. For more information, perform man touch and see -h option. $ cd ~/Workspace/Lab1 $ touch -h -t 03071200 link 6
Files and Metadata 7
Hard and Soft Link Hard and soft link mechanism inode # File Name Hard Link Original File Soft Link Metadata Address Inode#1 Inode#2 Data blocks File data Link data Partition A Partition B 8
File Type and Status (1/2) Commands file stat getfacl Brief description Determine file type Display file or file system status Get file access control lists $ cd ~/Workspace/Lab1; ln -s ~/201624000 symlink $ file symlink symlink: symbolic link to /home/cse/201624000/ $ stat symlink File: 'symlink' -> '/home/cse/201624000/' Size: 25 Blocks: 0 IO Block: 4096 symbolic link Device: 801h/2049d Inode: 978763 Links: 1 Access: (0777/lrwxrwxrwx) Uid: ( 1000/cse) Gid: ( 1000/cse) Access: 2017-03-09 00:13:26.735502135 +0900 Modify: 2017-03-09 00:13:24.107502206 +0900 Change: 2017-03-09 00:13:24.107502206 +0900 Birth: - $ 9
File Type and Status (2/2) $ stat -L symlink File: 'symlink' Size: 4096 Blocks: 8 IO Block: 4096 directory Device: 801h/2049d Inode: 983073 Links: 2 Access: (0775/drwxrwxr-x) Uid: ( 1000/cse) Gid: ( 1000/cse) Access: 2017-03-09 00:13:05.151502717 +0900 Modify: 2017-03-09 00:13:05.127502718 +0900 Change: 2017-03-09 00:13:05.127502718 +0900 Birth: - $ stat -f symlink File: "symlink" ID: 9229d5a17a15e5e3 Namelen: 255 Type: ext2/ext3 Block size: 4096 Fundamental block size: 4096 Blocks: Total: 4869551 Free: 3773331 Available: 3520211 Inodes: Total: 1245184 Free: 1017083 10
Notation of Permissions Symbolic Notation Numeric Notation ---------- 0000 no permissions Meaning -rwx------ 0700 read, write, & execute only for owner -rwxrwx--- 0770 read, write, & execute for owner and group -rwxrwxrwx 0777 ---x--x--x 0111 execute --w--w--w- 0222 write --wx-wx-wx 0333 write & execute -r--r--r-- 0444 read -r-xr-xr-x 0555 read & execute read, write, & execute for owner, group and others SECURITY RISK -rw-rw-rw- 0666 read & write -rwxr----- 0740 owner can read, write, & execute; group can only read; others have no permissions 11
Command: getfacl $ cd ~; getfacl.bashrc # file:.bashrc # owner: cse # group: cse user::rwgroup::r-- other::r-- $ getfacl Documents # file: Documents/ # owner: cse # group: cse user::rwx group::r-x other::r-x $ 12
Permission Commands chmod chgrp chown su sudo passwd Brief description Change file mode bits Change group ownership Change file owner and group Change user ID or become superuser Execute a command as another user Change user password 13
Command: chmod, sudo, su $ cd ~/Workspace/Lab1 $ ls -l config -rw-r--r-- 1 cse cse 94 3 월 9 05:00 config $ chmod 777 config $ ls -l config -rwxrwxrwx 1 cse cse 94 3 월 9 05:00 config $ chmod a-x config $ ls -l config -rw-rw-rw- 1 cse cse 94 3 월 9 05:00 config $ chmod o+x config $ ls -l config -rwxrw-rw- 1 cse cse 94 3 월 9 05:00 config $ sudo su - [sudo] password for cse: # pwd /root # cd /home/cse/workspace/lab1 # 14
Command: chown, chgrp # chgrp root config # ls -l config -rwxrwxr-x 1 cse root 94 3월 9 05:00 config # chown root:cse config # ls -l config -rwxrwxr-x 1 root cse 94 3월 9 05:00 config # chown root:root config # chmod 750 config # ls -l config -rwxr-x--x 1 root root 94 3월 9 05:00 config # exit logout $ cat config cat: config: Permission denied $ sudo cat config [sudo] password for cse: [DEFAULT] # how to byte-compile (comma separated: standard, optimize) byte-compile = standard 15
I/O Streams The Unix / Linux standard I/O streams with numbers: Handle Name Description 0 stdin Standard input 1 stdout Standard output 2 stderr Standard error Redirection metacharacters for redirection of I/O Character Action > Redirect standard output >& Redirect standard output and standard error < Redirect standard input >> Append standard output >>& Append standard output and standard error 16
I/O Streams with Command Command Syntax command > file command 2> file command > file 2>&1 command < file command < file.in > file.out command >> file command 2>> file command >> file 2>&1 command <<c command command2 command 2>&1 command2 Task Send stdout to a file Send stderr to file Send stdout and stderr to file Read stdin from a file Read stdin from file.in andsend stdout to file.out Append stdout to the end of a file Append stderr to the end of a file Append stdout and stderr to the end of file Read stdin from the keyboard until the character c Pipe stdout to command2 Pipe stdout and stderr to command2 17
Echo and I/O Stream (1/3) $ echo Hello Hello stdin echo stdout Hello (keyboard) (console) stderr (console) 18
Echo and I/O Stream (2/3) $ echo Hello > example.txt $ echo Hello 1> example.txt Hello stdin echo stdout example.txt (keyboard) (file) stderr (console) 19
Echo and I/O Stream (3/3) $ echo Hello 1> example.txt 2> example.txt $ echo Hello > example.txt 2>&1 Hello stdin echo stdout example.txt (keyboard) (file) stderr 20
Pipe $ cat example.txt grep llo example.txt input cat stdout grep (file) (console) stderr stderr (console) (console) 21
Print Commands Commands echo cat head tail more less grep Brief description Display a line of text Concatenate files and print on the standard output Output the first part of files Output the last part of files File perusal filter for crt viewing (space, enter key) Opposite of more Print lines matching a pattern 22
Command: echo $ echo 'This is the "first" line!'; echo "This is the 'second' line." This is the "first" line! This is the 'second' line. $ echo -e 'This is the first line!\nthis is the second line.' This is the first line! This is the second line. $ echo "This is the first redirection example." > example.txt $ cat example.txt This is the first redirection example. $ echo "This is append mode." >> example.txt $ cat example.txt This is the first redirection example. This is append mode. $ echo "overwrite the file" > example.txt $ cat example.txt overwrite the file $ 23
Command: cat $ cat > example.txt I am typing this text. To escape, you need to send signal Control-C. ^C $ cat example.txt I am typing this text. To escape, you need to send signal Control-C. $ cat ~/.bashrc # ~/.bashrc: executed by bash(1) for non-login shells. # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc) # for examples # If not running interactively, don't do anything case $- in *i*) ;; *) return;; esac... 24
Command: head and tail $ head n 8 ~/.bashrc # ~/.bashrc: executed by bash(1) for non-login shells. # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc) # for examples # If not running interactively, don't do anything case $- in *i*) ;; *) return;; $ tail n 8 ~/.bashrc # this, if it's already enabled in /etc/bash.bashrc and /etc/profile # sources /etc/bash.bashrc). if! shopt -oq posix; then if [ -f /usr/share/bash-completion/bash_completion ]; then. /usr/share/bash-completion/bash_completion elif [ -f /etc/bash_completion ]; then. /etc/bash_competion fi $ 25
Command: more, less $ dmesg... $ dmesg more... $ dmesg less... $ dmesg grep usb [ 0.365482] usbcore: registered new interface driver usbfs [ 0.365491] usbcore: registered new interface driver hub [ 0.365521] usbcore: registered new device driver usb [ 1.483609] usb usb1: New USB device found, idvendor=1d6b, idproduct=0002 [ 1.483612] usb usb1: New USB device strings: Mfr=3, Product=2, SerialNumber=1 [ 1.483614] usb usb1: Product: EHCI Host Controller [ 1.483615] usb usb1: Manufacturer: Linux 4.8.0-36-generic ehci_hcd [ 1.483617] usb usb1: SerialNumber: 0000:02:03.0 [ 1.484925] usb usb2: New USB device found, idvendor=1d6b, idproduct=0001 [ 1.484927] usb usb2: New USB device strings: Mfr=3, Product=2, SerialNumber=1 [ 1.484929] usb usb2: Product: UHCI Host Controller [ 1.484931] usb usb2: Manufacturer: Linux 4.8.0-36-generic uhci_hcd [ 1.484932] usb usb2: SerialNumber: 0000:02:00.0 $ dmesg grep pci less 26
Editors $ apropos editor grep text ed (1) - line-oriented text editor ex (1) - Vi IMproved, a programmers text editor gedit (1) - text editor for the GNOME Desktop gnome-text-editor (1) - text editor for the GNOME Desktop red (1) - line-oriented text editor rview (1) - Vi IMproved, a programmers text editor rvim (1) - Vi IMproved, a programmers text editor sed (1) - stream editor for filtering and transforming text vi (1) - Vi IMproved, a programmers text editor view (1) - Vi IMproved, a programmers text editor vim (1) - Vi IMproved, a programmers text editor xedit (1) - simple text editor for X $ gedit newfile.txt $ xedit newfile.txt $ nano newfile.txt $ vi newfile.txt 27