第1章 文件类基础命令
Posted dnote
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第1章 文件类基础命令相关的知识,希望对你有一定的参考价值。
本文只会阐述常用内容,具体的请查看man手册。
1.1 路径符号说明
在Linux中路径分为绝对路径和相对路径,绝对路径顾名思义是从根(/)开始的,而相对路径则是相对于某个位置开始(即不从根开始),因此就有了一些路径符号。下面说明一下路径符号。
路径展开符号:
. :(一个点)表示当前目录
.. : (两个点)表示上一层目录
- :(一个短横线)表示上一次使用的目录(常用"cd -"切换到上一次目录)
~ :(波浪号)表示用户的家目录
上述中的符号可以组合使用,前提是你确定工作目录(pwd输出工作目录)的位置。
另外说明一下/dir/和/dir,一般都表示dir目录和dir目录中的文件。但在某些地方会严格区分是否有随斜线,此时有斜线的表示此目录中的文件,没斜线的表示该目录本身和目录中的文件。
命令basename和命令dirname分别是获取文件名和目录名。同时注意这两个命令不完善,它们并不会检查文件和目录是否存在。
[[email protected] ~]# basename /etc/passwd
passwd
[[email protected] ~]# basename /etc/
etc
[[email protected] ~]# dirname /etc/passwd
/etc
[[email protected] tmp]# dirname /a/b/ #注意获取的是上层目录
/a
[[email protected] tmp]# dirname /dfhja/dfak #目录并不存在也会获取
/dfhja
1.2 查看目录内容(ls和tree)
ls命令会以制表符分隔列出目录中的内容,tree命令会按照树状结构递归列出目录和子目录中的内容。ls命令与tree命令不同的是需要使用"-R"选项才会递归显示内容。
ls命令
NAME
ls - list directory contents
SYNOPSIS
ls [OPTION]... [FILE]...
DESCRIPTION
List information about the FILEs (the current directory by default). Sort entries alphabetically if none of
-cftuvSUX nor --sort is specified.
Mandatory arguments to long options are mandatory for short options too.
-a, --all
do not ignore entries starting with .
-l use a long listing format
-h, --human-readable
with -l, print sizes in human readable format (e.g., 1K 234M 2G)
-d, --directory
list directories themselves, not their contents
-r, --reverse
reverse order while sorting
-R, --recursive
list subdirectories recursively
-t sort by modification time, newest first
以列出的都是一些常用的。
注意,ls以-h显示文件大小时,一般显示的都是不带B的单位,如K/M/G,它们的转换比例是1024,如果显示的但是是带了B的,如KB/MB/GB,则它们的转换比例为1000而非1024,一般很少显示带B的大小。
ls缺陷是不能显示出文件的全路径,好在使用find命令可以获取到。
比较实用的选项例子
[[email protected] tmp]# ls -ltr #按照修改时间显示
total 0
-rw-r--r--. 1 root root 0 Jul 25 16:22 practice_1.txt
-rw-r--r--. 1 root root 0 Jul 25 17:51 2.txt
-rw-r--r--. 1 root root 0 Jul 25 17:51 1.txt
drwxr-xr-x. 3 root root 15 Aug 3 16:46 a
以下是使用ls -l显示文件长格式的属性。
[[email protected] tmp]# ll /tmp
total 0
-rw-r--r--. 1 root root 0 Jul 25 17:51 1.txt
-rw-r--r--. 1 root root 0 Jul 25 17:51 2.txt
drwxr-xr-x. 3 root root 15 Aug 3 16:46 a
-rw-r--r--. 1 root root 0 Jul 25 16:22 practice_1.txt
此处缺少长格式属性说明和文件类型【图】
通过stat命令可以获取到指定文件的元数据。
文件的时间戳(atime/ctime/mtime)
文件的时间属性有三种:atime/ctime/mtime。
atime是access time,即上一次的访问时间;
mtime是modify time,是文件的修改时间;
ctime是change time,也是文件的修改时间。
mtime只有在修改文件内容才会改变,准确来说是修改了它的data block部分;而ctime是修改文件属性时改变的,确切的说是修改了它的元数据部分,例如重命名文件,修改文件所有者,移动文件(移动文件没有改变datablock,只是改变了其inode指针,或文件名)等.
当然,修改文件内容也一定会改变ctime(修改文件内容至少已经修改了inode记录上的mtime,这也是元数据),也就是说mtime的改变一定会引起ctime的改变。
对目录而言,考虑目录文件的data block,可知在目录中创建、删除文件以及目录内其他任意文件操作都会改变mtime,因为目录里的任何东西都是目录中的内容;而目录的ctime,除了目录的mtime引起ctime改变之外,对目录本身的元数据修改也会改变ctime。
总结下:
(1).atime只在文件被打开访问时才改变,若不是打开文件编辑内容(如重定向内容到文件中),则ctime和mtime的改变不会引起atime的改变;
(2).mtime的改变一定引起ctime的改变,而atime的改变不总是会影响ctime(如touch atime时会改变ctime,但cat文件时不会改变ctime),atime也是文件的元数据信息,它改变了为什么不会改变ctime?(待解答)
tree命令
tree命令可能不存在,可以通过yum install tree -y进行安装。
NAME
tree - list contents of directories in a tree-like format.
SYNOPSIS
tree [-L level [-R]] [-o filename] [-P pattern]
LISTING OPTIONS
-d List directories only.
-L level
Max display depth of the directory tree.
-P pattern
List only those files that match the wild-card pattern. Note: you must use the -a option to also con‐
sider those files beginning with a dot `.‘ for matching. Valid wildcard operators are `*‘ (any zero or
more characters), `?‘ (any single character), `[...]‘ (any single character listed between brackets
(optional - (dash) for character range may be used: ex: [A-Z]), and `[^...]‘ (any single character not
listed in brackets) and `|‘ separates alternate patterns.
-o filename
Send output to filename.
1.3 文件的创建与删除
文件的创建
mkdir命令
NAME
mkdir - make directories
SYNOPSIS
mkdir [OPTION]... DIRECTORY...
DESCRIPTION
Create the DIRECTORY(ies), if they do not already exist.
Mandatory arguments to long options are mandatory for short options too.
-m, --mode=MODE
set file mode (as in chmod), not a=rwx - umask
-p, --parents
no error if existing, make parent directories as needed
-v, --verbose
print a message for each created directory
没什么好说明的,看实际例子
[[email protected] tmp]# mkdir test1 #创建一个test1目录
[[email protected] tmp]# mkdir -m 711 test2 #创建目录test2时赋予权限711
[[email protected] tmp]# mkdir -p a/b/c #递归创建多层目录
[[email protected] tmp]# ls
a test1 test2
[[email protected] tmp]# tree /tmp -p
/tmp
├── [drwxr-xr-x] a
│ └── [drwxr-xr-x] b
│ └── [drwxr-xr-x] c
├── [drwxr-xr-x] test1
└── [drwx--x--x] test2
5 directories, 0 files
touch命令
NAME
touch - change file timestamps
SYNOPSIS
touch [OPTION]... FILE...
DESCRIPTION
Update the access and modification times of each FILE to the current time.
A FILE argument that does not exist is created empty, unless -c or -h is supplied.
A FILE argument string of - is handled specially and causes touch to change the times of the file associated
with standard output.
Mandatory arguments to long options are mandatory for short options too.
-a change only the access time
-c, --no-create
do not create any files #如果文件不存在,则不予创建
-d, --date=STRING
parse STRING and use it instead of current time
-m change only the modification time
-t STAMP
use [[CC]YY]MMDDhhmm[.ss] instead of current time
实践操作:
[[email protected] tmp]# touch 1.txt #创建1.txt文件
[[email protected] tmp]# touch {2..10}.txt #创建2-10.txt多个文件
[[email protected] tmp]# touch {a,b}_{c,d} 创建a_c、a_d、b_c、b_d四个文件
[[email protected] tmp]# ll
total 0
-rw-r--r--. 1 root root 0 Aug 3 19:33 10.txt
-rw-r--r--. 1 root root 0 Aug 3 19:33 1.txt
-rw-r--r--. 1 root root 0 Aug 3 19:33 2.txt
-rw-r--r--. 1 root root 0 Aug 3 19:33 3.txt
-rw-r--r--. 1 root root 0 Aug 3 19:33 4.txt
-rw-r--r--. 1 root root 0 Aug 3 19:33 5.txt
-rw-r--r--. 1 root root 0 Aug 3 19:33 6.txt
-rw-r--r--. 1 root root 0 Aug 3 19:33 7.txt
-rw-r--r--. 1 root root 0 Aug 3 19:33 8.txt
-rw-r--r--. 1 root root 0 Aug 3 19:33 9.txt
-rw-r--r--. 1 root root 0 Aug 3 19:36 a_c
-rw-r--r--. 1 root root 0 Aug 3 19:36 a_d
-rw-r--r--. 1 root root 0 Aug 3 19:36 b_c
-rw-r--r--. 1 root root 0 Aug 3 19:36 b_d
touch主要是修改文件的时间戳信息,当touch的文件不存在时就自动创建该文件。可以使用 touch –c 来取消创建动作。
touch可以更改最近一次访问时间(atime),最近一次修改时间(mtime),文件属性修改时间(ctime),这些时间可以通过命令stat file来查看。其中ctime是文件属性上的更改,即元数据的更改,比如修改权限。
touch -a修改atime,-m修改mtime,没有修改ctime的选项。因为使用touch改变atime或mtime,同时也都会改变ctime,虽说atime并不总是会影响ctime(如cat文件时)。
-t选项表示使用"[[CC]YY]MMDDhhmm[.ss]"格式的时间替代当前时间。
[[email protected] tmp]# touch -a -t 201708082008 a.txt # 将a.txt文件的atime修改为2017年08月08日20点12分
[[email protected] tmp]# stat a.txt
File: ‘a.txt’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 16805365 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2017-08-08 20:08:00.000000000 +0800
Modify: 2018-08-03 19:43:53.046051202 +0800
Change: 2018-08-03 19:46:03.629041857 +0800
Birth: -
-d选项表示使用指定的字符串描述时间格式来替代当前时间,如"3 days ago","next Sunday"等很多种格式。
文件删除
rm命令
SYNOPSIS
rm [OPTION]... FILE...
DESCRIPTION
This manual page documents the GNU version of rm. rm removes each specified file. By default, it does not
remove directories.
If the -I or --interactive=once option is given, and there are more than three files or the -r, -R, or --recur‐
sive are given, then rm prompts the user for whether to proceed with the entire operation. If the response is
not affirmative, the entire command is aborted.
Otherwise, if a file is unwritable, standard input is a terminal, and the -f or --force option is not given, or
the -i or --interactive=always option is given, rm prompts the user for whether to remove the file. If the
response is not affirmative, the file is skipped.
OPTIONS
Remove (unlink) the FILE(s).
-f, --force
ignore nonexistent files and arguments, never prompt
-i prompt before every removal
-r, -R, --recursive
remove directories and their contents recursively
-d, --dir #删除空目录也可以使用rmdir命令
remove empty directories
-v, --verbose
explain what is being done
强制删除一个文件test
[[email protected] tmp]# rm -rf test
注意: 在删除前,一定一定要确定文件是否真的要删除。最好使用rm -i(默认已经在~/.bashrc中定义了该别名),除非在脚本中,否则不要轻易使用-f选项。已经有非常多的人不小心rm -rf *和rm -rf /NNNNN了。例如想删除"rm –rf /abc",结果习惯性的多敲了一个空格"rm –rf /abc ",完了。
给个小小的建议,就是可以结合find命令查找,再进行删除。
1.4 文件类型查看
file命令
NAME
file — determine file type
实践操作:
[[email protected] tmp]# file /etc/aliases.db
/etc/aliases.db: Berkeley DB (Hash, version 9, native byte-order) #数据文件
[[email protected] tmp]# file ~/.bashrc
/root/.bashrc: ASCII text #ASCII文件
[[email protected] tmp]# file /bin/file
/bin/file: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=1d46ce62785380d7257659f0b7d7987b98c0af03, stripped #二进制文件
除了查看文件类型外,file有一个"-s"选项,可以查看设备的文件系统类型。像有些分区工具如parted在分区时是可以指定文件系统的(虽然不建议这么做,CentOS 7的parted版本中已经取消了该功能),但在分区后格式化前,一般是比较难查看该分区的文件系统类型的,但使用file可以查看到。
[[email protected] ~]# file -s /dev/sda1
/dev/sda1: Linux rev 1.0 ext4 filesystem data (needs journal recovery) (extents) (huge files)
[[email protected] ~]# file -s /dev/sda
/dev/sda: x86 boot sector; GRand Unified Bootloader, stage1 version 0x3, boot drive 0x80, 1st sector stage2 0x7f86, GRUB version 0.94; partition 1: ID=0x83,
active, starthead 32, startsector 2048, 512000 sectors; partition 2: ID=0x83, starthead 254, startsector 514048, 37332992 sectors; partition 3: ID=0x82,
starthead 254, startsector 37847040, 4096000 sectors, code offset 0x48
1.5 文件复制与移动
cp命令
NAME
cp - copy files and directories
SYNOPSIS
cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...
DESCRIPTION
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
Mandatory arguments to long options are mandatory for short options too.
-a, --archive
same as -dR --preserve=all
-f, --force
if an existing destination file cannot be opened, remove it and try again (this option is ignored when
the -n option is also used)
-i, --interactive
prompt before overwrite (overrides a previous -n option)
-p same as --preserve=mode,ownership,timestamps
-R, -r, --recursive
copy directories recursively
一般使用"cp -a"即可,对于目录使用"-r"选项递归复制目录及目录内部的所有内容。
通配符
- *:表示任意长度任意字符
- ?:表示 任意单个字符
- []:表示匹配指定范围内的任意单个字符
- [^]:表示匹配指定范围外的任意单个字符
注意,bash内置命令在进行通配符匹配文件的时候,"*"、"?"、"[]"默认是无法匹配到以"."开头的文件的(以"."的文件为隐藏文件),所以"*"不会匹配隐藏文件。要通配隐藏文件,使用"."代替上述几种通配元字符即可,它能匹配除了"."和".."这两个特殊目录外的所有文件。
例如,复制/etc/skel目录下所有文件包括隐藏文件到/tmp目录下。
cp -a /etc/skel/. /tmp
如果有重复文件,则即使加上-f选项,也一样会交互式询问。解决方法可以是使用"yes"这个工具,它会不断的生成y字母直到进程被杀掉,当然也可以自行指定要生成的字符串。
yes | cp -a /etc/skel/. /tmp
yes | cp -a /etc/skel/. /tmp
删除/tmp下所有文件包括隐藏文件。
rm -rf /tmp/{*,.*}
还需要说明的是,对于非bash内置命令,通配方法可能不一样,有些命令有自己的通配标准,如新版本的find中的"*"可以匹配点开头的文件,但如果它采用的仍然是bash的通配方式,则和上面的匹配方法一样。
如果想要让"*"匹配"."开头的文件,可以打开点号的glob扩展模式。
shopt -s dotglob
scp命令
NAME
scp — secure copy (remote file copy program)
SYNOPSIS
scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port]
[-S program] [[[email protected]]host1:]file1 ... [[[email protected]]host2:]file2
DESCRIPTION
scp copies files between hosts on a network. It uses ssh(1) for data transfer, and uses the same authentication
and provides the same security as ssh(1). scp will ask for passwords or passphrases if they are needed for
authentication.
File names may contain a user and host specification to indicate that the file is to be copied to/from that host.
Local file names can be made explicit using absolute or relative pathnames to avoid scp treating file names con‐
taining ‘:’ as host specifiers. Copies between two remote hosts are also permitted.
The options are as follows:
-1 Forces scp to use protocol 1.
-2 Forces scp to use protocol 2.
-C Compression enable. Passes the -C flag to ssh(1) to enable compression.
-l limit
Limits the used bandwidth, specified in Kbit/s.
-P port
Specifies the port to connect to on the remote host. Note that this option is written with a capital ‘P’,
because -p is already reserved for preserving the times and modes of the file.
-p Preserves modification times, access times, and modes from the original file.
-r Recursively copy entire directories. Note that scp follows symbolic links encountered in the tree traver‐
sal.
-v Verbose mode. Causes scp and ssh(1) to print debugging messages about their progress. This is helpful in
debugging connection, authentication, and configuration problems.
实践操作:
1.把本地文件/etc/hosts拷贝到远程服务器192.168.100.146上的/tmp,连接时使用远程的root用户:
scp /etc/hosts [email protected]:/tmp/
2.目标主机不写路径时,表示拷贝到对方的家目录下:
scp /etc/hosts [email protected]
3.把远程文件/etc/fstab拷贝到本机:
scp [email protected]:/etc/fatab # 不接本地目录表示拷贝到当前目录
scp [email protected]:/etc/fatab /tmp # 拷贝到本地/tmp目录下
4.拷贝远程机器的/home/目录到本地/tmp目录下。
scp -r [email protected]:/home/ /tmp
5.从远程主机192.168.100.60拷贝文件到另一台远程主机192.168.100.62上。
scp [email protected]:/tmp/copy.txt [email protected]:/tmp
mv命令
NAME
mv - move (rename) files
SYNOPSIS
mv [OPTION]... [-T] SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...
DESCRIPTION
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
Mandatory arguments to long options are mandatory for short options too.
--backup[=CONTROL]
make a backup of each existing destination file
-b like --backup but does not accept an argument
-f, --force
do not prompt before overwriting
-i, --interactive
prompt before overwrite
-n, --no-clobber
do not overwrite an existing file
-u, --update
move only when the SOURCE file is newer than the destination file or when the
destination file is missing
mv命令能够移动文件或者重命名文件,默认已经递归操作,无需"-r"选项。
1.6 查看文件内容
cat命令
连接文件并在标准输出上打印。
AME
cat - concatenate files and print on the standard output
SYNOPSIS
cat [OPTION]... [FILE]...
DESCRIPTION
Concatenate FILE(s), or standard input, to standard output.
-A, --show-all
equivalent to -vET
-b, --number-nonblank
number nonempty output lines, overrides -n
-E, --show-ends
display $ at end of each line
-n, --number
number all output lines
-s, --squeeze-blank
suppress repeated empty output lines
-T, --show-tabs
display TAB characters as ^I
cat主要有三大功能:
1.一次显示整个文件。
shell> cat filename
2.从键盘创建一个文件。
shell> cat > filename
只能创建新文件,不能编辑已有文件.
3.将几个文件合并为一个文件。
shell> cat file1 file2 > file
实践操作:
[[email protected] tmp]# cat >file.txt<<EOF #一个">"符号表示创建文件
> this is a test file
> EOF
[[email protected] tmp]# cat file.txt
this is a test file
[[email protected] tmp]# cat >>file.txt<<EOF #两个">"表示追加
> add one line to this text
> EOF
[[email protected] tmp]# cat file.txt
this is a test file
add one line to this text
EOF是“end of file”,表示文本结束符。EOF在这里没有特殊的含义,你可以使用FOE或OOO等,注意大小写敏感,前后一致。
tac命令
连接文件并反向输出,和cat相反。
NAME
tac - concatenate and print files in reverse
SYNOPSIS
tac [OPTION]... [FILE]...
DESCRIPTION
Write each FILE to standard output, last line first. With no FILE, or when FILE is -, read standard input.
实践操作:
[[email protected] ~]# echo -e ‘1
2
3‘ | tac
3
2
1
[[email protected] ~]# echo -e ‘1
2
3‘ | cat
1
2
3
head和tail
head
输出文件的前部分
NAME
head - output the first part of files
SYNOPSIS
head [OPTION]... [FILE]...
DESCRIPTION
Print the first 10 lines of each FILE to standard output. With more than one FILE,
precede each with a header giving the file name. With no FILE, or when FILE is -,
read standard input.
Mandatory arguments to long options are mandatory for short options too.
-n, --lines=[-]K
print the first K lines instead of the first 10; with the leading ‘-’, print
all but the last K lines of each file
-q, --quiet, --silent
never print headers giving file names
-v, --verbose
always print headers giving file names
tail
输出文件的后部分。
NAME
tail - output the last part of files
SYNOPSIS
tail [OPTION]... [FILE]...
DESCRIPTION
Print the last 10 lines of each FILE to standard output. With more than one FILE,
precede each with a header giving the file name. With no FILE, or when FILE is -,
read standard input.
Mandatory arguments to long options are mandatory for short options too.
-f, --follow[={name|descriptor}]
output appended data as the file grows; -f, --follow, and --follow=descriptor
are equivalent
-n, --lines=K
output the last K lines, instead of the last 10; or use -n +K to output lines
starting with the Kth
--pid=PID
with -f, terminate after process ID, PID dies
-v, --verbose
always output headers giving file names
nl
以行号显示文件内容。
NAME
nl - number lines of files
SYNOPSIS
nl [OPTION]... [FILE]...
DESCRIPTION
Write each FILE to standard output, with line numbers added. With no FILE, or when FILE is -, read standard
input.
Mandatory arguments to long options are mandatory for short options too.
-b, --body-numbering=STYLE
use STYLE for numbering body lines
a number all lines
默认"-b t",表示空行不显示行号,等价于cat -b。
常用"-b a",表示不论是否空行都显示行号,等价于cat -n;
实践操作:
[[email protected] tmp]# nl /etc/issue
1 S
2 Kernel
on an m
[[email protected] tmp]# nl -b a /etc/issue
1 S
2 Kernel
on an m
3
more和less
分页浏览文件的过滤器
NAME
more - file perusal filter for crt viewing
SYNOPSIS
more [-dlfpcsu] [-num] [+/pattern] [+linenum] [file ...]
DESCRIPTION
More is a filter for paging through text one screenful at a time. This version is espe-
cially primitive. Users should realize that less(1) provides more(1) emulation and
extensive enhancements.
NAME
less - opposite of more
SYNOPSIS
less -?
less --help
less -V
less --version
less [-[+]aBcCdeEfFgGiIJKLmMnNqQrRsSuUVwWX~]
[-b space] [-h lines] [-j line] [-k keyfile]
[-{oO} logfile] [-p pattern] [-P prompt] [-t tag]
[-T tagsfile] [-x tab,...] [-y lines] [-[z] lines]
[-# shift] [+[+]cmd] [--] [filename]...
(See the OPTIONS section for alternate option syntax with long option names.)
DESCRIPTION
Less is a program similar to more (1), but which allows backward movement in the file
as well as forward movement. Also, less does not have to read the entire input file
before starting, so with large input files it starts up faster than text editors like
vi (1). Less uses termcap (or terminfo on some systems), so it can run on a variety
of terminals. There is even limited support for hardcopy terminals. (On a hardcopy
terminal, lines which should be printed at the top of the screen are prefixed with a
caret.)
Commands are based on both more and vi. Commands may be preceded by a decimal number,
called N in the descriptions below. The number is used by some commands, as indi-
cated.
按页显示文件内容。使用more时,使用/向后搜索字符串,按n或N键表示向下或向上继续搜索。使用less时,还多了一个搜索功能,使用?向前搜索字符串,同样,使用n或N键可以向上或向下继续搜索。
文件内容比较
diff命令
NAME
diff - compare files line by line
SYNOPSIS
diff [OPTION]... FILES
vimdiff命令
NAME
vimdiff - edit two, three or four versions of a file with Vim and show differences
SYNOPSIS
vimdiff [options] file1 file2 [file3 [file4]]
gvimdiff
1.7 文件的查找
which
显示(shell)命令的全路径。
NAME
which - shows the full path of (shell) commands.
SYNOPSIS
which [options] [--] programname [...]
DESCRIPTION
Which takes one or more arguments. For each of its arguments it prints to stdout the
full path of the executables that would have been executed when this argument had been
entered at the shell prompt. It does this by searching for an executable or script in
the directories listed in the environment variable PATH using the same algorithm as
bash(1).
This man page is generated from the file which.texinfo.
同时默认命令的别名也会显示出来。
实践操作:
[[email protected] ~]# which cp
alias cp=‘cp -i‘
/bin/cp
[[email protected] ~]# which which
alias which=‘alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde‘
/usr/bin/which
where
查找命令的二进制文件、源文件和man手册。
NAME
whereis - locate the binary, source, and manual page files for a command
SYNOPSIS
whereis [-bmsu] [-BMS directory... -f] filename...
DESCRIPTION
whereis locates source/binary and manuals sections for specified files. The supplied
names are first stripped of leading pathname components and any (single) trailing
extension of the form .ext, for example, .c. Prefixes of s. resulting from use of
source code control are also dealt with. whereis then attempts to locate the desired
program in a list of standard Linux places.
实践操作:
[[email protected] ~]# whereis ls
ls: /bin/ls /usr/share/man/man1p/ls.1p.gz /usr/share/man/man1/ls.1.gz
[[email protected] ~]# whereis whereis
whereis: /usr/bin/whereis /usr/share/man/man1/whereis.1.gz
whatis
在whatis数据库中搜索关键词词。
NAME
whatis - search the whatis database for complete words.
SYNOPSIS
whatis keyword ...
DESCRIPTION
whatis searches a set of database files containing short descriptions of system com-
mands for keywords and displays the result on the standard output. Only complete word
matches are displayed.
The whatis database is created using the command /usr/sbin/makewhatis.
实践操作:
[[email protected] ~]# whatis passwd
passwd (1) - update user‘s authentication tokens
passwd (5) - password file
passwd [sslpasswd] (1ssl) - compute password hashes
[[email protected] ~]# whatis shadow
shadow (3) - encrypted password file routines
shadow (5) - shadowed password file
locate
locate(locate) 命令用来查找文件或目录。 locate命令要比find -name快得多,原因在于它不搜索具体目录,而是搜索一个数据库/var/lib/mlocate/mlocate.db 。这个数据库中含有本地所有文件信息。Linux系统自动创建这个数据库,并且每天自动更新一次,因此,我们在用whereis和locate 查找文件时,有时会找到已经被删除的数据,或者刚刚建立文件,却无法查找到,原因就是因为数据库文件没有被更新。为了避免这种情况,可以在使用locate之前,先使用updatedb命令,手动更新数据库。整个locate工作其实是由四部分组成的:
1./usr/bin/updatedb 主要用来更新数据库,通过crontab自动完成的
2./usr/bin/locate 查询文件位置
3./etc/updatedb.conf updatedb的配置文件
4./var/lib/mlocate/mlocate.db 存放文件信息的文件
find
find命令内容很多,这里通过一个实践了解简单用法。
实践操作:
[[email protected] tmp]# mkdir /tmp/a
[[email protected] tmp]# touch /tmp/a/{1..5}.log
[[email protected] tmp]# find /tmp/a
/tmp/a
/tmp/a/5.log
/tmp/a/1.log
/tmp/a/4.log
/tmp/a/3.log
/tmp/a/2.log
[[email protected] tmp]# find /tmp/a -print0
/tmp/a/tmp/a/5.log/tmp/a/1.log/tmp/a/4.log/tmp/a/3.log/tmp/a/2.log[[email protected] tmp]#
[[email protected] tmp]# find /tmp/ -name "*.log"
/tmp/a/5.log
/tmp/a/1.log
/tmp/a/4.log
/tmp/a/3.log
/tmp/a/2.log
[[email protected] tmp]# find /tmp/ -name ‘*a*/*.log‘
find: warning: Unix filenames usually don‘t contain slashes (though pathnames do). That means that ‘-name `*a*/*.log‘‘ will probably evaluate to false all the time on this system. You might find the ‘-wholename‘ test more useful, or perhaps ‘-samefile‘. Alternatively, if you are using GNU grep, you could use ‘find ... -print0 | grep -FzZ `*a*/*.log‘‘.
[[email protected] tmp]# find /tmp/ -path ‘*a*/*.log‘
/tmp/a/5.log
/tmp/a/1.log
/tmp/a/4.log
/tmp/a/3.log
/tmp/a/2.log
[[email protected] test]# ls
1.sh 22.sh 2.sh 3.sh
[[email protected] test]# find -name "[1-2].sh"
./1.sh
./2.sh
[[email protected] test]# find -name "[1-22].sh"
./1.sh
./2.sh
[[email protected] test]# find -name "[1-23].sh"
./1.sh
./3.sh
./2.sh
[[email protected] test]# touch 0.sh
[[email protected] test]# find -name "[1-20].sh"
./1.sh
./2.sh
./0.sh
[[email protected] test]# find -name "[1-22-3].sh"
./1.sh
./3.sh
./2.sh
[[email protected] test]# find /tmp -type f -name "a*.sh" #根据文件类型搜索,搜索普通文件(f)
[[email protected] test]# find /tmp -type d -name "a*" #根据文件类型搜索,搜索目录(d)
[[email protected] test]# find /tmp -type f -mtime -3 -name "*.sh" #根据时间戳搜索
[[email protected] test]# find /tmp -type f -size +100k -name ‘*.sh‘ #根据文件大小搜索
[[email protected] test]# find /tmp -type f -perm -0700 -name ‘*.sh‘ #根据文件权限搜索
[[email protected] test]# find /tmp -type d -empty #搜索空目录
[[email protected] test]# find /tmp -type f -name "*.tmp" -exec rm -rf ‘{}‘ ; #搜索文件并删除
以上是关于第1章 文件类基础命令的主要内容,如果未能解决你的问题,请参考以下文章