Post on 22-Aug-2020
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
BI296: Linux and Shell Programming
Lecture 02: Linux Command Line
Maoying,Wuricket.woo@gmail.com
Dept. of Bioinformatics & BiostatisticsShanghai Jiao Tong University
Spring, 2019
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
Quick Recaps
I Decimal/Binary/Octal/Hexadecimal numbers and theircross-conversion
I Original digit (源码), One’s complement(反码), Two’scomplement(补码)
I bit, byte, kilobyte, megabyte, gigabyte, terabyte, ...I Richard Stallman, GNU, Free and Open-source, FSFI Linus Torvalds, Unix, Linux, TuxI Middleware of hardware and user, utilities
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
Linux Commands
I Many things on a Linux system can be done by typingcommands
I Note that the GUI (X-window) is not needed for running a Linuxsystem.
I In order to type commands in X-window you need to start aterminal emulator
I Command PromptI Can be configure yourself, the default isI $ - “logged in as a regular user”I # - “logged in as root (privileged user)”
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
Start Linux
Now start Linux and log in as administrator“root”; and then open a terminal on yourdesktop. See, what’s your commandprompt? “#” or “$”?
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
Command Syntax
Syntax format<command> [option(s)] [argument(s)]
Examples# list files and directories in current directoryls# list files and directories lengthilyls -l# list files and directories in /devls /devls -al /dev
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
Some Basic Commands
I System information:uname, uptime, arch, nproc, hostname, hostid
I User management: id, user, passwd, mkpasswd, useradd,
userdel, groupadd, groupdel, groupmems
I Environment-related: env, export
I File system:ls, file, cd, mkdir, rmdir, rm, mv, cp, stat
I Help information: man, info, whatis
I Process control: ps, pstree, fg, bg, jobs, nohup
I File searching: locate, whereis, which, find
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
who, what, where, where to go
I pwd: Print Working DirectoryI uname: Print the system informationI whoami: Who am II cd: Change working Directory
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
pwd: knowing where you are now
I Synopsis: pwdI All the path names are started with a slash (“/”), for exampleI “/root” is the home directory for rootI “/home/xxx” is the home directory for user “xxx”I Use command “which pwd” can show you where your command
locatesI Most of the user commands are stored in /bin or /usr/binI Commands for super user are in the directories /sbin and
/usr/sbin
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
uname: knowing what my system is
I Synopsis: uname option(s)
I uname -a: --all
I uname -i: --hardware-platform
I uname -m: --machine
I uname -n: --node-name
I uname -o: --operation-system
I uname -r: --kernel-release
I uname -s: --kernel-name
I uname -v: --kernel-version
I uname -p: --processor
I You can use uname --help to obtain more information for thiscommand.
I This command is very useful in compiling the system-dependentcode.
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
whoami: knowing who you are
I Synopsis: whoamiI The command is different from “who am i”I and also “who”I Now guess what the command “who” can do for you?I And also the command “w”?
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
cd: change directory to
I Synopsis: cd dir name
I Command “cd” without any argument will direct you to yourhome directory
I Command “cd ∼xxx” can direct you to the home directory ofuser “xxx”
I You can use either absolute pathname to visit the directory, e.g.“cd /tmp” or
I You can also use relative pathname to visit the relevant directory.I If you are root, you can visit anywhere; but if you are a normal
user, maybe you will be forbidden to visit somewhere, forexample “/root”.
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
Say Something
I echo "I am genius!"#using double quotes
I echo ’I am genius!’ #using single quotes
I echo I am genius! #no quotes
I echo $SHELL #print environment variable
I echo $HOSTNAME #print environment variable
I echo "$HOSTNAME is running under $SHELL"
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
Say Something More Formally
printf
[bio@localhost ˜]$ printf "UserName\tUserID\t\tGroupID\t\tDescription\n"UserName UserID GroupID Description[bio@localhost ˜]$ printf "The cost is \v%-5.2f dollars\n" 12.5The cost is
12.50 dollars
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
The other system-context commands
I date: print or set the system date and time.I arch: print machine hardware name.I nproc: print the number of processing units available.I hostname: show or set the system’s host name.I hostid: print the numeric identifier for the current host.I uptime: tell how long the system has been running.
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
How to get help for a command
I “man” commandI for example “man ls” can return the manual for the command “ls”I manpage is stored in the directory/usr/share/man/
I “info”I command --help or command -h
I HOWTO DocumentationI Refer to Internet
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
man
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
The man command (1)
I With the man command you can get the manual page ofcommands
I Manual pages are stored in /usr/man or /usr/share/manI The manual page consists of:
I NAME: The name of the command and a online descriptionI SYNOPSIS: The syntax of the commandI DESCRIPTION: Explanation of how the command works and what
it doesI Files: The files used by the commandI Bugs: Known bugs and errorsI See Also: Other relevant commands
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
The man command (2)
I The “-k” optionI man -k print
I Manual pages are divided into 8 sections:1. User commands2. System calls3. Libc calls4. Devices5. File formats and protocols6. Games7. Conventions, macro packages and so forth8. System administration
I To select the correct section, add section number:I man 1 passwdI man 5 passwd
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
info
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
Working with Files and Directories
What is a file?I A collection of dataI An object that can be read from, or written to, or both.I All the objects in Linux are filesI A file has certain attributes include
I File typeI Access permissions
File StructureI Generally: byte-stream, record sequence, record treeI In Linux: byte stream
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
Directory Structure
I Linux File System Standard: http://www.pathname/fhs
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
File typesI ls -l /dev/sda1
I file /dev/sda1
File type Description Example
Regular file (-) Plain data file /etc/passwd, /bin/bash
Directory (d) A directory entry /usr
Block special file (b) Block device /dev/sda1
Character special file (c) Character device /dev/null
FIFO (p) Named pipe /run/systemd/initctl/fifo
Socket (s) /tmp/mysqld.sock
Symbolic link (l) Symbolic link /usr/lib64/libc.so.6
[bio@localhost ˜]$ file /usr/lib64/libc-2.17.so/usr/lib64/libc-2.17.so: ELF 64-bit LSB shared object, x86-64, version 1 (GNU
/Linux), dynamically linked (uses shared libs), BuildID[sha1]=53c0918c85fa9cc08d2b57e76467631ab07554ae, for GNU/Linux 2.6.32, notstripped
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
File permissionsI Four access levels
I user(u): The user that owns the fileI group(g): The group that owns the fileI others(o): The others except the owner and groupI all(a): all the users
I Three permissionsI Read(r): Read content of a file or list content of a directoryI Write(w): Change content of a file or create/delete files in a
directoryI Execute(x): Execute files as a program or use directory as active
directoryI Special file permission
I Setuid, setgid(s): special permissions for executable files. Anyuser who runs that executable file assumes the user ID of theowner/group of the file.
I Sticky-bit(t): special permission for public directories.
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
Directory permissions
Symbol Description
r List files in the directory.
w Add or remove files/directories/links in the directory.
xOpen/execute files in the directory,
change to the directory and subdirectories.
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
Change the permission of a file
Command Description
ls -l filename list the properties of a file
chown bio:bio filename change the owner/group of a file
chgrp bio filename change the group of a file
chmod 755 filename change the mode/permission of a file
chmod u+w filename add write permission for the owner
chattr -A filename change the attribute of a file
lsattr filename list the attributes of a file
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
umask
I By default, the system sets the permission on a text file to 666,and to 777 on a directory or executable file.
I The value assigned by the umask command is subtracted fromthe default.
I umask 022 command denies write permission for group andothers.
I Usually the root and a normal user has different umaskstrategies. Why?
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
Display file status
stat filename
I mtime: last time the file content was modified, ls -l
I ctime: last time of file status modification, ls -lc
I atime: last access time, ls -lu
I crtime: creation time, stat --printf ’%n: created %w\n’
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
Display file contents
I Display entire fileI cat, tac /etc/passwdI less, more /etc/passwdI nl /etc/passwdI od /etc/passwdI base64 /etc/passwd
I Display parts of fileI head /etc/passwdI tail /etc/passwdI split -d -n 2 /etc/passwd passwd.
I Formatting file contentsI fmtI pr -o 5 /etc/passwdI fold -w 20 /etc/passwd
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
The other commands relating to file anddirectory
I Directory listing: ls, dir
I Basic operations: cp, dd, mv, rm
I Directory operations: mkdir, rmdir, cp, mv
I Changing file attributes: chgrp, chmod, chown, touch
I Summarizing files: wc, sum, md5sum
I Sorting files: sort, uniq, comm
I Operating on fields: cut, paste, join
I Operating on characters: tr, expand
I Creating link file: ln
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
ln: creating link file
I ln a b: create hard link b for aI ln -s a b: create symbolic link b for aI Can you create a hard link for a directory? symbolic link?I Can you create a hard link between different operating systems?
symbolic link?
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
Exercise (1)
1. What is the difference between man 1 printf andman 3 printf?
2. We have known that the command tree can print out thedirectory tree. How to set the output level to be 2?
3. Compare ls -a and ls -A. Can you find what this option ”-a” isable to do?
4. What is the difference among commands cat, less and more
to view the content of a regular file?
5. What is the usage of the command umask? List the possibledistinctions between dealing with files and with directories?
6. Tell the difference between hard link and symbolic link? If Iwould like to use ln to create a hard link file2 for file1, whatshould I do? If we delete file1, what will happen to file2? Howabout symbolic link?
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
Exercise (2)
1. What is the difference between whoami and who am i?
2. What does chmod 735 file1 do? If we want to prevent a foldernew from being accessed by all the user except the owner, whatshould we do?
3. How to create a multiple-level directory /tmp/a/b/c/d usingmkdir in exactly one line command?
4. Tell what the following commands can do for you.I chmod u+w file2I chmod a-x file2I chmod 644 file2I chmod o=rwx file2
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
UsersI Each user has a unique username and userIDI A user can belong to more than one groupI All the users are stored in the file /etc/passwdI The encrypted password for each user is stored in the file
/etc/shadowI The user can be added using useradd command, and modified
by usermod, and deleted by userdel
I For each user, the private environment is defined by the file/.bashrc and the common environment is defined in the file/etc/bashrc and /etc/profile
I The default settings for useradd is defined in the file/etc/default/useradd.
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
Output of user information
I id: print real and effective user and group IDs.I logname: print user’s login name.I whoami: print effective user id.I groups: print the groups the user is inI users: print the user names of users currently logged in to the
current host.I who: show who is logged in.I w: Show who is logged on and what they are doing.
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
Exercise
Have a look at the manual page of the commandgroupmems and the configuration file /etc/sudoers.
1. Log in to the system as the root;
2. Enable the group wheel has the sudo privilege;
3. Recruit the user ’bio’ into the group wheel;
4. Use command id to confirm whether the above job work;
5. Try run sudo su - root to check whether the sudo privilegework for user bio.
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
Process control (1)
Command Description
CTRL+z Stop (interrupt) a foreground process.
CTRL+c Kill a foreground job.
ps list the running processes.
pstree print the process tree.
fg run the suspended job in foreground.
bg Run the suspended job in background.
jobs List the background or suspended jobs.
CMD & place the job in the background.
nohup CMD & run in the background.
kill Kill a running process or suspended jobs.
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
Process control (2)
1. Type in the command yes I am genius
2. Press CTRL+z to suspend the running process
3. Use jobs to look through the suspended or backgroundprocesses
4. Use fg %job-id to bring the process to foreground, or
5. Use bg %job-id to run the job in background, or
6. use kill %job-id to kill the process
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
Standard streams (标准输入/输出/错误流)
I Standard Input (stdin: 0)I Generally it is a keyboard.
I Standard Output (stdout: 1)I Generally it is the terminal or the printer.
I Standard Error (stderr: 2)I Generally it is the terminal.
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
Piping (管道)
I The STDOUT of a command acts as the STDIN of the nextcommand.
I ExamplesI cat /etc/paswd | wc -l
count the number of lines in the file.I cat /etc/passwd | grep mysql
see if the user exists.I ls -l /dev | less
view the content as streamI gunzip file.tar.gz | tar xvf -
uncompress the .tar.gz file.
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
Redirection (重定向)
I STDIN RedirectionI cat 0< /etc/passwdI cat < /etc/passwd
I STDOUT RedirectionI ls -l > filelist.log
redirect the output to the fileI ls -l 1> filelist.log
redirect the stdout to the fileI ls -l >> filelist.log
append the stdout to the fileI STDERR Redirection
I ls -z 2> err.logredirect the stderr to the file
I ls -z 1> run.log 2>&1redirect both the stdout and stderr to the file
I ls -z 2>/dev/nullsuppress the output of the stderr
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
Disk usage (硬盘使用信息)
I fdisk -l
manipulate disk partition table.I df -h
report file system disk space usage.Filesystem Size Used Avail Use% Mounted on/dev/sda2 50G 35G 16G 69% //dev/sda3 177G 106G 72G 60% /home/dev/sda1 497M 158M 340M 32% /boot
I du -sh /home/bio
estimate file space usage.102G /home/bio
I stat -f /
display system status.File: "/"
ID: fd0000000000 Namelen: 255 Type: xfsBlock size: 4096 Fundamental block size: 4096Blocks: Total: 13100800 Free: 4125851 Available: 4125851Inodes: Total: 52428800 Free: 51628882
BI296-Lec02
Maoying Wu
Linux BasicCommands
Getting Help
File-relatedCommands
User-RelatedCommands
Process control
Input/Output/Error
Exercises
The command ls -i can list the inode number of therequested file.
1. Use echo and redirection to create a new file;
2. Use cp to create a new copy of the above file;
3. Use mv to create a new file of the above file;
4. Use ln to create a hard link of the above file;
5. Use ln -s to create a symbolic link of the above file;
6. Use ls -i to check whether the files have the same index node.
7. Tell the difference among cp, mv, ln, ln -s.