Linux 常用命令

[size=medium]

[b]Linux查看一台服务器有多少个cpu、每个cpu有几个核心、每个核心有几个线程[/b]
查看物理cpu个数

grep 'physical id' /proc/cpuinfo | sort -u | wc -l

查看每个cpu核心数量

grep 'core id' /proc/cpuinfo | sort -u | wc -l

查看线程总数

grep 'processor' /proc/cpuinfo | sort -u | wc -l

[url]https://jingyan.baidu.com/article/63acb44a81001361fcc17e21.html[/url]


[b]查看压缩文件内容命令:zcat[/b]
zcat命令能够不用将.gz的压缩文件解压就能查看里面的内容
# zcat file.gz

[b]获取当前日期,日期加减后的值[/b]
display today with yyyy-mm-dd format
date +%Y-%m-%d

display the date which is 5 days ago than today
date --date="5 days ago" +%Y-%m-%d

set 5 days ago to a variable
exportedDateVar = $(date --date="5 days ago" +%Y-%m-%d)


[b]创建文件[/b]
Depending on what you want the file to contain:

touch /path/to/file for an empty file
somecommand > /path/to/file for a file containing the output of some command.

eg: grep --help > randomtext.txt
echo "This is some text" > randomtext.txt
nano /path/to/file or vi /path/to/file (or any other editor emacs,gedit etc)
It either opens the existing one for editing or creates & opens the empty file to enter, if it doesn't exist

[b]执行脚本的方式(Execute shell script)[/b]
1)对于ksh,如果希望脚本在当前shell而不是subshell中执行,
用"dot space dot slash"即". ./"
. ./file.sh
2) Execute Shell Script Using Source Command
source file.sh
3) Execute Shell SCript by Specifying the Interpreter
csh file.sh
4) Execute Shell Script Using File Name
./file.sh
or
/a/b/c/file.sh

按时间升序列出文件
ls -lt

按时间降序列出文件
ls -ltr

AWK命令快速入门
[url]http://blog.csdn.net/hello_hwc/article/details/39528879[/url]

[b]查找命令[/b]
递归列出指定目录下的所有目录和文件
find 指定目录
1. find
find是最常见和最强大的查找命令,你可以用它找到任何你想找的文件。
find的使用格式如下:
  $ find <指定目录> <指定条件> <指定动作>
  - <指定目录>: 所要搜索的目录及其所有子目录。默认为当前目录。
  - <指定条件>: 所要搜索的文件的特征。
  - <指定动作>: 对搜索结果进行特定的处理。
如果什么参数也不加,find默认搜索当前目录及其子目录,并且不过滤任何结果(也就是返回所有文件),将它们全都显示在屏幕上。
find的使用实例:
  $ find . -name 'my*'
搜索当前目录(含子目录,以下同)中,所有文件名以my开头的文件。
  $ find . -name 'my*' -ls
搜索当前目录中,所有文件名以my开头的文件,并显示它们的详细信息。
  $ find . -type f -mmin -10
搜索当前目录中,所有过去10分钟中更新过的普通文件。如果不加-type f参数,则搜索普通文件+特殊文件+目录。

2. locate
locate命令其实是“find -name”的另一种写法,但是要比后者快得多,原因在于它不搜索具体目录,而是搜索一个数据库(/var/lib/locatedb),这个数据库中含有本地所有文件信息。Linux系统自动创建这个数据库,并且每天自动更新一次,所以使用locate命令查不到最新变动过的文件。为了避免这种情况,可以在使用locate之前,先使用updatedb命令,手动更新数据库。
locate命令的使用实例:
  $ locate /etc/sh
搜索etc目录下所有以sh开头的文件。
  $ locate ~/m
搜索用户主目录下,所有以m开头的文件。
  $ locate -i ~/m
搜索用户主目录下,所有以m开头的文件,并且忽略大小写。

3. whereis
whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b)、man说明文件(参数-m)和源代码文件(参数-s)。如果省略参数,则返回所有信息。
whereis命令的使用实例:
  $ whereis grep

4. which
which命令的作用是,在PATH变量指定的路径中,搜索某个系统命令的位置,并且返回第一个搜索结果。也就是说,使用which命令,就可以看到某个系统命令是否存在,以及执行的到底是哪一个位置的命令。
which命令的使用实例:
  $ which grep

5. type
type命令其实不能算查找命令,它是用来区分某个命令到底是由shell自带的,还是由shell外部的独立二进制文件提供的。如果一个命令是外部命令,那么使用-p参数,会显示该命令的路径,相当于which命令。
type命令的使用实例:
  $ type cd
系统会提示,cd是shell的自带命令(build-in)。
  $ type grep
系统会提示,grep是一个外部命令,并显示该命令的路径。
  $ type -p grep
加上-p参数后,就相当于which命令。

6. examples
查找当前目录下的以dz开头的目录
access_log 2>/dev/null表示不显示permmisiondenied 错误信息。
find . -type d -name 'dz*' access_log 2>/dev/null

7. grep or egrep
ps -ef | grep 'java'

Reference: Linux的五个查找命令
[url]http://www.ruanyifeng.com/blog/2009/10/5_ways_to_search_for_files_using_the_terminal.html?bsh_bid=223636115[/url]

[b]Grep命令详解-9个经典使用场景[/b]
[url]http://blog.csdn.net/hello_hwc/article/details/40017833[/url]

[b]Finding all files containing a text string on Linux[/b]
[url]http://stackoverflow.com/questions/16956810/finding-all-files-containing-a-text-string-on-linux[/url]
Do the following:
grep -rnw 'directory' -e "pattern"
-r or -R is recursive, -n is line number and -w stands match the whole word. -l (letter L) can be added to have just the file name.

Along with these, --exclude or --include parameter could be used for efficient searching. Something like below:

grep --include=\*.{c,h} -rnw 'directory' -e "pattern"
This will only search through the files which have .c or .h extensions. Similarly a sample use of --exclude:

grep --exclude=*.o -rnw 'directory' -e "pattern"
Above will exclude searching all the files ending with .o extension. Just like exclude file it's possible to exclude/include directories through --exclude-dir and --include-dir parameter, the following shows how to integrate --exclude-dir:

grep --exclude-dir={dir1,dir2,*.dst} -rnw 'directory' -e "pattern"
This works for me very well, to achieve almost the same purpose like yours.
For more options :
man grep


[b]Editing a previous command line[/b]
参考文档:[url]http://unixhelp.ed.ac.uk/shell/ksh_hist.html[/url]
To edit a previous command line use the shell command fc (fix command) command.


[b]Delete Files Older Than x Days on Linux[/b]
The find utility on linux allows you to pass in a bunch of interesting arguments, including one to execute another command on each file. We’ll use this in order to figure out what files are older than a certain number of days, and then use the rm command to delete them.

Command Syntax

find /path/to/files* -mtime +5 -exec rm {} \;

[color=red]Note that there are spaces between "rm", "{}", and "\;"[/color]

Explanation
The first argument is the path to the files. This can be a path, a directory, or a wildcard as in the example above. I would recommend using the full path, and make sure that you run the command without the exec rm to make sure you are getting the right results.
The second argument, -mtime, is used to specify the number of days old that the file is. If you enter +5, it will find files older than 5 days.
The third argument, -exec, allows you to pass in a command such as rm. The [color=red]{} \;[/color] at the end is required to end the command.
This should work on Ubuntu, Suse, Redhat, or pretty much any version of linux.


[b]To delete all files and directories within the current directory[/b]

find . -mtime +3 | xargs rm -Rf
Or alternatively, more in line with the OP's original command:

find . -mtime +3 -exec rm -Rf -- {} \;


fc -e editor -lnr first last
This will open the commands from the history list as specified in the range first to last. When you exit from the editor the command(s) will be run.

Setting the environment variable FCEDIT to the editor that you prefer will save you specifying it on the command line. For example:

FCEDIT=emacs; export FCEDIT
Examples of editing previous command lines

To list commands from the history list:

fc -l 27 34
This lists commands 27 through 34 from the history list. Using the -n option instead of -l prevents the history numbers from being listed.

To edit a command from the history list:

fc -e emacs 32
This will start the EMACS editor together with the contents of command number 32. You can then edit the command and exit the editor to run the command.

To edit several commands from the history list:

[color=blue]fc -e vi 27 35[/color]
This will start the vi editor and open a temporary file containing commands 27 through 35. You can then edit the commands and exit the editor to run the commands.


[b]查看本系统共支持哪些shell[/b]
cat /etc/shells

[b]两个快捷键[/b]
CTRL+L 清屏
CTRL+U 清除一行中(光标前的部分)

[b]命令别名[/b]
alias:查看当前的别名

[b]输入输出重定向[/b]
将输出的内容重定向到/dev/null中,这里可以理解/dev/null为一个黑洞,重定向到这里的东西都不会打印出来.
符号 >> 是清空原内容,然后写入
符号 > 是在原内容的末尾追加

输入重定向
符号 <
/dev/tty 这个文件常用来重定向到一个终端,对输入来说十分有用
read password < /dev/tty 从终端读取密码

错误输出重定向,只有在遇到错误的时候才进行重定向
常用的处理是生成相关的log文件
cp –R /usr /backup/ 2> /bak.error


[b]wc命令用来统计多少行,多少个单词,多少个字符[/b]
wc #会依次输出 行数,单词数,字符个数
wc -l #行数
wc -w#单词数
wc -c #字符个数

文件行数
1) sed -n '$=' filename
2) wc -l filename

[b]命令连接符[/b]
&& 前一个命令执行成功,后一个命令才会执行
|| 前一个命令执行失败,后一个命令才会执行

[b]命令替换符号[/b]
在键盘tab上边的那个"`",注意不是引号
遇到命令替换符号,会把命令替换符号内的语句当成命令执行,然后把执行结果返回给上一层
ls -l `which bash`

[b]读取文件的开头或者结尾[/b]
head -n 文件前几行
tail -n file 文件后几行
tail -f file 持续观察一个文件的结尾,适合动态观察log文件

[b]查看帮助命令[/b]
//Take the shell command "find" as an example:
1)man find
2)info find
3)find --help
4)whatis find

//other commands
which find //show the current verion we are using
whereis find//show the place which the find locates


[b]tar 命令[/b]
Examples
tar -cf archive.tar file1 file2
Create archive archive.tar containing files file1 and file2.

tar -tvf archive.tar
List the files in the archive archive.tar verbosely.

tar -xf archive.tar
Extract the files from archive archive.tar.

tar -xf archive.tar -C /target/directory
解压缩到指定目录

tar -xzvf archive.tar.gz
Extract the files from gzipped archive archive.tar.gz verbosely.

[b]重命名单个文件[/b]
use mv command to rename a single file

[b]rename 重命名多个文件,大小些转换[/b]
参考:
[url]http://blog.csdn.net/zgl_dm/article/details/6627411[/url]
[url]http://www.computerhope.com/unix/rename.htm[/url]
Linux 下的 rename 命令有两个版本:
C语言版本
Perl语言版本
其中,C语言版本的命令如下:
rename 原字符串 新字符串 文件名
样例1: 目录 test 下存在两个文件: a_01 、a_02,若需要将文件名中的 a 替换为 b, 其他部分保持不变,则
(1) 若 当前位于 test 目录下,则执行 rename a b *
(2) 若当前位于test的父目录,则执行 rename a b test/*
Perl 语言版本的命令如下:
rename ‘s/原字符串/新字符串/’ 文件名

样例2: 题目如样例1,则
(1) 若当前位于 test 目录下,则执行 rename ‘s/a/b/’ *
(2) 若当前位于test的父目录,则执行 rename 's/a/b' test/*
样例3:若改用脚本,可如下:

[html] view plaincopy
find -name "a*" -print |
while read name; do
echo $name
rename 's/a/b/' $name
done

样例4:
rename 'y/A-Z/a-z/' *
Rename files such that all uppercase letters are changed to their lowercase equivalents.

最后,该如何判断rename是哪个版本,可通过 man rename 查看。
若第一行为如下,则是C语言版本的,
RENAME(1) Linux Programmer’s Manual RENAME(1),
若出现
RENAME(1) Perl Programmers Reference Guide RENAME(1)
则为 Perl 版本的。

[b]df:The df command reports the amount of available disk space being used by file systems. df command is able to display the physical server name[/b]
1. Check File System Disk Space Usage
The “df” command displays the information of device name, total blocks, total disk space, used disk space, available disk space and mount points on a file system.
# df

Filesystem 1K-blocks Used Available Use% Mounted on
/dev/cciss/c0d0p2 78361192 23185840 51130588 32% /
/dev/cciss/c0d0p5 24797380 22273432 1243972 95% /home
/dev/cciss/c0d0p3 29753588 25503792 2713984 91% /data
/dev/cciss/c0d0p1 295561 21531 258770 8% /boot
tmpfs 257476 0 257476 0% /dev/shm
2. Display Information of all File System Disk Space Usage
The same as above, but it also displays information of dummy file systems along with all the file system disk usage and their memory utilization.
# df -a

3. Show Disk Space Usage in Human Readable Format
# df -h

4. Display Information of /home File System
To see the information of only device /home file system in human readable format use the following command.
# df -hT /home

5. Display Information of File System in Bytes
To display all file system information and usage in 1024-byte blocks, use the option ‘-k‘ (e.g. –block-size=1K) as follows.
# df -k

6. Display Information of File System in MB
To display information of all file system usage in MB (Mega Byte) use the option as ‘-m‘.
# df -m

7. Display Information of File System in GB
To display information of all file system statistics in GB (Gigabyte) use the option as ‘df -h‘.
# df -h

8. Display File System Inodes
Using '-i' switch will display the information of number of used inodes and their percentage for the file system.
# df -i

9. Display File System Type
If you notice all the above commands output, you will see there is no file system type mentioned in the results. To check the file system type of your system use the option ‘T‘. It will display file system type along with other information.
# df -T

10. Include Certain File System Type
If you want to display certain file system type use the ‘-t‘ option. For example, the following command will only display ext3 file system.
# df -t ext3

11. Exclude Certain File System Type
If you want to display file system type that doesn’t belongs to ext3 type use the option as ‘-x‘. For example, the following command will only display other file systems types other than ext3.
# df -x ext3


[b]
du:Report the amount of disk space used by a file or files.
显示文件或目录大小[/b]
whatis du
output: estimate file space usage
so du may refer to directory space usage

列出当前目录下所有子目录的大小
du -sh *

查看一个指定目录的大小
du -sh <directory>

查看当前目录的大小
du -lh

du -sh 文件夹/文件
du -sk 文件夹/文件
du -sm 文件夹/文件
查看指定文件 或者文件夹 的大小

sh/sk/sm 的区别:
sh 根据文件大小显示为nK/nM/nG eg. [4.0K xxx.log]
sk 根据文件大小显示为nK eg. [4.3K xxx.log]
sm 根据文件大小显示为nM eg. [7M xxx.log]

列出当前目录下所有子目录和文件, 其中有一项为大小
ls -lh

案例:
1. du -sh
  查看当前文件夹大小
  如下:
  Info代码
  $du -sh
  7.0M xxx.log
  $du -sh 7.0M xxx.log

2. du -sh * | sort -n
  列出当前文件夹下的所有文件夹及其大小,并按照文件夹大小排序
  如下:
  Info代码
  $du -sh * | sort -n
  4.0K xxx.log
  4.0K xxx.log
  4.4M xxx.log
  4.8M xxx.log
  $du -sh * | sort -n 4.0K xxx.log 4.0K xxx.log 4.4M xxx.log 4.8M xxx.log


[url]https://www.ibm.com/developerworks/community/blogs/58e72888-6340-46ac-b488-d31aa4058e9c/entry/estimate_file_space_usage_with_du_command_in_linux62?lang=en[/url]


[b]Linux下cut命令用法详解[/b]

有时我们经常会遇到这样一些问题:有一页电话号码薄,上面按顺序规则地写着人名、家庭住址、电话、备注等,此时我们只想取出所有人的名字和其对应的电话号码,你有几种方法可以实现呢?

确实这种纵向定位的方式用常规办法难以实现,这时,cut就可以大显身手了。

What’s cut?

cut命令可以从一个文本文件或者文本流中提取文本列,主要用来提取各种各样的数据。


命令用法:

cut -b list [-n] [file ...]

cut -c list [file ...]

cut -f list [-d delimiter][-s][file ...]



上面的 -b、-c、-f分别表示字节、字符、字段(即byte、character、field);

list表示-b、-c、-f操作范围,-n常常表示具体数字;

file表示的自然是要操作的文本文件的名称;

delim(英文全写:delimiter)表示分隔符,默认情况下为TAB;

-s表示不包括那些不含分隔符的行(这样有利于去掉注释和标题)





上面三种方式中,表示从指定的范围中提取字节(-b)、或字符(-c)、或字段(-f)。

范围的表示方法:

N
只有第N项

N-
从第N项一直到行尾

N-M
从第N项到第M项(包括M)

-M
从一行的开始到第M项(包括M)

-
从一行的开始到结束的所有项


英文字符格式文本提取实例

cut -cchars file
如:
-c5 提取第5个字符
-c5- 提取第5个字符以后的字符
-c1,5,12 提取多个字符,中间用“,”符号隔开
-c5-14 提取第5个字符到第14个字符间的字符
http://gan.cublog.cn
[service@dsg tmp]$ cat f.txt
service pts/0 Oct 9 20:27 (211.95.114.235)
service pts/1 Oct 9 21:06 (218.80.203.242)
service pts/2 Oct 9 14:35 (218.80.203.242)
service pts/3 Oct 9 21:07 (218.80.213.242)
service pts/4 Oct 9 21:07 (218.80.213.242)
service pts/5 Oct 9 21:45 (58.31.205.19)
[service@dsg tmp]$ cut -c5 f.txt
i
i
i
i
i
i
[service@dsg tmp]$ cut -c5- f.txt
ice pts/0 Oct 9 20:27 (211.95.114.235)
ice pts/1 Oct 9 21:06 (218.80.203.242)
ice pts/2 Oct 9 14:35 (218.80.203.242)
ice pts/3 Oct 9 21:07 (218.80.213.242)
ice pts/4 Oct 9 21:07 (218.80.213.242)
ice pts/5 Oct 9 21:45 (58.31.205.19)
[service@dsg tmp]$ cut -c1,5,14 f.txt
si0
si1
si2
si3
si4
si5
------------------------------
cut -d -f
-d, --delimiter=DELIM
use DELIM instead of TAB for field delimiter
-f, --fields=LIST
output only these fields; also print any line that contains no
delimiter character, unless the -s option is specified
-d -f 主要用来从某中分隔符中提取数据
如:
[service@dsg tmp]$ cat f.txt
service1:pts/0:Oct 9 20:27: (211.95.114.235)
service2:pts/1:Oct 9 21:06: (218.80.203.242)
service3:pts/2:Oct 9 14:35: (218.80.203.242)
service4:pts/3:Oct 9 21:07: (218.80.213.242)
service5:pts/4:Oct 9 21:07: (218.80.213.242)
service6:pts/5:Oct 9 21:45: (58.31.205.19)
[service@dsg tmp]$ cut -d: -f1 f.txt
service1
service2
service3
service4
service5
service6
[service@dsg tmp]$ cut -d: -f2 f.txt
pts/0
pts/1
pts/2
pts/3
pts/4
pts/5
[service@dsg tmp]$ cut -d: -f3 f.txt
Oct 9 20
Oct 9 21
Oct 9 14
Oct 9 21
Oct 9 21
Oct 9 21
[service@dsg tmp]$ cut -d: -f5 f.txt
(211.95.114.235)
(218.80.203.242)
(218.80.203.242)
(218.80.213.242)
(218.80.213.242)
(58.31.205.19)
[service@dsg tmp]$ cut -d: -f9 f.txt
[service@dsg tmp]$ cut -d: -f1,4 f.txt #提取1和4列数据
service1:27
service2:06
service3:35
service4:07
service5:07
service6:45
Given that the fields are separated by tabs, you should use the -f option to cut instead:
如果文件使用tab键隔开就直接使用-f就可以了,-d的默认分隔符号就为tab键。

中文文本提取实例

在这个文件中,每个汉字都是用半角空格分隔的。
chinese.txt
春天来了,万物复苏了。
让我们,一起去欣赏春的魅力吧。

Cut -c-1 chinese.txt right


Cut –b-2 chinese.txt right
Cut –b-1 chinese.txt wrong

汉字本身是双字节的,cut –c把汉字“春”当成一个字符来处理,而cut –b是以字节来处理,把“春”拆成了两个字节,结果是字符被“切成两半”,因此无法正常显示。


[b]Paste命令[/b]
该命令主要用来将多个文件的内容合并,与cut命令完成的功能刚好相反。

使用事例:
$ cat names
Tony
Emanuel
Lucy
Ralph
Fred
gan.cublog.cn
$ cat numbers
(307) 555-5356
(212) 555-3456
(212) 555-9959
(212) 555-7741
(212) 555-0040

$ paste names numbers 将两个文件合并,他们中间用tab键分隔开
Tony (307) 555-5356
Emanuel (212) 555-3456
Lucy (212) 555-9959
Ralph (212) 555-7741
Fred (212) 555-0040
gan.cublog.cn
$ cat addresses
55-23 Vine Street, Miami
39 University Place, New York
17 E. 25th Street, New York
38 Chauncey St., Bensonhurst
17 E. 25th Street, New York
gan.cublog.cn
$ paste names addresses numbers
Tony 55-23 Vine Street, Miami (307) 555-5356
Emanuel 39 University Place, New York (212) 555-3456
Lucy 17 E. 25th Street, New York (212) 555-9959
Ralph 38 Chauncey St., Bensonhurst (212) 555-7741
Fred 17 E. 25th Street, New York (212) 555-0040

$ paste -d'+' names addresses numbers 他们中间用指定的'+'符号隔开
Tony+55-23 Vine Street, Miami+(307) 555-5356
Emanuel+39 University Place, New York+(212) 555-3456
Lucy+17 E. 25th Street, New York+(212) 555-9959
Ralph+38 Chauncey St., Bensonhurst+(212) 555-7741
Fred+17 E. 25th Street, New York+(212) 555-0040

$ paste -s names 将多行数据合并到一行,他们间用tab键隔开
Tony Emanuel Lucy Ralph Fred

[/size]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值