Shell Basics1

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
  1. bash usually have $ in the prompt
  2. C shell uses %
  3. tcsh users often use >
  4. Superuser #
~$ echo $SHELL
~$ grep yourloginname /etc/passwd
~$ cat /etc/shells

Shell usage

~$ <option(s)> <argument(s)>

~$ 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>

SectionDescription
1Executable programs and shell commands.
2System calls (functions provided by the kernel)
3Library calls (functions within program libraries)
4Special files
5File formats and conventions
6Games
7Miscellaneous (including macro packages and conventions)
8System 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 name
mv – 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 navigate
less: “LESS is MORE”
head: the command head shows the first 10 lines
tail: 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 and stderr 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 an ls 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值