The linux command line--part 3 Common Tasks And Essential Tools

来源:互联网 发布:淘宝食品许可证 编辑:程序博客网 时间:2024/04/28 14:51

Part 3 - Common Tasks And Essential Tools

 14 – Package Management

   How A Package System Works

    Package Files

  High And Low-level Package Tools

Common Package Management Tasks

Installing A Package From A Package File

Removing A Package

 

 eg。  apt-get remove emacs

Updating Packages From A Repository

 

Listing Installed Packages

 

Determining If A Package Is Installed

  

Displaying Info About An Installed Package

  

Finding Which Package Installed A File

15 – Storage Media

● mount – Mount a file system
● umount – Unmount a file system
● fsck – Check and repair a file system
● fdisk – Partition table manipulator
● mkfs – Create a file system
● fdformat – Format a floppy disk
● dd – Write block oriented data directly to a device
● genisoimage (mkisofs) – Create an ISO 9660 image file
● wodim (cdrecord) – Write data to optical storage media
● md5sum – Calculate an MD5 checksum

Mounting And Unmounting Storage Devices

 


Viewing A List Of Mounted File Systems

Determining Device Names

  ls /dev

 

Creating New File Systems

Manipulating Partitions With fdisk

Creating A New File System With mkfs

Testing And Repairing File Systems

Formatting Floppy Disks

Moving Data Directly To/From Devices

Creating CD-ROM Images

Creating An Image Copy Of A CD-ROM

  genisoimage -o cd-rom.iso -R -J ~/cd-rom-files

Writing CD-ROM Images

Writing An Image

16. Network

   Linux is used to build all sorts of networking systems and appliances, including firewalls,routers, name servers, NAS (Network Attached Storage) boxes and on and on.

● ping - Send an ICMP ECHO_REQUEST to network hosts
● traceroute - Print the route packets trace to a network host
● ip - Show / manipulate routing, devices, policy routing and tunnels
● netstat - Print network connections, routing tables, interface statistics, masquerade
connections, and multicast memberships
● ftp - Internet file transfer program
● wget - Non-interactive network downloader
● ssh - OpenSSH SSH client (remote login program)

 Examining And Monitoring A Network

 ping

  ping www.baidu.com

  A successful “ping” will indicate that the elements of the network (its interface
cards, cabling, routing, and gateways) are in generally good working order

 traceroute

    traceroute slashdot.org

 ip

  ip a

  lo is the loopback interface,a virtual interface that the system uses to "talk to itself"

netstat

  netstat -ie

  netstat -r

 Transporting Files Over A Network

 ftp

  lftp – A Better ftp

  wget

  wget http://linuxcommand.org/index.php

 Secure Communication With Remote Hosts

  ssh

  scp and sftp

17 – Searching For Files

● locate – Find files by name
● find – Search for files in a directory hierarchy
We will also look at a command that is often used with file-search commands to process
the resulting list of files:
● xargs – Build and execute command lines from standard input
In addition, we will introduce a couple of commands to assist us in our explorations:
● touch – Change file times
● stat – Display file or file system status

locate – Find Files The Easy Way

find – Find Files The Hard Way

  find ~ -type f | wc -l

  

find ~ -type f -name "*.JPG" -size +1M | wc -l


Operators

 

 

Operators

   find ~ \( -type f -not -perm 0600 \) -or \( -type d -not -perm 0700 \)

Predefined Actions

User-Defined Actions

Improving Efficiency

xargs

A Return To The Playground

Options

18 – Archiving And Backup(归档 和备份)

压缩:

● gzip – Compress or expand files
● bzip2 – A block sorting file compressor

归档:

● tar – Tape archiving utility
● zip – Package and compress files

文件同步程序:

rsync – Remote file and directory synchronization

压缩文件

  basic goals 不变 ---摆脱冗余的数据

  压缩算法有两种:无损 和 有损

gzip

  gunzip 是 gzip的逆

bzip2

归档文件

 tar


zip

Synchronizing Files And Directories

  rsync options source destination
  where source and destination are one of the following:

● A local file or directory
● A remote file or directory in the form of [user@]host:path
● A remote rsync server specified with a URI of rsync://[user@]host[:port]/path

Using rsync Over A Network

19 – Regular Expressions(常规表达式)

 grep  global regular expression print

    ls /usr/bin | grep zip

 

grep命令选项及其意义:
-c              #只输出匹配行的数量
-i              #搜索时忽略大小写
-h              #查询多文件时不显示文件名
-l              #只列出符合匹配的文件名,而不列出具体的匹配行
-n              #列出所有的匹配行,并显示行号
-s              #不显示不存在或无匹配文本的错误信息
-v              #显示不包含匹配文本的所有行
-w              #匹配整词
-x              #匹配整行
-r              #递归搜索,不仅搜索当前工作目录,而且搜索子目录
-q              #禁止输出任何结果,以退出状态表示搜索是否成功
-b              #打印匹配行距文件头部的偏移量,以字节为单位
-o              #与-b选项结合使用,打印匹配的词距文件头部的偏移量,以字节为单位
-E              #支持扩展的正则表达式
-F              #不支持正则表达式,按照字符串的字面意思进行匹配

 查找文件中的字符串

  grep aaa file1.txt

 只返回文件名不需要具体内容

  grep -l aaa file1.txt

Metacharacters And Literals(元字符和常量)

 ^ $ . [ ] { } - ? * + ( ) | \

The Any Character

   grep -h '.zip' dirlist*.txt

正则表达式 ^zip 会匹配以zip 开头的字符串

 zip$ 会匹配以zip结尾的字符串

 ^zip$ 匹配 zip 字符串

POSIX Character Classes


   ls /usr/sbin/[[:upper:]]*

  |  或者

 数量

 ?匹配一个元素 或者 零个

  * 匹配零个元素 或者更多个

 + 匹配一个元素 或者更多元素

  {} 匹配一个元素出现的次数


20 – Text Processing 文本处理

  ● cat – Concatenate files and print on the standard output
● sort – Sort lines of text files
● uniq – Report or omit repeated lines
● cut – Remove sections from each line of files
● paste – Merge lines of files
● join – Join lines of two files on a common field
● comm – Compare two sorted files line by line
● diff – Compare files line by line
● patch – Apply a diff file to an original
● tr – Translate or delete characters
● sed – Stream editor for filtering and transforming text
● aspell – Interactive spell checker

Applications Of Text 文本的应用

  文档

  网页

  邮件

  打印机打印

  程序源代码

再访问我们之前学习的命令

   前面第六章重定向大致讲了cat,现在更详细的讲cat是如何用来文本处理。

  cat

  -A 选项  显示所有内容,包括 non-printing characters 例如结束符$,Tab符^I

 cat -A foo.txt

 -n  显示行数

 -s  将很多空白行只保留一行

  sort  待续

  uniq

Slicing And Dicing

 cut  选取命令,将一段数据经过分析,取出我们想要的。一般来说,选取信息通常是针对“行”来进行分析的,并不是整篇分析的。

  -b :字节为单位分割

  -c  :字符

  -d  :自定义分隔符,默认制表符。

  -f :与 -d 一起,指示显示哪个区域

 

 paste

  join

Comparing Text 文本比较

  comm file1 file2  会列出两个文件相同的地方和不同的地方。

  comm -12 file1 file2  显示相同的地方

 有三个选项 1 2 3

  diff 显示两个文件不同的地方

  diff file1.txt file2.txt


diff  -c file1.txt file2.txt

patch

Editing On The Fly

tr

例子 及 返回:

  echo "lowercase letters" | tr a-z A-Z  返回LOWERCASE LETTERS

echo "lowercase letters" | tr [:lower:] A 返回 AAAAAAAAA AAAAAAA

echo "aaabbbccc" | tr -s ab 返回 abccc

echo "abcabcabc" | tr -s ab 返回abcabcabc

sed

 是 stream editor 的缩写

echo "front" | sed 's/front/back/' 返回 back

echo "front" | sed 's_front_back_' 返回back

echo "aaabbbccc" | sed 's/b/B/' 返回 aaaBbbccc

echo "aaabbbccc" | sed 's/b/B/g' 返回 aaaBBBccc

aspell

  用法: aspell check textfile

   语法检查

21 – Formatting Output 格式化输出

● nl – Number lines
● fold – Wrap each line to a specified length
● fmt – A simple text formatter
● pr – Prepare text for printing
● printf – Format and print data
● groff – A document formatting system

 简单的格式化工具

  nl 显示文件的行号

  fold 将每一行分割成指定的长度 如果没有长度参数就默认80字符 -s 选项不会在行末尾拆开英文单词

echo "The quick brown fox jumped over the lazy dog."| fold -w 12

echo "The quick brown fox jumped over the lazy dog."| fold -w 12 -s

  fmt 一个简单的文本格式化工具

  printf 格式化打印 print formatted

例子:printf "I formatted '%s' as a string.\n" foo

 


  文件格式化系统

    groff

    troff nroff rnoff

22 – Printing 印刷

● pr – Convert text files for printing
● lpr – Print files
● a2ps – Format files for printing on a PostScript printer
● lpstat – Show printer status information
● lpq – Show printer queue status
● lprm – Cancel print jobs

   简短的历史关于printing

Printing With Linux

 pr 打印 可以在管道中应用作为一个过滤器

 lpr 打印文件(伯克利风格)

 lpr -P printer_name

    ls /usr/bin | pr -3 | lpr

 lp  打印文件(系统V风格)

  监控和控制打印工作

   lpstat 展示打印系统的状态

     lpstat -a

  lpq 展示打印机队列状态

  lprm/cancel 取消打印工作

23 – Compiling Programs 编译程序

   为什么编译软件?两个原因:

  一、可用性

  二、及时性

● make – Utility to maintain programs

   sudo make install

  三个简单的命令:

  ./configure

  make

  make install

0 0