linux常用命令

Linux中的目录

路径:也就是linux中的目录(文件夹)有绝对路径和相对路径
根目录:/
用户主目录(home directory):位于/home目录下,用户登录时
工作目录(working directory):当前目录

当前目录查看命令:pwd (print working directory)
当前目录:./
当前目录的上一级目录:../或..
返回到上一级目录:cd ..

进入当前目录下的dirfile目录:cd dirfile
cd ~ :进入用户主目录(账号所在目录)  或者直接cd回车
cd - :(回到先前的目录)

创建、删除查看和显示目录

1.创建目录

格式:mkdir [选项]  目录

功能:创建目录

常用选项说明:

 -m  创建目录的同时设置访问权限

-p  一次性创建多级目录

【例】:在rootfile文件夹下创建test文件夹,并在test文件夹下创建file文件夹。

[root@localhost rootfile]# mkdir -p test/file

[root@localhost rootfile]# ls

test

[root@localhost rootfile]# cd test

[root@localhost test]# ls

file

[root@localhost test]#

 

 

【例】:在rootfile文件夹下创建test2文件夹,并设置test2的权限为766

[root@localhost rootfile]# mkdir -m 766 test2

[root@localhost rootfile]# ls

test  test2

[root@localhost rootfile]# ls -l

total 16

drwxr-xr-x 3 root root 4096 Jul 21 21:27 test

drwxrw-rw- 2 root root 4096 Jul 21 21:30 test2

 

 

注释:rwxrw-rw-分别对应三种不同用户的权限,分别有三们二进制表示,766对应111 110 110

2.删除目录

格式:rmdir  [选项]  目录

功能:删除目录

常用选项说明:

-p  递归删除目录,当子目录删除后其父目录为空时,也一同删除

【例】:删除test下的file目录(文件夹),同时test也一并删除

[root@localhost rootfile]# ls

test  test2

[root@localhost rootfile]# rmdir -p test/file

[root@localhost rootfile]# ls

test2

 

 

3.查看当前目录

格式:pwd

功能:pwd (print working directory),查看当前目录.

常用选项说明:

【例】:查看当前目录

[root@localhost rootfile]# pwd

/home/rootfile

 

 

5.显示目录内容

格式:ls  [选项]  [文件目录]

功能:显示指定目录中的文件和了目录信息,当不指定目录时,显示当前目录下的文件和子目录信息

常用选项说明:

-a  显示所有文件和子目录,包括隐藏文件和主目录

-l  显示文件和子目录的详细信息,包括文件类型、权限、所有者和所属群组、文件大小、最后修改时间、文件名

-d  如果参数是目录,则只显示目录信息,而不显示其中所包含的文件信息

-t  按时间顺序显示

-R  不仅显示指定目录下的文件和子目录信息,而且还递归地显示子目录下的文件和子目录信息

创建和查看文件

创建文件

格式:touch filename

功能:创建文件

常用选项说明:

【例】:在rootfile下创建文件file.txt和test2/file2.txt

[root@localhost rootfile]# touch file.txt

[root@localhost rootfile]# touch test2/file2.txt

[root@localhost rootfile]# ls

file.txt  test2

[root@localhost rootfile]# cd tes*

[root@localhost test2]# ls

file2.txt

 

 

cat命令

格式:cat  [选项]  filename

功能:依次读取filename中的内容

常用选项说明:

【例】:读取rootfile下Test.java和file中的文件内容

[root@localhost rootfile]# ls

file.txt  test2  Test.class  Test.java

[root@localhost rootfile]# vi test2

[root@localhost rootfile]# vi file*

[root@localhost rootfile]# cat Test.java

public class Test {

        public static void main(String args[]) {

                System.out.println("Hello Linux!");

        }

}

[root@localhost rootfile]# cat Test.java file.txt

public class Test {

        public static void main(String args[]) {

                System.out.println("Hello Linux!");

        }

}

this is a file test.

 

 

【例】:把Test.java和file.txt文件合并到combine.txt文件中

[root@localhost rootfile]# cat Test.java file.txt > combine.txt

[root@localhost rootfile]# cat comb*

public class Test {

        public static void main(String args[]) {

                System.out.println("Hello Linux!");

        }

}

this is a file test.

 

 

more命令

格式:more  [选项]  filename

功能:依次读取filename中的内容,该命令与cat的不同是可以逐屏往下翻页显示,按q退出。

常用选项说明:

-p  显示下一屏之前先清屏

-s  文件中连续的空白行压缩成一个空白行显示

【例】:显示file.txt的内容

[root@localhost rootfile]# more file.txt

this is a file test.

 

 

【例】:显示Test.java和file.txt的内容

[root@localhost rootfile]# more Test.java file.txt

::::::::::::::

Test.java

::::::::::::::

public class Test {

        public static void main(String args[]) {

                System.out.println("Hello Linux!");

        }

}

::::::::::::::

file.txt

::::::::::::::

this is a file test.

 

 

less命令

格式:less  [选项]  filename

功能:依次读取filename中的内容,该命令与more的不同是不仅可以向下翻页,还可以向上翻页,使用上下键、Enter、空格、pageDown、pageUp可以实现前后翻页,按q退出。

常用选项说明:

【例】:显示Test.java的内容

[root@localhost rootfile]# less Test.java

public class Test {

        public static void main(String args[]) {

                System.out.println("Hello Linux!");

        }

}

 

 

head命令

格式:head  [选项]  filename

功能:显示文件的头几行

常用选项说明:

-n  显示文件的前n行,如果没有n值,默认为10行

【例】:显示Test.java的前3行

[root@localhost rootfile]# head -3 Test.java

public class Test {

        public static void main(String args[]) {

                System.out.println("Hello Linux!");

 

 

tail命令

格式:tail  [选项]  filename

功能:显示文件的末尾几行

常用选项说明:

+n  从第n行开始显示

-n  显示文件的最后n行,如果没有n值,默认为最后10行

【例】:显示Test.java的最后3行

[root@localhost rootfile]# tail -3 Test.java

                System.out.println("Hello Linux!");

        }

}

 

 

文件查找

格式:find  [选项]  filename

功能:从指定的目录开始,递归地搜索其子目录,查找满足条件的文件并对之采取相关的操作

常用选项说明:

-name ‘字串’  要查找的文件名,可以用通配符*、?、[]

-group ‘字串’  文件所属的用户组名

-user  文件所属的用户名

find命令提供的查询条件可以是一个用逻辑符and、or、not组成的复合条件

-a  逻辑与

-o  逻辑或

-!  逻辑非

【例】:查找当前目录下文件名含有Test的文件

[root@localhost rootfile]# find -name 'Test*'

./Test.class

./Test.java

【例】:在根目录下查找文件名为’temp’或是匹配’install*’的所有文件

[root@localhost rootfile]# find / -name 'temp' -o -name 'instal*'

/etc/rhgb/temp

/etc/yum/pluginconf.d/installonlyn.conf

/etc/vmware-tools/installer.sh

/software/tomcat5/webapps/docs/appdev/installation.html

/software/tomcat5/temp

/sbin/install-info

/sbin/installkernel

/usr/share/aclocal-1.9/install-sh.m4

/usr/share/icons/Bluecurve/96x96/mimetypes/install.png

/usr/share/icons/Bluecurve/24x24/mimetypes/install.png

/usr/share/icons/Bluecurve/16x16/mimetypes/install.png

/usr/share/icons/Bluecurve/48x48/mimetypes/install.png

/usr/share/aclocal-1.7/install-sh.m4

/usr/share/doc/cyrus-sasl-lib-2.1.22/install.html

/usr/share/doc/sgml-common-0.6.3/html/install-catalog.html

/usr/share/doc/m2crypto-0.16/demo/Zope27/install_dir

/usr/share/doc/m2crypto-0.16/demo/ZopeX3/install_dir

/usr/share/doc/libstdc++-devel-4.1.1/html/install.html

……

 

 

【例】:在rootfile下查找不含Test*的文件

[root@localhost rootfile]# find ! -name 'Test*'

.

./.Test2.swp

./1q

./.Test.java.swp

./test2

./test2/file2.txt

./combine.txt

./file.txt

 

 

文字统计命令

格式:wc  [选项]  filename

功能:统计文件的字节数、字数、行数

常用选项说明:

-c  统计字节数

-l  统计行数

-w  统计字数

【例】:统计Test.java的字节数、行数、字数

[root@localhost rootfile]# wc Test.java

  5  14 105 Test.java

[root@localhost rootfile]# wc -wcl Test.java

  5  14 105 Test.java

 

 

复制、移动和删除文件或文件夹

cp 命令

格式:cp  [选项]  源目录或文件  目标目录或文件

功能:将给出的文件或目录复制到另一个文件或目录中

常用选项说明:

-b  若存在同名文件,则覆盖前备份原来的文件

-f  强制覆盖同名文件

-r或R  按递归方式,保留原目录结构复制文件

【例】:复制file.txt文件到file2,若file2已经存在,则备份file2.

[root@localhost rootfile]# ls

1q  combine.txt  file.txt  test2  Test.class  Test.java

[root@localhost rootfile]# cp -b file.txt file2

[root@localhost rootfile]# ls

1q  combine.txt  file2  file.txt  test2  Test.class  Test.java

[root@localhost rootfile]# cp -b file.txt file2

cp: overwrite `file2'? n

[root@localhost rootfile]# ls

1q  combine.txt  file2  file.txt  test2  Test.class  Test.java

[root@localhost rootfile]# cp -b file.txt file2

cp: overwrite `file2'? y

[root@localhost rootfile]# ls

1q  combine.txt  file2  file2~  file.txt  test2  Test.class  Test.java

 

 

【例】:把test2文件复制到test3文件夹

[root@localhost rootfile]# ls

1q  combine.txt  file2  file2~  file.txt  test2  Test.class  Test.java

[root@localhost rootfile]#

[root@localhost rootfile]# cp -r test2 test3

[root@localhost rootfile]# ls

1q  combine.txt  file2  file2~  file.txt  test2  test3  Test.class  Test.java

 

 

mv命令

格式:mv  [选项]  源目录或文件  目标目录或文件

功能:移动或重命名文件或目录

常用选项说明:

-b  若存在同名文件,则覆盖前备份原来的文件

-f  强制覆盖同名文件

【例】:将/home/rootfile下的Test.java移动到/home/rootfile /test2下

[root@localhost rootfile]# mv Test.java test2/Test

[root@localhost rootfile]# ls -R

.:

1q  combine.txt  file2  file2~  file.txt  test2  test3  Test.class

./test2:

file2.txt  Test

./test3:

file2.txt

 

 

rm 命令

格式:rm  [选项]  文件夹或目录

功能:删除文件夹或目录

常用选项说明:

-f  强制删除文件,不出现确认提示

-r或R  按递归方式删除目录,默认只删除文件

【例】:删除当前目录下的test3文件夹

[root@localhost rootfile]# ls

1q  combine.txt  file2  file2~  file.txt  test2  test3  Test.class

[root@localhost rootfile]# ls test3

file2.txt

[root@localhost rootfile]# rm -r test3

rm: descend into directory `test3'? y

rm: remove regular empty file `test3/file2.txt'? y

rm: remove directory `test3'? y

[root@localhost rootfile]# ls

1q  combine.txt  file2  file2~  file.txt  test2  Test.class

 

 

【例】: 强制删除当前目录下的test2文件夹

[root@localhost rootfile]# ls

1q  combine.txt  file2  file2~  file.txt  test2  Test.class

[root@localhost rootfile]# rm -rf test2

[root@localhost rootfile]# ls

1q  combine.txt  file2  file2~  file.txt  Test.class

 

 

shell有多种,下面有些命令在某些shell下不可用,更多更具体的命令查询:http://www.computerhope.com/unix.htm

一 打开终端

· 应用程序->附件->终端

二 根符号

· # : root 的命令提示符
· $ : 非特权用户的命令提示符

三 帮助

· whatis commandname                显示该命令的一句话帮助。 commandname。
· commandname -h                      简单帮助
· commandname --help                 更多帮助
. man commandname                   最全的帮助。例如:man man;man bash;man ls.
· man -a commandname               显示命令的解释信息。 commandname. (Unix style)
· info commandname                    显示很长的命令解释 commandname. (GNU style)
· type commandname                   显示命令 commandname 的信息。
· type -p commandname               显示命令 commandname 所的的地方。 which commandname 也可以用来做这个 。
· apropos key-word                      找到和 key-word 相关的命令。 man -k key-word 也可以做到。
· whereis                                     找到command的shell的位置,和locate差不错。

四 目录

· ls                列出目录内容 (非以.开始的文件和目录)
· ls -a            列出目录内容(所有的文件和目录)
· ls -A            列出目录内容。      (几乎所有的文件和目录,略去".." and ".")
· ls -la           列出目录所有文件和目录的详细信息。   
· ls -d           列出当前目录下的目录名称,而不是目录下的内容。

· cd foo           切换到当前目录下或者在变量 CDPATH 中列出来的目录 foo。
· cd /              切换到根目录。
· cd                切换到用户主目录。
· cd ~             切换到用户主目录。
· cd /foo         切换到绝对路径/foo 所指定的目录。
· cd ..             切换到上一级目录。
· cd ~/foo       切换到用户主目录下的 foo 目录去。
· cd -              切换到上一次所去的目录。
· (cd dir && command) 进入目录dir,执行命令command然后回到当前目录


五 文件

· touch junkfile          创建一个空文件 junkfile。
· cp foo bar               拷贝一个已有的文件 foo 到新文件 bar。
· rm junkfile               删除文件 junkfile
· rm -r directroy         删除文件 目录directory
· mv foo bar              把已有的文件 foo 重命名为 bar
· mv foo bar/baz        把已有的文件 foo 移动到新位置并重命名为 bar/baz。目录 bar 必 须 存 在 。
· mkdir foo                在当前目录下创建一个新的目录 foo。
· ln -s file link            创建 file 的符号连接 link

· file foo                      显示 foo 文件的属性。
· lsof foo                            显示文件 foo 的打开状态。
· cat test.cpp                      查看test.cpp的内容。
· cat test.cpp |more           more用来分页查看test.cpp的内容。
· </etc/motd pager              使用默认的分页程序查看文件/etc/motd 的内容。
· basename file                    返回不包含路径的文件名,如: basename /bin/tux 会返回 tux。
· dirname file                       返回文件所在路径,如:dirname /bin/tux 会返回 /bin。

· chmod 600 foo                         让已经存在的文件 foo 其他人不能读写。      (所有人都 不 能执 行) 。
· chmod 644 foo                         使文件 foo 其他的人可以读,但是不能写。      (所有人 都不能执行)。
· chmod 755 foo                         使文件 foo 其他的人能读不能写。      (所有人都可以执行)。
· chgrp                                      改变文件或目录所属的组。
· chown                                     更改某个文件或目录的属主和属组。
· chmod ugo+r file1.txt               更改读写可执行属性


· gzip foo                                              用 Lempel-Ziv(LZ77)压缩算法压缩 foo,生成 foo.gz。
· gunzip foo.gz                                      将文件 foo.gz 解压缩生成 foo。
· bzip2 foo                                            将文件 foo.bz2 解压缩生成 foo。
· tar -xvvf foo.tar                                  从打包文件 foo.tar 解出文件来。
· tar -xvvzf foo.tar.gz                            从打包压缩的文件 foo.tar.gz 中解开文件。
· tar -xvvf --bzip2 foo.tar.bz2                从文件 foo.tar.bz2 解压缩文件。
· tar -cvvf foo.tar bar/                           把目录 bar/的内容打包存放到 foo.tar 存档中。
· tar -cvvzf foo.tar.gz bar/                     把目录 bar/的内容打包并且压缩存放到 foo.tar.gz 存档中。
· tar -cvvf --bzip2 foo.tar.bz2 bar/         把目录 bar/中的内容打包存放到 foo.tar.bz2 存档里面。
· zcat README.gz | pager                      实用默认的分页显示程序 pager 来显示压缩文件 README.gz 中的内容。
· zcat README.gz > foo                        使用文件 README.gz 解开后的内容创建一个文件 foo。
· zcat README.gz >> foo                      把文件 README.gz 解开后的内容追加到文件 foo 的后面(如果文件不存在的话,就会创建一个)。
· unzip 压缩文件名.zip                             解压zip文件

find -name '*.[ch]' | xargs grep -E 'expr'    在当前目录及其子目录下所有.c和.h文件中寻找'expr'. 参见findrepo
find -type f -print0 | xargs -r0 grep -F 'example'    在当前目录及其子目录中的常规文件中查找字符串'example'
find -maxdepth 1 -type f | xargs grep -F 'example'    在当前目录下查找字符串'example'
find -maxdepth 1 -type d | while read dir; do echo $dir; echo cmd2; done    对每一个找到的文件执行多个命令(使用while循环)
find -type f ! -perm -444    寻找所有不可读的文件(对网站有用)
find -type d ! -perm -111    寻找不可访问的目录(对网站有用)



六 文本操作

· grep -e "pattern" *.html                   找到当前目录下面所有以.html 结尾的文件中含有"pattern"的行,并显示它们。
· find -name pattern                           用 shell 找到匹配 pattern 的文件名(慢一些)。
· locate -d pattern                              用 shell 找到匹配 pattern 的文件名(使用已有的规则的数据库,快一些)。
· wc –l file或 wc -w file 或wc -c file         分别计算文件的行数(line)、单词数(word)和字符数(character),file为待计算的文件名。
· find                                                 搜索文件,如根据文件名搜索:find . -name filename -print。
· sed                                                 是一个基本的查找替换程序。
· awk                                                用来提取文本文件中的字段。

· cut -b column file       将指定范围内的文件内容输出到标准输出设备(屏幕)上。
· read var                    提示用户输入,并将输入内容赋值给变量var。
· sort file.txt                对file.txt文件所有行进行排序。
· uniq                          只输出文件中内容不一致的行,如: sort file.txt | uniq。
· tee                            将数据输出到标准输出设备(屏幕) 和文件,比如:somecommand | tee outfile。
· head file                     打印文本文件开头几行。
· tail file                        打印文本文件末尾几行。
· more file                     查看 file 的内容


七 作业进程

· top                                            全屏显示进程信息。输入”q”退出。
· ps aux | pager                            用 BSD 风格输出所有正在运行的进程的信息。
· ps -ef | pager                             用 system-V 风格来输出所有正在运行的进程的信息。
· ps aux | grep -e "[e]xim4*"         显示 exim4 进程,或者运行 exim 的进程。
· ps axf | pager                            用 ASCCI 艺术形式来显示运行所有进程信息。
· kill 1234                                     杀死进程号为 1234 的进程。
· killall proc                                   杀掉所有名为 proc 的进程 *
· bg                                            列出已停止或后台的作业
· fg                                             将最近的作业带到前台
· fg n                                          将作业 n 带到前台

八 用户

· adduser                           增加用户
· password                         修改密码
· su                                   它可以让一个普通用户拥有超级用户或其他用户的权限,也可以让超级用户以普通用户的身份做一些事情。(貌似ubuntu下是sudo)
· ssh user@host                 以 user 用户身份连接到 host
· ssh -p port user@host     在端口 port 以 user 用户身份连接到 host
· ssh-copy-id user@host     将密钥添加到 host 以实现无密码登录

九 其他

· clear                         清屏
· pwd                  显示当前工作路径。
· whoami                 显示当前用户名。
· w                                 显示当前登陆的用户,可以多用户登陆
· date                             显示当前时间。
· cal                                显示某年某月的日历。
· echo                             显示器上显示一段文字,一般起到一个提示的作用。
· free                              查看当前系统内存的使用情况,它显示系统中剩余及已用的物理内存和交换内存,以及共享内存和被核心使用的缓冲区。
· df                                显示磁盘占用情况
· du                               显示目录空间占用情况
· uptime                         显示系统已经运行了多长时间。
· shutdown                     安全地关闭或重启Linux系统。(-r now用来重启)
· write                            向系统中某一个用户发送信息。
· mesg                           设定是否允许其他用户用write命令给自己发送信息。
· wall                             对全部已登录的用户发送信息,用户可以先把要发送的信息写好存入一个文件中
· expr                            进行数学运算,如要进行2+3的运算,命令为: expr 2 + 3。
· uname -a                     显示内核信息
· cat /etc/issue               显示当前的操作系统信息
· cat /proc/cpuinfo           查看 cpu 信息
· cat /proc/meminfo         查看内存信息
· ping host                      ping host 并输出结果
· whois domain                获取 domain 的 whois 信息
· dig domain                    获取 domain 的 DNS 信息
· echo                             输入一行
· printf                            输出,可以制定格式,具体看man printf
· env                             查看全局变量和环境变量
· printenv                      打印全局变量和环境变量
· set                            显示本地变量和函数
· echo $PATH                 输出环境变量
· /etc/init.d/httpd restart( service httpd restart)重启httpd

十 命令后面加&表示用新的线程!例如firefox &,表示新开线程为firefox,后面可以继续其他的命令。完!

[Linux系统信息查看命令大全:http://tech.idv2.com/2008/01/11/linux-sysinfo-cmds/]

原文地址:https://www.cnblogs.com/200911/p/3369756.html