运维必修147个命令 只要我写上来的,我都会

本文详细介绍了Linux运维中常见的147个命令,包括目录操作、文件操作、搜索文件、用户管理、网络操作等。重点讲解了ls、tree、mkdir、rmdir、cd等基础命令,以及find、cp、mv、rm、ln等高级命令的使用,旨在帮助运维人员提升工作效率。
摘要由CSDN通过智能技术生成

目录操作命令(6个)
ls tree pwd mkdir rmdir cd
其中tree在/usr/bin下面,其他在/bin下

—ls—

-a: 显示隐藏目录
-l: use a long listing format
-c: show create time and sort by ctime
-t: sort by modification time
-d */: list director
-h: human readable
-o: 类似-l,但不显示group
-g: 类似-l,但不先手onwer
-n: 类似-l,udi和gid用数字代替
-r: reverse显示
-i: 显示inode
-S: sort by file size
-1 :每行列一个文件或者目录

—tree—

-L: level, Max display depth of the derectory tree
-a : all files are printed, 不包括.,包括.bashrc

–pwd—

—mkdir— and —rmdir—

-p : parents
-v : verbose

—cd—

文件操作命令(7个)
touch cp mv rm ln find rename
只有rename在/usr/bin下面,其他都在/bin下

—touch—

注意如果文件存在,touch会把文件的stamp都改了,但内容保留
-a:更改访问时间
-m:可更改修改时间

—cp—

此命令总是有提示,是因为root/.bashrc中定义了alias。如果不想看提示,可以使用/bin/cp 或者\cp
-r: copy directories
-p: 保持属性
-v:explain what is being done

—mv— mv [option] … source dest

对文件和文件夹都实用
-f : force, do not prompt befor overwritig,如果dest已有相同名字的文件

—rm—

remove files or directories, 同样定义了alias
-f:force, ignore nonexistent files, and never prompt
-r : recursive
-v: verbose

—ln—

-s :symblic

—find—

find [pathnames] [conditions]

An extremely useful command for finding particular groups of files (numerous examples follow this description). find descends(降序) the directory tree beginning at each pathname and locates files that meet the specified conditions. The default pathname is the current directory. The most useful conditions include -name and -type (for general use), -exec and -size (for advanced use), and -mtime and -user (for administrators).

Conditions(匹配规则) may be grouped by enclosing them in ( **) (escaped parentheses), negated with !, given as alternatives by separating them with -o, or repeated (adding restrictions to the match; usually only for -name, -type, or -perm). **Note that “modification” refers to editing of a file’s contents, whereas “change” means a modification, or permission or ownership changes. In other words, -ctime is more inclusive(范围广的) than -atime or -mtime.

Conditions and actions

-amin +n| -n| n
Find files last accessed more than n (+n), less than n (-n), or exactly n minutes ago.

-anewer file 查找其存取时间较指定文件或目录的存取时间更接近现在的文件或目录。
Find files that were accessed after file was last modified. Affected by -follow when after -follow on the command line.

-atime +n| -n| n
Find files that were last accessed more than n (+n), less than n (-n), or exactly n days ago. Note that find changes the access time of directories supplied as pathnames.

-cmin +n| -n| n
Find files last changed more than n (+n), less than n (-n), or exactly n minutes ago.

-cnewer file
Find files that were changed after they were last modified. Affected by -follow when after -follow on the command line.

-ctime +n| -n| n
Find files that were changed more than n (+n), less than n (-n), or exactly n days ago. A change is anything that changes the directory entry for the file, such as a chmod.

-daystart???
Calculate times from the start of the day today, not 24 hours ago.

-depth从指定目录下最深层的子目录开始查找。???
Descend the directory tree, skipping directories and working on actual files first, and then the parent directories. Useful when files reside[ri`zaid]居住 in unwritable directories (e.g., when using find with cpio).

-empty???
Continue if file is empty. Applies to regular files and directories.

-exec command{ } \ ;
Run the Linux command, from the starting directory on each file matched by find (provided command executes successfully on that file—i.e., returns a 0 exit status). When command runs, the argument { } substitutes the current file. Follow the entire sequence with an escaped转义 semicolon分号 (\;). In some shells, the braces 大括号may need to be escaped as well.

-false
Return false value for each file encountered.

-follow排除软链接
Follow symbolic links and track the directories visited (don’t use with -type l).

-fstype type
Match files only on type filesystems. Acceptable types include minix, ext, ext2, xia, msdos, umsdos, vfat, proc, nfs, iso9660, hpfs, sysv, smb, and ncpfs.

-gid num
Find files with numeric group ID of num.

-group gname
Find files belonging to group gname. gname can be a group name or a group ID number.

-ilname pattern
A case-insensitive忽略大小写 version of -lname.

-iname pattern
A case-insensitive version of -name.

-inum n
Find files whose inode number is n.

-ipath pattern
A case-insensitive version of -path.

-iregex pattern
A case-insensitive version of -regex.

-links n
Find files having n links.

-lname pattern
Search for files that are symbolic links, pointing to files named pattern. pattern can include shell metacharacters and does not treat / or . specially. The match is case-insensitive.

-maxdepth num
Do not descend more than num levels of directories.

-mindepth num
Begin applying tests and actions only at levels deeper than num levels.

-mmin +n| -n| n
Find files last modified more than n (+n), less than n (-n), or exactly n minutes ago.

-mount, -xdev
Search only for files that reside on the same filesystem as pathname.

-mtime +n| -n| n
Find files that were last modified more than n (+n), less than n (-n), or exactly n days ago. A modification is a change to a file’s data.

-name pattern
Find files whose names match pattern. Filename metacharacters may be used but should be escaped or quoted.

-newer file
Find files that were modified more recently than file; similar to -mtime. Affected by -follow only if it occurs after -follow on the command line.

-nogroup
The file’s group ID does not correspond to any group.

-noleaf
Normally, find assumes that each directory has at least two hard links that should be ignored (a hard link for its name and one for “.”–i.e., two fewer “real” directories than its hard link count indicates). -noleaf turns off this assumption, a useful practice when find runs on non-Unix-style filesystems. This forces find to examine all entries, assuming that some might prove to be directories into which it must descend (a time-waster on Unix).

-nouser
The file’s user ID does not correspond to any user.

-ok command { }\;
Same as -exec, but prompts user to respond with y before command is executed.

-path pattern
Find files whose names match pattern. Expect full pathnames relative to the starting pathname (i.e., do not treat / or . specially).

-perm nnn
Find files whose permission flags (e.g., rwx) match octal number nnn exactly (e.g., 664 matches -rw-rw-r–). Use a minus sign before nnn to make a “wildcard” match of any unspecified octal digit (e.g., -perm -600 matches -rw-*, where can be any mode).

-print
Print the matching files and directories, using their full pathnames. Return true. This is the default behavior.

-regex pattern
Like -path, but uses grep-style regular expressions instead of the shell-like globbing used in -name and -path.

-size n[c]
Find files containing n blocks, or if c is specified, n characters long.

-type c
Find files whose type is c. c can be b (block special file), c (character special file), d (directory), p (fifo or named pipe), l (symbolic link), s (socket), or f (plain file).

-user user
Find files belonging to user (name or ID).

Examples

List all files (and subdirectories) in your home directory:

find $HOME -print

List all files named chapter1 in the /work directory:

find /work -name chapter1

List all files beginning with memo owned by ann:

find /work -name ‘memo*’ -user ann -print

Search the filesystem (begin at root) for manpage directories:

find / -type d -name ‘man*’ -print

Search the current directory, look for filenames that don’t begin with a capital letter, and send them to the printer:

find . ! -name ‘[A-Z] *-exec lpr { }\;

Find and compress files whose names don’t end with .gz:

gzip find . \! -name '*.gz' -print

Remove all empty files on the system (prompting first):

find / -size 0 -*ok rm { } *;

Search the system for files that were modified within the last two days (good candidates for backing up):

find / -mtime -2 -print

Recursively grep for a pattern down a directory tree:

find /book -print | xargs grep ‘[Nn] utshell’

If the files kt1 and kt2 exist in the current directory, their names can be printed with the command:

$ find . -name ‘kt[0-9] ’ ./kt1 ./kt2

Since the command prints these names with an initial ./ path, you need to specify the ./ when using the -path option:

$ find . -path ‘./kt[0-9] ’ ./kt1 ./kt2

The -regex option uses a complete pathname, like -path, but treats the following argument as a regular expression rather than a glob pattern (although in this case the result is the same):

$ find . -regex ‘./kt[0-9] ’ ./kt1 ./kt2

$ find /usr/bin -type f -perm 4755 -exec ls -l {} \;

$ find ./ -maxdepth 1 -type d ! -name “.” // -and -or ! 都可以用

$ find /app/logs -type f –name “*.log” –mtime +7 –exec rm -f {} \;
\$ find /app/logs -type f -name “*.log” -mtime +7 –delete
$ find /app/logs -type f -name “*.log” -mtime +7 |xargs rm –f;

—rename—

文件查看及处理命令(19个)
cat more cut sort 在/bin下面
tr seq less head tac tail paste uniq wc iconv file diff vimdiff tree chattr lsattr rev在/sur/bin下面

—cat—

Read(concatenate 连锁) one or more files and print them on standard output
e.g. 1: cat ch1 —Display a file
e.g. 2: cat ch1 ch2 ch3 > all —Combine files
e.g. 3: cat note5 >> notes —Append to a file
e.g. 4: cat > temp1— Create file at terminal and edit, don’t forget add a(Enter). To exit, enter (Ctrl-D).
cat > temp2 << EOF —Create file at terminal and add contents. To exit, enter EOF.

-****n : 显示行号

—more—

值得一提的是,more和less可以切换到vi状态*记住按空格键和b就好了*
+num :从numbered line往下显示
-num : set screen size

—less—
比more强大,按page up和down来翻页,/pattern 来向下寻找字符,?pattern向上寻找字符

—cut—

-c : 指定characters个数cut
# cut -c 1-10 /etc/passwd
-d:指定分隔符delimiter,默认是空格
#cut -d: -f1,6 /etc/passwd
#who | cut -d” ” -f1
#cut -c4 file | paste - file
[root@localhost-ziyi home]# who|cat >temp10
[root@localhost-ziyi home]# cat temp10
root pts/0 2017-03-21 20:07 (192.168.0.101)
root pts/1 2017-03-21 09:27 (192.168.0.101)
[root@localhost-ziyi home]# cut -c4 temp10|paste - file1
t .
t |– a
|– file1
|– test
|– test1
|– test2
-- test3
– file1

—sort—

-b 忽略每行前面开始出的空格字符。
-c 检查文件是否已经按照顺序排序。
-f 排序时,忽略大小写字母。
-M 将前面3个字母依照月份的缩写进行排序。
-n 依照数值的大小排序。
-o<输出文件> 将排序后的结果存入指定的文件。
-r 以相反的顺序来排序。
-t<分隔字符> 指定排序时所用的栏位分隔字符。
-k 选择以哪个区间进行排序。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
下面通过几个例子来讲述Sort的使用。
(1)sort将文件的每一行作为一个单位,相互比较,比较原则是从首字符向后,依次按ASCII码值进行比较,最后将他们按升序输出。
[rocrocket@rocrocket programming] catseq.txtbananaapplepearorange[rocrocket@rocrocketprogramming] sort seq.txt
apple
banana
orange
pear
用户可以保存排序后的文件内容,或把排序后的文件内容输出至打印机。下例中用户把排序后的文件内容保存到名为result的文件中。
Sortseq.txt>result2sortu[rocrocket@rocrock

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值