Skip to content

Useful Commands

Some common abbreviations

Abbreviations Description
. represents the current directory
.. represents the parent directory
~ represents the home directory
~username represents the home directory of user username

Software package documentation

/usr/share/doc/packages/

Release Notes

/usr/share/doc/release-notes/

Command help

<command> -h or <command> --help
# tree --help

Manual pages

man [section] command
# man 5 crontab
# man
/sestion options
Show tree command manual:
# man tree

List for keywords:

# man -k keyword

Force mandb to update. Normally this is done daily via a cron job.

# mandb

Search for all instances of a command or a file named crontab

# man -f crontab
# whatis crontab (same output with above command)

# man -k crontab
# apropos crontab (same output with above command)

To go directly to a given man page:

# man 5 crontab
* 1G : go to the 1st line * 10G : go to the 10th line * G : go to the end of the page * /^SELinux : search the word SELinux * /section OPTIONS : go to the section OPTIONS

man共有以下几个章节,比如,man 5 crontab就是进入crontab的第5章节:

  • Executable programs or shell commands (标准命令)
  • System calls (functions provided by the kernel)(系统调用)
  • Library calls (functions within program libraries)(库函数)
  • Special files (usually found in /dev)(设备说明)
  • File formats and conventions eg /etc/passwd (文件格式)
  • Games (游戏和娱乐)
  • Miscellaneous (including macro packages and conventions)(杂项,惯例与协定等网络协定、ASCII code等等的說明)
  • System administration commands (usually only for root) (管理员命令)
  • Kernel routines [Non standard] (其他Linux特定的,用来存放内核例行程序的文档。)

man常用快捷键

  • 翻屏
    • 向后翻一屏:space(空格键)
    • 向前翻一屏:b
    • 向后翻一行:Enter(回车键)
    • 向前翻一行:k
  • 查找
    • /关键词
    • ?关键词
    • n (下一个)
    • N (前一个)

man 中文化。在/etc/profile加入下面alias,可以在man中输出中文

# For man in zh_CH
alias cman='man -M /usr/share/man/zh_CN'

Display descriptions:

whatis command

Info pages:

info command

# info
# info top

From the terminal window display the info pages for the info command by entering:

# info info

Move the cursor to the line referring to (Invoking Info) by pressing Tab Tab

Follow the link by pressing Enter

Move the cursor to the link Note Custom Key Bindings: by pressing Tab(6 times)

Follow the link by pressing Enter

Return to the page Note Custom Key Bindings: by typing (lowercase L): l

Exit the info file by typing: q

pwd command

Display the current working directory

cd command

Change directory

ls command

Display directory contents

* Display hidden files with -a option
* Detailed listing with -l option
* Output is recursive, including all subdirectories with -R option
* With option -F After each name, a character indicates the file type (“/” for directories, “*” for executable files, “|” for FIFO files, “@” symbolic link).

cp command

Copy a file or directory

Syntax: cp [option] <source> <destination>

  • Option -a : Copies a directory and subdirectories (compare -R); symbolic links, file permissions, owners, and time stamps are not changed. 它保留符号链接、文件属性,并复制目录下的所有内容。其作用等于-dpR参数组合。
  • Option -I : Asks before overwriting.
  • Option -R, -r : Copies directories recursively (the directory and any subdirectories). 递归拷贝,包含子目录及(隐含)文件,继承目标目录的权限和属性等
  • Option -l : Makes hardlinks instead of copying (创建硬链接的另外一个方法)
  • Option -s : Makes symbolic instead of copying (创建符号链接的另外一个方法)
  • Option -u : Copies a file only when the source file is newer than the destination file or when the destination file is missing.
  • Option -p : 连同档案的属性一起复制过去,包括修改时间、访问权限、所有者组等,而非使用预设属性;

Labs:

  • Initiate directories and files
    mySUSE:~ # su - pmgr
    pmgr@mySUSE:~> mkdir /data/program
    pmgr@mySUSE:~> mkdir /data/program/general
    pmgr@mySUSE:~> mkdir /data/program/general/staffing
    pmgr@mySUSE:~> touch /data/program/general/program_scope
    pmgr@mySUSE:~> touch /data/program/general/staffing/assignment
    
    mySUSE:~ # su - pm1
    pm1@mySUSE:~> mkdir /data/project1
    pm1@mySUSE:~> mkdir /data/project1/iot
    pm1@mySUSE:~> mkdir /data/project1/iot/bigdata
    pm1@mySUSE:~> touch /data/project1/iot/devicelist
    pm1@mySUSE:~> touch /data/project1/iot/bigdata/math_lib
    
    mySUSE:~ # su - pm2
    pm2@mySUSE:~> mkdir /data/project2
    pm2@mySUSE:~> mkdir /data/project2/erp
    pm2@mySUSE:~> mkdir /data/project2/erp/fin
    pm2@mySUSE:~> touch /data/project2/erp/erp_vision
    pm2@mySUSE:~> touch /data/project2/erp/fin/fin_ar
    pm2@mySUSE:~> chmod g+w /data/project2/erp/erp_vision
    
    pmgr@mySUSE:~> ln /data/project2/erp/erp_vision /data/program/general/p2_erp_version 
        (创建硬链接,当前用户需要对源文件erp_vision有w权限)
    pmgr@mySUSE:~> ln -s /data/project1/iot/devicelist /data/program/general/p1_devicelist 
        (创建符号链接,不验证当前用户是否对源文件devicelist有权限)
    
    mySUSE:~ # tree /data
    /data
    ├── program
    │   └── general
    │       ├── p1_devicelist -> /data/project1/iot/devicelist
    │       ├── p2_erp_version
    │       ├── program_scope
    │       └── staffing
    │           └── assignment
    ├── project1
    │   └── iot
    │       ├── bigdata
    │          └── math_lib
    │       └── devicelist
    └── project2
        └── erp
            ├── erp_vision
            └── fin
                └── fin_ar
    
    pmgr@mySUSE:~> cp -R /data/project1 /data/program/ 
        (/data/program/project1的用户和组都继承了/data/program/)
    pmgr@mySUSE:~> cp -a /data/project2 /data/program/
        (/data/program/project2的用户继承了/data/program/,但组还是保留原来的)
    

mv command

Move or rename a file or directory

  • Option -i : Asks for confirmation before moving or renaming a file. This prevents existing files with the same name from being overwritten.
  • Option -u : Only moves files that are newer than the target files of the same name.
    pmgr@mySUSE:/data/program/general> cp program_scope ./staffing/
    pmgr@mySUSE:/data/program/general> mv -i program_scope ./staffing/
    mv: overwrite './staffing/program_scope'? n
    

rm command

Delete a file or directory

  • Option -i : Asks for confirmation before deleting.
  • Option -r : (recursively) Allows full directories to be deleted.
  • Option -f : (force) By default, rm asks for confirmation if the file that should be deleted is read-only. Using this option, the files are deleted without asking for confirmation.

mkdir command

Create a new directory

  • Option -p lets you create a complete path (层级路径一次创建)
    pmgr@mySUSE:/data> mkdir -p industry/utilities
    pmgr@mySUSE:/data> tree ./industry/
    ./industry/
    └── utilities
    

rmdir command

Remove an empty directory. The directory or directories must be empty before you can delete them.

ln command

Create a link

  • Default: Hard link
  • Symbolic link with -s option
  • Syntax: ln [-s] <original> <link>

touch command

Change the access and modification times

  • Create an empty file if the given file does not exist.
  • Change the time stamp of a file.
    • Option -a : Changes only the time of the last read access (access time).
    • Option -m : Changes only the time of the last modification (modification time).
    • Option -r file : Sets the time stamp of file instead of the current time.
    • Option -t time : Instead of the current time, sets time (structure: [[CC]YY]MMDDhhmm.[ss] ([Century]Year] Month Day Hour Minute [Seconds], two digits in each))
pmgr@mySUSE:/data/industry> touch readme
pmgr@mySUSE:/data/industry> touch -a readme
pmgr@mySUSE:/data/industry> touch -m readme
pmgr@mySUSE:/data/industry> stat readme 
  File: readme
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: 3dh/61d Inode: 338         Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1003/    pmgr)   Gid: ( 1000/  admins)
Access: 2019-03-31 10:07:47.489055973 +0800
Modify: 2019-03-31 10:07:58.805109884 +0800
Change: 2019-03-31 10:07:58.805109884 +0800
 Birth: -

cat command

Concatenates files

tac command

Same as cat, but displays the file(s) in reverse

pmgr@mySUSE:/data/industry> cat readme 
line 1
line 2
line 3
pmgr@mySUSE:/data/industry> tac readme 
line 3
line 2
line 1

more command

Display file contents one page at a time

less command

Displays file contents for better navigation

head command

Displays the first 10 lines of a file.

  • To set the number of lines use the -n option
pmgr@mySUSE:/data/industry> head readme 
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10
pmgr@mySUSE:/data/industry> head -n 5 readme 
line 1
line 2
line 3
line 4
line 5

tail command

Display the last lines of a file

  • To set the number of lines use the -n option
  • To output appended data use -f option, displays a continuously updated view of the last lines of a file. To exit tail -f, press Ctrl+C.
pmgr@mySUSE:/data/industry> tail -n 6 readme 
line 10
line 11
line 12
line 13
line 14
line 15

tar command

Create, expand or list archive files

  • Use option c to create an archive
  • Use option f to specify the archive file name
  • Use option v for verbose mode
  • Use option x to extract an archive
  • Use option t to list the content of an archive
  • Use option z to (un-)compress the archive with gzip
  • Use option j to (un-)compress the archive with bzip

The /etc directory (include sub-directories) is backed up to the /backup/etc.tar file

pmgr@mySUSE:~> tar -cvf /data/backup/project1.tar /data/project1/
pmgr@mySUSE:~> tar -cvf /data/backup/project2.tar /data/project2/
pmgr@mySUSE:~> tar -cv --exclude='*.conf' -f /data/backup/project2a.tar /data/project2/

View the contents of an archive. Some .conf files are excluded in project2a.tar file.

pmgr@mySUSE:~> tar -tvf /data/backup/project2.tar
drwxr-xr-x pm2/project2      0 2019-03-31 16:47 data/project2/
drwxr-xr-x pm2/project2      0 2019-03-31 16:47 data/project2/erp/
drwxr-xr-x pm2/project2      0 2019-03-31 16:47 data/project2/erp/fin/
-rw-r--r-- pm2/project2      0 2019-03-29 16:06 data/project2/erp/fin/fin_ar
-rw-r--r-- pm2/project2      0 2019-03-31 16:47 data/project2/erp/fin/fin.conf
-rw-rw-r-- pm2/project2      0 2019-03-29 16:06 data/project2/erp/erp_vision
-rw-r--r-- pm2/project2      0 2019-03-31 16:47 data/project2/erp/erp.conf
-rw-r--r-- pm2/project2      0 2019-03-31 16:47 data/project2/project2.conf

pmgr@mySUSE:~> tar -tvf /data/backup/project2a.tar
drwxr-xr-x pm2/project2      0 2019-03-31 16:47 data/project2/
drwxr-xr-x pm2/project2      0 2019-03-31 16:47 data/project2/erp/
drwxr-xr-x pm2/project2      0 2019-03-31 16:47 data/project2/erp/fin/
-rw-r--r-- pm2/project2      0 2019-03-29 16:06 data/project2/erp/fin/fin_ar
-rw-rw-r-- pm2/project2      0 2019-03-29 16:06 data/project2/erp/erp_vision

Unpack and write all files in the archive to the current directory. Extract to another directory by using the -C option

pmgr@mySUSE:~> mkdir project1.backup 
pmgr@mySUSE:~> tar -xvf /data/backup/project1.tar -C /data/backup/project1.backup/

Incremental backup with tar command

pmgr@mySUSE:~> tar -g snapshot_program -cvf /data/backup/bkp_program_full.tar /data/program/
pmgr@mySUSE:~> tar -tvf /data/backup/bkp_program_full.tar
pmgr@mySUSE:~> touch /data/program/general/general.conf
pmgr@mySUSE:~> tar -g snapshot_program -cvf /data/backup/bkp_program_inc.tar /data/program/

pmgr@mySUSE:~> rm -rf program/
pmgr@mySUSE:~> tar -xvf /data/backup/bkp_program_inc.tar -C /data/

cpio command

Another archiving command

gzip command

Compress files using the gzip algorithm

  • Option -c : Compresses the file without modifying the original file. The result is written to the standard output (usually the screen). From there, it can be redirected to a file with “>”.
  • Option -d : Decompresses the specified file (gunzip)
  • Option -r : Compresses and decompresses files in all subdirectories.
  • Option -1 to -9, --fast, --best : Controls the compression speed: -1 means --fast and causes a quick compression but produces larger files, -9 corresponds to --best and requires more computing time but produces smaller files. The default setting is -6.

gunzip command

Expand files compressed with gzip

bzip2 command

Compress files using the bzip2 algorithm

  • Option -c :
  • Option -d : Decompresses the specified file (bunzip2).
  • Option -1 to -9 : Controls the compression speed: -1 causes a quick compression but produces larger files, -9 requires more computing time but produces smaller files. The default setting is -9.

bunzip2 command

Expand files compressed with bzip

rsync command

Copy only deltas between two directories. A key benefit of using rsync is that when copying data, rsync compares the source and the target directory and transfers only data that has changed or has been created. 仅复制两个目录之间的增量。 使用rsync的一个主要好处是,在复制数据时,rsync会比较源目录和目标目录,并仅传输已更改或已创建的数据。

  • Local or via network 本地或通过网络
  • Uses ssh as default transport 使用ssh作为默认传输
  • Can talk to rsync daemon on the remote machine 可以与远程计算机上的rsync守护程序通信

Note:

rsync must be installed on both the source and the target computer for this to work. 必须在源计算机和目标计算机上安装rsync才能使其正常工作。 As the default shell used by rsync is ssh, the -e option only needs to be used when you want to use something else than ssh. 由于rsync使用的默认shell是ssh,因此只有在想要使用除ssh以外的其他工具时才需要使用-e选项。

Options

  • Option -a : Puts rsync into the archive mode. The -a option ensures the following are preserved 保留 in the mirrored copy of the directory:
    • Symbolic links (l option)
    • Access permissions (p option)
    • Owners (o option)
    • Group membership (g option)
    • Time stamp (t option)
  • Option -x : Saves files on one file system only, which means that rsync does not follow symbolic links to other file systems. 不跨文件系统,只在一个文件系统内(don't cross filesyste* undaries)
  • Option -v : Enables the verbose mode. Use this mode to output information about the transferred files and the progress of the copying process. 输出传输过程的细节信息
  • Option -z : Compresses the data during the transfer. This is especially useful for remote synchronization. 压缩方式传输
  • Option --delete : Deletes files from the mirrored directory that no longer exist in the original directory.
  • Option --exclude-from : Does not back up files listed in an exclude file.

Local backup via rsync command

pmgr@mySUSE:/data> rsync -av /data/industry/* /data/backup/industry/
sending incremental file list
created directory /data/backup/industry
readme
utilities/
sent 264 bytes  received 87 bytes  702.00 bytes/sec
total size is 111  speedup is 0.32

pmgr@mySUSE:/data/industry/utilities> touch roadmap.txt

pmgr@mySUSE:/data> rsync -av /data/industry/* /data/backup/industry/
sending incremental file list
utilities/
utilities/roadmap.txt
sent 171 bytes  received 39 bytes  420.00 bytes/sec
total size is 111  speedup is 0.53
pmgr@mySUSE:/data> rm roadmap.txt
pmgr@mySUSE:/data> rsync -av /data/industry/* --delete /data/backup/industry/
sending incremental file list
deleting utilities/roadmap.txt
utilities/
sent 102 bytes  received 41 bytes  286.00 bytes/sec
total size is 111  speedup is 0.78

dd command

Copies files block by block

  • Used to create disk or partition images
  • Most important options:
    • if=input_file
    • `of``=output_file
    • bs=block_size

You can use the dd command to convert and copy files byte-wise.

Normally dd reads from the standard input and writes the result to the standard output. But with the appropriate parameters, regular files can be addressed as well.

You can copy all kinds of Linux data with this command, including entire hard disk partitions. You can even copy an entire installed system (or just parts of it).

Copy file /etc/protocols to protocols.old. The default size for a record is 512 bytes. Below 45+1 means, 45 complete record of standard size and 1 incomplete record (less than 512 bytes

pmgr@mySUSE:/data/program> dd if=/etc/protocols of=protocols.old bs=512
45+1 records in
45+1 records out
23259 bytes (23 kB, 23 KiB) copied, 0.000595392 s, 39.1 MB/s

创建一个100M的空文件

pmgr@mySUSE:/data/program> dd if=/dev/zero of=datafile bs=100M count=2
2+0 records in
2+0 records out
209715200 bytes (210 MB, 200 MiB) copied, 2.79311 s, 75.1 MB/s

备份整个分区

#dd if=/dev/sda1 of=boot.partition

制作U盘启动盘(U盘挂载到/dev/sdb)

#dd if=/root/diskboot.img of=/dev/sdb bs=125682176

备份硬盘主引导记录

#dd if=/dev/sda of=/tmp/mbr_copy bs=512 count=1

还原硬盘主引导记录

#dd if=/disk.mbr of=/dev/hda bs=512 count=1

将内存里的数据拷贝到root目录下的mem.bin文件

# dd if=/dev/mem of=/root/mem.bin bs=1024

拷贝光盘数据到root文件夹下,并保存为cd.iso文件

# dd if=/dev/cdrom of=/root/cd.iso

利用随机的数据填充硬盘(销毁硬盘数据)

# dd if=/dev/urandom of=/dev/hda1

测试硬盘读写速度。通过两个命令输出的执行时间,可以计算出测试硬盘的读/写速度:

mySUSE:/data # dd if=/data/program/datafile bs=64k | dd of=/dev/null
3200+0 records in
3200+0 records out
209715200 bytes (210 MB, 200 MiB) copied, 0.67138 s, 312 MB/s
409600+0 records in
409600+0 records out
209715200 bytes (210 MB, 200 MiB) copied, 0.675912 s, 310 MB/s

# dd if=/dev/zero of=/data/program/datafile bs=1024 count=100

切割大文件bigfile,共98336321字节,则:

# dd if=bigfile of=smallfile1 bs=1 count=20000000
# dd if=bigfile of=smallfile2 bs=1 count=20000000 skip=20000000
# dd if=bigfile of=smallfile3 bs=1 count=20000000 skip=40000000
# dd if=bigfile of=smallfile4 bs=1 count=20000000 skip=60000000
# dd if=bigfile of=smallfile5 bs=1 count=18336321 skip=80000000

将切割文件组装

# dd if=smallfile1 of=bigfile bs=1 count=20000000
# dd if=smallfile2 of=bigfile bs=1 count=20000000 seek=20000000
# dd if=smallfile3 of=bigfile bs=1 count=20000000 seek=40000000
# dd if=smallfile4 of=bigfile bs=1 count=20000000 seek=60000000
# dd if=smallfile5 of=bigfile bs=1 count=18336321 seek=80000000

if: 要切割的大文件名
of: 切割后的子文件名
bs: 以多少字节作为一个切割记录单位
count: 是要切割的单位记录数
skip: 说明切割时的起点
seek: 明确指出开始位置

find command

Search for files or directories

Syntax: find path criterion [action]

The find command has a multitude of options, a few of which are explained here. You can use the following arguments with the command:

  • path: The section of the file system to search (the specified directory and all its subdirectories). If nothing is specified, the file system below the current directory is used.
  • criterion: The properties the file should have (see below)
  • action: Options that influence the following conditions or control the search as a whole

The most important actions are:

  • -print (default)
  • -exec command

With the -exec option, you can call up another command. This option is frequently used to link find and grep, as in the following:

找出gen开头的文件

pmgr@dcmaster:/data> find . -name gen\*
./program/general
./program/general/general.conf

找出gen开头的文件,并在文件内容中查找xen,找到后结果输出xen

pmgr@dcmaster:/data> find . -name gen\* -type f -exec grep xen {} \;
xen
xening 

The two brackets “{}” stand as placeholders for the file names which are found and passed to the grep command. The semicolon closes the -exec instruction. Because this is a special character, it is masked by placing a backslash in front of it.

-ctime [±]days

Searches for files whose last change took place no later than (no earlier than) a specified number of days ago.  
在过去n天内被修改过的文件
mgr@dcmaster:/data> find . -ctime 1
.
./program/datafile

-gid number

Searches for files with the numeric GID (Group ID) number.  (gid 是 n)

-group name

Searches for files that are owned by the group name. Instead of a name, the numeric GID is allowed. (group 名称是 name)

-name pattern

Searches for files whose names contain the given pattern. If the pattern contains meta characters or wild cards, the name must be enclosed by quotation marks. 
Otherwise thename will be interpreted by the shell and not by find.

-newer file

Searches for files that were modified more recently than file. 
比文件 file 更新的文件
pmgr@dcmaster:/data> find . -cnewer ./program/datafile
.

-size [±]size

Matches files that are above or below a certain size. The size (in blocks of 512 bytes) is given as an argument. 
The suffix “c“switches to byte and “k” to blocks of 1024bytes. 
A preceding “+” stands for all larger files and a “-” for all smaller files. (文件大小 是 n
    • b 代表 512 位元组的区块
    • c 表示字元数
    • k 表示 kilo bytes
    • w 是二个位元组
pmgr@dcmaster:/data> find . -size 20k
./backup/project2.tar

-type file_type

Searches for a file type. A file type can be one of the following: 
    * c : 文件类型是 c 的文件。
    * d: 目录
    * c: 字型装置文件
    * b: 区块装置文件
    * p: 具名贮列
    * f: 一般文件
    * l: 符号连结
    * s: socket

-uid number

Searches for files with the numeric UID (User ID) number.

-user name

Searches for files, which are owned by user name. Instead of a name, the numeric UID is allowed.

常用参数

  • mount, -xdev : 只检查和指定目录在同一个文件系统下的文件,避免列出其它文件系统中的文件
  • amin n : 在过去 n 分钟内被读取过
  • anewer file : 比文件 file 更晚被读取过的文件
  • atime n : 在过去n天内被读取过的文件
    pmgr@dcmaster:/data> find . -atime 1
    ./program/datafile
    
  • cmin n : 在过去 n 分钟内被修改过
    pmgr@dcmaster:/data> find . -cmin 20
    
  • empty : 空的文件
  • ipath p, -path p : 路径名称符合 p 的文件,ipath 会忽略大小写
  • name name, -iname name : 文件名称符合 name 的文件。iname 会忽略大小写
  • pid n : process id 是 n 的文件

查找当前目录及子目录中所有文件长度为0的普通文件,并列出它们的完整路径

pmgr@dcmaster:/data> find . -type f -size 0 -exec ls -l {} \;

查找前目录中文件属主具有读、写权限,并且文件所属组的用户和其他用户具有读权限的文件

pmgr@dcmaster:/data> find . -type f -perm 644 -exec ls -l {} \;

查找/var/log目录中更改时间在17日以前的普通文件,并在删除之前询问它们

pmgr@dcmaster:/data> find /var/log -type f -mtime +17 -ok rm {} \;

将目前目录及其子目录下所有最近 1 天内更新过的文件列出

mgr@dcmaster:/data> find . -ctime 1

将目前目录其其下子目录中所有一般文件列出

pmgr@dcmaster:/data> find . -type f

将目前目录及其子目录下所有延伸档名是conf 的文件列出来

pmgr@dcmaster:/data> find . -name "*.conf"

which command

Searches all paths listed in the variable $PATH and returns the full path of the command

The which command searches all paths listed in the variable $PATH for the specified command and returns the full path of the command. In the variable \(PATH, the most important directoriesare listed where the shell looks for executable files. which命令搜索变量\)PATH中列出的所有路径以获取指定命令,并返回命令的完整路径。

The which command is especially useful if several versions of a command exist in different directories and you want to know which version is executed when entered without specifying apath. 如果命令的多个版本存在于不同的目录中,并且您想知道在输入时执行了哪个版本而未指定路径,那么which命令特别有用。

NOTE: To see the content of a variable, use the echo command

Options Description
-n<文件名长度> 指定文件名长度,指定的长度必须大于或等于所有文件中最长的文件名。
-p<文件名长度> 与-n参数相同,但此处的<文件名长度>包括了文件的路径。
-w 指定输出时栏位的宽度。
-V 显示版本信息
# which grep
/usr/bin/grep

# which -V grep
GNU which v2.21, Copyright (C) 1999 - 2015 Carlo Wood.
GNU which comes with ABSOLUTELY NO WARRANTY;
This program is free software; your freedom to use, change
and distribute this program is protected by the GPL.

whereis command

The whereis command returns the binaries (option -b), manual pages (option -m), and the source code (option -s) of the specified command. If no option is used, all this information is returned, provided the information is available. This command is faster than find, but it is less thorough.

Attempts to locate the desired program in the standard Linux places, and in the places specified by $PATH and $MANPATH. 尝试在标准Linux位置和指定位置(\(PATH和\)MANPATH)找到所需的程序

Options Description
-b 只查找二进制文件。
-B<目录> 只在设置的目录下查找二进制文件。
-f 不显示文件名前的路径名称。
-m 只查找说明文件。
-M<目录> 只在设置的目录下查找说明文件。
-s 只查找原始代码文件。
-S<目录> 只在设置的目录下查找原始代码文件。
-u 查找不包含指定类型的文件。

以下输出信息从左至右分别为查询的程序名、bash路径、bash的man手册页路径。

# whereis grep
grep: /usr/bin/grep /bin/grep /usr/share/man/man1/grep.1.gz /usr/share/info/grep.info.gz

显示bash 命令的二进制程序

# whereis -b grep
grep: /usr/bin/grep /bin/grep

显示bash 命令的帮助文件

# whereis -m grep
grep: /usr/share/man/man1/grep.1.gz /usr/share/info/grep.info.gz

type command

The type command shows what kind of command is executed when you enter it: 命令的类型

  • a shell built-in command (an essential command that is hard coded in the shell), for example type or cd
  • an external command (called by the shell)
  • an alias, for example ls. An alias defines shortcuts and synonyms for commonly used shell commands.
  • a function
  • The -a option delivers all instances of a command bearing this name in the file system.

NOTE: If you want to have more information about a file format, you can use the file command.

不适用于普通文件

dcmaster:/data/shell # type pwd.txt
-bash: type: pwd.txt: not found

不适用于自定义可执行脚本

dcmaster:/data/shell # type math.sh
-bash: type: math.sh: not found

系统命令

dcmaster:/data/shell # type rsync 
rsync is /usr/bin/rsync

别名

dcmaster:/data/shell # type l  
l is aliased to `ls -alF'

file command

file命令用于辨识文件类型

  • -b 列出辨识结果时,不显示文件名称。
  • -c 详细显示指令执行过程,便于排错或分析程序执行的情形。
  • -f <名称文件> 指定名称文件,其内容有一个或多个文件名称时,让file依序辨识这些文件,格式为每列一个文件名称。
  • -L 直接显示符号连接所指向的文件的类别。
  • -m<魔法数字文件> 指定魔法数字文件。
  • -v 显示版本信息。
  • -z 尝试去解读压缩文件的内容。
dcmaster:/data/linktype # l
-rw-r--r-- 3 root root  44 May  3 09:50 file
-rw-r--r-- 3 root root  44 May  3 09:50 hardlinkfile1
-rw-r--r-- 3 root root  44 May  3 09:50 hardlinkfile2
lrwxrwxrwx 1 root root   4 Mar 28 15:21 symlinkfile1 -> file
lrwxrwxrwx 1 root root  12 Mar 28 15:49 symlinkfile1-1 -> symlinkfile1
lrwxrwxrwx 1 root root   4 Mar 28 15:23 symlinkfile2 -> file

dcmaster:/data/linktype # file hardlinkfile1
hardlinkfile1: ASCII text

dcmaster:/data/linktype # file -i hardlinkfile1
hardlinkfile1: text/plain; charset=us-ascii

dcmaster:/data/linktype # file /data/linktype/
/data/linktype/: directory

dcmaster:/data/linktype # file -L /data/linktype/
/data/linktype/: directory

dcmaster:/data/linktype # file -i /data/linktype/
/data/linktype/: inode/directory; charset=binary

dcmaster:/data/linktype # file symlinkfile1
symlinkfile1: symbolic link to file

dcmaster:/data/linktype # file -i symlinkfile1
symlinkfile1: inode/symlink; charset=binary

grep command

You can specify search patterns in the form of regular expressions, although the basic grep command is limited in this regard. To search for more complex patterns, use the egrep command (or grep -E) instead, which accepts extended regular expressions.

To avoid having special characters in search patterns interpreted by the shell, enclose the pattern in quotation marks.

Syntax: grep [options] search_pattern filename * egrep = grep -E * rgrep

参数

  • -a--text : 不要忽略二进制的数据。
  • -A<显示行数>--after-context=<显示行数> : 除了显示符合范本样式的那一列之外,并显示该行之后的内容。
  • -b--byte-offset : 在显示符合样式的那一行之前,标示出该行第一个字符的编号。
  • -B<显示行数>--before-context=<显示行数> : 除了显示符合样式的那一行之外,并显示该行之前的内容。
  • -c--count : 计算符合样式的列数。
  • -C<显示行数>--context=<显示行数>-<显示行数> : 除了显示符合样式的那一行之外,并显示该行之前后的内容。
  • -d <动作>--directories=<动作> : 当指定要查找的是目录而非文件时,必须使用这项参数,否则grep指令将回报信息并停止动作。
  • -e<范本样式>--regexp=<范本样式> : 指定字符串做为查找文件内容的样式。
  • -E--extended-regexp : 将样式为延伸的普通表示法来使用。
  • -f<规则文件>--file=<规则文件> : 指定规则文件,其内容含有一个或多个规则样式,让grep查找符合规则条件的文件内容,格式为每行一个规则样式。
  • -F--fixed-regexp : 将样式视为固定字符串的列表。
  • -G--basic-regexp : 将样式视为普通的表示法来使用。
  • -h--no-filename : 在显示符合样式的那一行之前,不标示该行所属的文件名称。
  • -H--with-filename : 在显示符合样式的那一行之前,表示该行所属的文件名称。
  • -i--ignore-case : 忽略字符大小写的差别。
  • -l--file-with-matches : 列出文件内容符合指定的样式的文件名称。
  • -L--files-without-match : 列出文件内容不符合指定的样式的文件名称。
  • -n--line-number : 在显示符合样式的那一行之前,标示出该行的列数编号。
  • -o--only-matching : 只显示匹配PATTERN 部分。
  • -q--quiet或--silent : 不显示任何信息。
  • -r--recursive : 此参数的效果和指定"-d recurse"参数相同。
  • -s--no-messages : 不显示错误信息。
  • -v--revert-match : 显示不包含匹配文本的所有行。
  • -V--version : 显示版本信息。
  • -w--word-regexp : 只显示全字符合的列。
  • -x --line-regexp : 只显示全列符合的列。
  • -y : 此参数的效果和指定"-i"参数相同。

在当前目录中,查找后缀有conf字样的文件中包含xen字符串的文件,并打印出该字符串的行。

pmgr@dcmaster:/data/program/general> grep xen *.conf
xen
xening

查找前缀有gen的文件包含xen字符串的文件

pmgr@dcmaster:/data/program/general> grep xen gen*
xen
xening

以递归的方式查找符合条件的文件。查找指定目录/data/program/及其子目录(如果存在子目录的话)下所有文件中包含字符串xen的文件,并打印出该字符串所在行的内容

pmgr@dcmaster:/data> grep -r xen /data/program/
./program/general/general.conf:xen
./program/general/general.conf:xening

反向查找。前面各个例子是查找并打印出符合条件的行,通过 -v 参数可以打印出不符合条件行的内容。查找文件名中包含xen的文件中不包含xen的行

pmgr@dcmaster:/data/program/general> grep -v xen gen*
Linux
Test
test

查找当前目录下包含字符串“Linux”的文件

pmgr@dcmaster:/data/program/general> grep Linux *
general.conf:Linux
grep: staffing: Is a directory
pmgr@dcmaster:/data/program/general> egrep Linux *
general.conf:Linux
grep: staffing: Is a directory