Shell命令实际操作

1.Shell命令提示符

[root@localhost ~]# 超级用户的命令提示符

[zhou@localhost ~]$   普通用户的命令提示符

#为超级用户的提示符,在命令行输入su 所要切换的用户名即可切换至该用户,不需密码输入;

[root@localhost ~]# su zhou

[zhou@localhost root]$ 

 

$为普通用户的命令提示符 ,在命令行输入su ,之后进行密码输入,就可切换至root用户。

[zhou@localhost root]$ su

Password: 

[root@localhost ~]# 

 

注:

@之前的rootzhou表已登录的用户名;

@之后的localhost表计算机的主机名,若未设置过主机名,则默认为localhost

localhost之后的为当前目录名(~表示用户的主目录)


常用Shell命令

1.目录的创建与删除命令

(1)mkdir命令

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

-p 一次性创建多级目录

例如:

创建名为test的目录,并在其下创建file目录。

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

[root@localhost ~]# ls test

file

[root@localhost ~]# ls

a1   anaconda-ks.cfg  Documents    install.log.syslog  Templates

a2   a.out            Download     Music               test

a3   b.c              hello.c      Pictures            Videos

a.c  Desktop          install.log  Public

 

(2)rmdir命令

从一个目录中删除一个或多个子目录项,要求目录删除之前须为空

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

例如:

删除test的目录下的flie目录,同时将test目录一并删除。

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

[root@localhost ~]# ls

a1  a.c              b.c        Download     install.log.syslog  Public

a2  anaconda-ks.cfg  Desktop    hello.c      Music               Templates

a3  a.out            Documents  install.log  Pictures            Videos

[root@localhost ~]# ls test

ls: cannot access test: No such file or directory

 

2.改变工作目录的命令

将当前目录改变为指定的目录。若没有指定目录,则回到用户的主目录,也可使用“cd ..” 返回到系统的上一级目录。

[root@localhost ~]# cd /home

[root@localhost home]# ls

4     a.txt  group       passwd   peat   test   tt1          zhou

a.sh  b.txt  linuxshare  passwd2  share  test3  windowshare

[root@localhost home]# cd zhou

[root@localhost zhou]# 

当所要切换的目录和当前目录不属于父子关系时,加上“/”;属于父子关系时,不用加“/”。

如:~目录下没有home目录--加上“/”;home目录下有zhou目录,不用加“/”。

[root@localhost home]# cd ..

[root@localhost /]# 

cd ..:切换回上一级目录

 

3.显示路径的命令pwd

显示当前目录的绝对路径

[root@localhost ~]# pwd

/root

 

4.显示目录内容的命令ls

[root@localhost ~]# ls

a1  a.c              b.c        Download     install.log.syslog  Public

a2  anaconda-ks.cfg  Desktop    hello.c      Music               Templates

a3  a.out            Documents  install.log  Pictures            Videos

查看当前目录下的文件和子目录信息

 

[root@localhost ~]# ls -a

.                .dbus            install.log.syslog   Videos

..               Desktop          .local               .viminfo

a1               Documents        .metacity            .wapi

a2               Download         Music                .xauth1NYVNZ

a3               .esd_auth        .nautilus            .xauth2LlHKf

a.c              .gconf           Pictures             .xauthdrTYNC

anaconda-ks.cfg  .gconfd          Public               .xauthHfehdg

a.out            .gnome2          .pulse               .xauthlmOrx8

.bash_history    .gnome2_private  .pulse-cookie        .xauthnfmXDh

.bash_logout     .gstreamer-0.10  .recently-used.xbel  .xauthNZMsp7

.bash_profile    .gtk-bookmarks   .sqlite_history      .xauthRDwS0F

.bashrc          .gvfs            .tcshrc              .xauthutuVnB

b.c              hello.c          Templates            .xauthX0Yr7K

.config          .ICEauthority    .tomboy              .xauthZXaVPA

.cshrc           install.log      .tomboy.log

该命令等同于

[root@localhost ~]# ls --all

显示所有文件和子目录,包括隐藏文件和隐藏子目录(以“.”开头)

 

[root@localhost ~]# ls -al /etc

total 1756

drwxr-xr-x 106 root root   12288 2014-08-03 08:37 .

drwxr-xr-x  22 root root    4096 2014-08-03 08:34 ..

drwxr-xr-x   4 root root    4096 2014-03-16 19:17 acpi

-rw-r--r--   1 root root      45 2014-03-29 02:31 adjtime

-rw-r--r--   1 root root    1512 2005-04-26 00:48 aliases

-rw-r-----   1 root smmsp  12288 2014-08-03 08:34 aliases.db

查看/etc目录下的所有文件和子目录的详细信息。

 

5.显示文件内容命令

(1)cat命令

创建文本文件f1,显示文件的内容。

[root@localhost ~]# cat >f1 输入该命令后,可将要输入内容写入文本

hello

输入hello,按Ctrl+D快捷键,在当前目录下保存文件f1

[root@localhost ~]# cat f1 输入该命令,可查看文件内容

hello

 

(2)more命令

分屏显示文件的内容

例如:

分屏显示/etc目录下的passwd文件的内容

[root@localhost ~]# more /etc/passwd

继续显示--Enter键或空格键,按q键退出该命令。

显示完毕会自行退出

 

(3)less命令

分屏显示文件的内容

例如:

分屏显示/etc目录下的passwd文件的内容

[root@localhost ~]# less /etc/passwd

继续显示--使用上下方向键、Enter键、空格键、PageDownPageUp键前后翻阅文本内容,按q键退出该命令。

显示完毕不会自行退出,需按q键退出。

 

(4)head命令

显示文件的头几行内容

例如:

显示/etc/passwd文件的前2行内容。

[root@localhost ~]# head -2 /etc/passwd

屏幕显示:

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

未给出n值,默认设置为10

 

(5)tail命令

显示文件最后几行内容

[root@localhost ~]# tail -4 /etc/passwd

mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash

zhou:x:500:500:zhou:/home/zhou:/bin/bash

test3:x:503:503::/home/test3:/bin/bash

tt1:x:504:504::/home/tt1:/bin/bash

 

6.文件内容查询命令grep

例如:

在文件/etc/passwd中查找“root”字符串

[root@localhost ~]# grep  "root"  /etc/passwd

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

查找到含有root的行

 

搜索出当前目录下所有文件中含有“data”字符串的行

[root@localhost ~]# grep  data  *

Binary file a.out matches

install.log:Installing tzdata-2008b-1.fc9.noarch

install.log:Installing tzdata-java-2008b-1.fc9.noarch

install.log:Installing libiptcdata-1.0.2-2.fc9.i386

install.log:Installing hwdata-0.217-1.fc9.noarch

install.log:Installing gnome-mime-data-2.18.0-2.fc7.noarch

install.log:Installing obex-data-server-0.3.1-1.fc9.i386

install.log:Installing yum-metadata-parser-1.1.2-8.fc9.i386

install.log:Installing mono-data-1.9.1-2.fc9.i386

install.log:Installing mono-data-sqlite-1.9.1-2.fc9.i386

install.log:Installing evolution-data-server-2.22.1-2.fc9.i386

install.log.syslog:<21>Mar 16 07:20:34 sendmail[2495]: alias database /etc/aliases rebuilt by root

 

7.文件查找命令find

例如:

在根目录下查找文件名为“temp”或是匹配“install*”的所有文件

[root@localhost ~]# find / -name 'temp' -o -name 'install*'

/root/install.log.syslog

/root/install.log

/root/.local/share/Trash/files/sqlite-3.3.7/install-sh

/selinux/class/db_database/perms/install_module

/selinux/class/x_colormap/perms/install

/tmp/vmware-tools-distrib/etc/installer.sh

/tmp/vmware-tools-distrib/installer

/sbin/install-info

 

在根目录下查找文件名不是‘temp’的所有文件

[root@localhost ~]# find  /  !  -name 'temp'


8.文件内容统计命令wc

例如:

统计文件f1的字节数、行数和字数

[root@localhost ~]# wc  -clw  f1

屏幕显示:

1    1    6  f1

 

9.文件的复制、移动和删除命令

(1)cp命令

例如:

f1文件复制为f2,若f2文件已存在,则备份原来的f2文件

[root@localhost ~]# touch f1

[root@localhost ~]# ls

f1

[root@localhost ~]# cat >f2

hello

^C

[root@localhost ~]# cp -b f1 f2

cp: overwrite `f2'? y

[root@localhost ~]# ls

f1  f2  f2~


(2)mv命令

例如:

将当前工作目录下的f1文件移动到/root/test目录下

[root@localhost ~]# mkdir test

[root@localhost ~]# ls

a1   anaconda-ks.cfg  Documents  f2~                 Music      test

a2   a.out            Download   hello.c             Pictures   Videos

a3   b.c              f1         install.log         Public

a.c  Desktop          f2         install.log.syslog  Templates

[root@localhost ~]# mv f1 test

[root@localhost ~]# ls test

f1

屏幕显示f1


test目录改名为mytest

[root@localhost ~]# mv test mytest

[root@localhost ~]# ls

a1   anaconda-ks.cfg  Documents  hello.c             mytest     Videos

a2   a.out            Download   install.log         Pictures

a3   b.c              f2         install.log.syslog  Public

a.c  Desktop          f2~        Music               Templates


(2)rm命令

删除文件或目录

例如:

删除当前目录下的f2文件

[root@localhost ~]# rm  -f  f2

[root@localhost ~]# ls

屏幕显示无f2文件

-f 强制删除,无提示信息

 

删除mytest目录,连同其下子目录

[root@localhost ~]# ls mytest

f1

[root@localhost ~]# rm -r mytest

rm: descend into directory `mytest'? y

rm: remove regular empty file `mytest/f1'? y

rm: remove directory `mytest'? y

[root@localhost ~]# ls

a1  a.c              b.c        Download  install.log         Pictures   Videos

a2  anaconda-ks.cfg  Desktop    f2~       install.log.syslog  Public

a3  a.out            Documents  hello.c   Music               Templates

屏幕显示已无mytest目录


10.帮助命令man

显示mkdir命令的帮助信息

[root@localhost ~]# man mkdir

Formatting page, please wait...

q键退出

 

11.创建文件命令touch

[root@localhost ~]# touch a b c

[root@localhost ~]# ls

创建文件abc,创建多个文件--并列,用空格隔开

屏幕显示有a b c3个文件

 

删除a b c3个文件,可用rm

[root@localhost ~]# rm a b c

rm: remove regular empty file `a'? y 

rm: remove regular empty file `b'? y

rm: remove regular empty file `c'? y

[root@localhost ~]# ls

屏幕显示无a b c3个文件


[root@localhost ~]# rm -rf a b

若用该命令,则无提示,直接强制删除ab文件。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值