Shell Basics1
文章目录
- Getting Started
- Shell
- Shell usage
- Shell – Environment variable
- Variables
- Basic commands
- Man(ul) pages
- Directory Navigation导航
- Other usage of ls
- Interact with files and directories
- What’s in a file? [again]
- Input and Output (I/O)
- Output redirection
- Error redirection
- Summary (out & err redirection)
- Input redirection
Getting Started
- Everything in Linux is considered to be either a file or a process:
- A process is an executing program identified by a unique process identifier, called a PID.
- A file is a collection of data, with a location in the file system called a path. (separated by forward slashes “/”)
- A directory is a special type of file, which is used to hold information about other files.
- A symbolic link is a special type of file which refer to another file in the filesystem. 软连接
Linux中硬连接(hard link)与软连接(symbolic link)的区别
Shell
- There are many Shells.
sh (Bourne shell), csh (C shell), tcsh
ksh (Korn shell), pdksh (Public domain ksh)
bash (Bourne-agin shell)
zsh - Which Shell am I running?
Various prompts are used in different shells
- bash usually have $ in the prompt
- C shell uses %
- tcsh users often use >
Superuser #
~$ echo $SHELL
~$ grep yourloginname /etc/passwd
~$ cat /etc/shells
Shell usage
~$
~$ which grep
- Tab completion
Shell – Environment variable
~$ echo $PATH
~$ export
Variables
- Add
${var_name}
to use a variable ({} optinal) - No
[space]
while defining a variable - Environment variables:
$SHELL
,$PATH
, … - System variables:
$$
,$?
,$1
, …
Basic commands
- Show what’s in a file:
~$ cat <filename>
- Demonstrate the system time:
~$ date
- List all of your own current running processes:
~$ ps
Man(ul) pages
~$ man <program or command>
~$ man –k <command>
Section | Description |
---|---|
1 | Executable programs and shell commands. |
2 | System calls (functions provided by the kernel) |
3 | Library calls (functions within program libraries) |
4 | Special files |
5 | File formats and conventions |
6 | Games |
7 | Miscellaneous (including macro packages and conventions) |
8 | System administration commands (usually only for root) |
Directory Navigation导航
- navigate command:
~$ cd <directory name>
- root directory:
/
- home directory:
~
- current directory:
~$ pwd
ls
command: display the files in the current directory or any directory specified with a path- Use
–a
option to display all files (dot file included) - Use
–l
option to display in long format (detailed info)
Other usage of ls
-t
option: Oldest Files-u
option: Newest Files- use the wildcard * to view files of a specific type
~$ ls *.h
Interact with files and directories
mkdir
– make a new directory of the given namemv
– move files, directories, or both to a new location~$ mv file1 Newdir
Tips: use mv to rename files;~$ mv file1 file2
Tips: use wildcard like *
to move all files of a specific type~$ mv *.c ../CodeDir
cp
– copy files, directories, or both to a new location
Tips: to copy a directory, use –r
option (recursively)rm
– remove files or directories permanently永久的 from the system
Tips: DO NOT TRY sudo rm /*
alias
: 设置指令的别名
alias[别名]=[指令名称]
Which of the follow behavior can somehow help to avoid using “rm /*” accidentally?
- Take good care when using “rm”
- Always use –i option with rm
- Add alias rm=‘mv $1 ./.trash’
I want to filter out a specified type of files (say like extension .zj) in “myfolder” and move
them to a new directory “mywork”. Which of these command do I need to do?
- mv *.zj mywork/
- cd myfolder
- mkdir mywork
Suppose I am a writer, and I’ve finished writing my new book with independent text files for each chapter, such as chap1.txt, chap2.txt, …, along with a “prologue”, “contents”, etc.
Which command would better help me to examine all the chapter txt file from the others?
- cat chap*.txt
What’s in a file? [again]
more
: compare with cat, show “more” detailed info of the file, use [space] to navigateless
: “LESS is MORE”head
: the command head shows the first 10 linestail
: the command tail –n shows the final n lines~$ cat file
~$ cat chap1 chap2 chap3
~$ cat chap1 chap2 chap3 > book
~$ cat phone_bill_this_month >> phone_bill_this_year
Input and Output (I/O)
- Inputs and outputs of a program are called streams in Linux.
- There are three types of streams:
stdin (0)
– stream data going into a program (by default from the keyboard)stdout (1)
– data is written out by a program (by default to the screen)stderr (2)
– program output error messages (by default to the screen) - Use “>” or “<” to redirect the streams
Output redirection
- With the redirection operator
>
, we can save the output (stdout
) from a program to a file - 追加模式
>>
Error redirection
stdout
andstderr
both consider screen as the default output target.- With redirection operator
2>
, we can redirect only stderr to a separate file
Command “history” displays last 1000 history input prompts by default. Please try these following command in this order in your terminal and think what happened? and What’s in myhistory?
history > myhistory
history >> myhistory
history 2> myhistory
history 2>> myhistory
- What happened?
wc
command: count word in the file
with –l option: only displays the line count - What’s the meaning?
Please try command “strace
”
strace常用来跟踪进程执行时的系统调用和所接收的信号。 在Linux世界,进程不能直接访问硬件设备,当进程需要访问硬件设备(比如读取磁盘文件,接收网络数据等等)时,必须由用户态模式切换至内核态模式,通 过系统调用访问硬件设备。strace可以跟踪到一个进程产生的系统调用,包括参数,返回值,执行消耗的时间。~$ strace ls
it will display all the system call when execute anls
command - what happens when execute
~$ strace ls >log1
? - what happens when execute
~$ strace ls 2>log2
?
Summary (out & err redirection)
- difference between > and >>
- descriptors of stdin, stdout and stderr are 0, 1, 2 respectively
- thus, use > or 1> to redirect stdout,
2>
to redirect stderr
Tips: redirect out to multiple files~$ command|tee outputfile1 outputfile2
~$ command|tee –a outputfile
redirect stderr with tee~$ command 2>&1|tee outputfile
把错误流和输出流合并
Input redirection
- By using the redirection operator <, Input can also be given to a command from a file instead of typing it in the shell
~$ cat > inputfile < source_file
<<
indicates that the input stream ends after typing the following [symbol]~$ cat > inputfile << EOF
I have two files in current directory: a and b, I want to copy data in b to a, which command would be useful?
- cp a b
- cat > a < b
还没有评论,来说两句吧...