目录相关命令)


mkdir

centos命令大全及用法?作用:make directories 创建目录

语法:mkdir [选项] 目录名称

选项:

    -p 级联创建目录,如果目标目录已存在不会报错

==================================================================================
#1、同一目录下目录文件和普通文件不可重名,因为在linux下目录文件和普通文件都被视为文件
[root@san01 ~]# ls -l test1
-rw-r--r--. 1 root root 60 Mar  4 23:17 test1
[root@san01 ~]# mkdir test1
mkdir: cannot create directory `test1': File exists2、-p参数的使用
[root@san01 ~]# mkdir aa/bb/cc
mkdir: cannot create directory `aa/bb/cc': No such file or directory
[root@web01 ~]# mkdir -p aa/bb/cc                #加上-p参数创建成功
[root@web01 ~]# tree aa                          #tree命令,看一下目录结构
aa
└── bb└── cc2
directories, 0 files[root@san01 ~]# ls -ld aa
drwxr-xr-x. 3 root root 4096 Mar  5 02:44 aa
[root@san01 ~]# mkdir -p aa
#aa目录已存在,但加上-p以后目录重复并不会报错,也不会改变aa目录的内容和属性
==================================================================================

linux 指令继续,rm

作用:remove files or directories 删除文件

语法:rm [选项] 文件名称

选项:

shell基础,    -r recursive 递归的删除目录文件及其内容

    -f force 强制删除,不会提示用户,哪怕此文件并不存在

===================================================================================
#1、-r选项
[root@san01 ~]# ls ./dir1
text1
[root@san01 ~]# rm dir1
rm: cannot remove `dir1': Is a directory      #rm不加-r选项无法删除目录文件
[root@san01 ~]# rm -r dir1
rm: descend into directory `dir1'? y
rm: remove regular empty file `dir1/text1'? y
rm: remove directory `dir1'? y#2、-f选项
[root@san01 ~]# ls dir2
test2
[root@san01 ~]# rm -rf dir2                   #并不会做任何提示,直接删除[root@san01 ~]# ls bad
ls: cannot access bad: No such file or directory
[root@san01 ~]# rm -f bad                     #bad文件并不存在,加上-f选项后不会提示
===================================================================================



centos命令,cp

作用:copy files and directories 复制文件

语法:cp [选项] 源文件/目录 目标文件/目录

选项:

linux运行文件命令、    -r 递归复制目录及其内容

    -p 保留文件时间戳、拥有者、权限等信息

==================================================================================
#1、-r选项
[root@san01 ~]# ls -l dir3
total 0
-rw-r--r--. 1 root root 0 Mar  5 04:34 test3
[root@san01 ~]# cp dir3 dir4
cp: omitting directory `dir3'
[root@san01 ~]# cp -r dir3 dir4
[root@san01 ~]# ls -l dir4
total 0
-rw-r--r--. 1 root root 0 Mar  5 04:35 test3      #可以看出来test3文件的时间戳改变了#2、-p选项
[root@san01 home]# ll /home
total 4
drwx------. 2 nagios nagios 4096 Mar  4 04:36 nagios
[root@san01 home]# cp -rp /home/nagios /tmp/
[root@san01 home]# ll -d /tmp/nagios
drwx------. 2 nagios nagios 4096 Mar  4 04:36 /tmp/nagios  #保留了属主、属组、时间及权限
==================================================================================


mv

linux基础命令。作用:move (rename) files 移动(重命名)文件

语法:mv [选项] 源文件/目录 目标文件/目录

选项:

    -f force 覆盖文件时不提示

====================================================================================
#1、-f选项
[root@san01 dir4]# ll test*
-rw-r--r--. 1 root root 0 Mar  5 04:35 test3
-rw-r--r--. 1 root root 0 Mar  5 04:53 test4
[root@san01 dir4]# mv test3 test4
mv: overwrite `test4'? n                     #因为test4已存在,所以会提示是否覆盖
[root@san01 dir4]# mv -f test3 test4
[root@san01 dir4]# ll test*
-rw-r--r--. 1 root root 0 Mar  5 04:35 test4 #查看结果,test4已被覆盖
====================================================================================

Linux,

文档命令)


touch

shell基本命令的使用。作用:可以修改文件时间参数,当touch目标文件不存在时会创建它。

语法:touch filename

PS:时间参数包含

    atime:accesstime 访问时间;

    mtime:modifytime 修改内容时间;

    ctime:changetime 改变文件权限时间

===============================================================================
#1、修改时间参数
[root@san01 ~]# stat test11       #查看test11文件的状态信息File: `test11'Size: 3               Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d      Inode: 12121       Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2015-03-05 04:44:01.088979285 +0800
Modify: 2015-03-05 04:43:53.537979166 +0800
Change: 2015-03-05 04:43:53.537979166 +0800
[root@san01 ~]# touch test11      #touch一下
[root@san01 ~]# stat test11       #再看一下,发现三个时间改变了吧File: `test11'Size: 3               Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d      Inode: 12121       Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2015-03-05 05:26:11.701977204 +0800
Modify: 2015-03-05 05:26:11.701977204 +0800
Change: 2015-03-05 05:26:11.701977204 +0800#2、创建普通文件
[root@san01 ~]# ll test*
-rw-r--r--. 1 root root 3 Mar  5 05:26 test11
-rw-r--r--. 1 root root 6 Mar  5 04:44 test22
[root@san01 ~]# touch test33
[root@san01 ~]# ll test*
-rw-r--r--. 1 root root 3 Mar  5 05:26 test11
-rw-r--r--. 1 root root 6 Mar  5 04:44 test22
-rw-r--r--. 1 root root 0 Mar  5 05:28 test33
================================================================================

 

cat

作用:查看文档内容并在终端界面输出

语法:cat filename

PS:可以通过cat > file的方式向file中输入键盘上敲打的字符

================================================================================
[root@san01 ~]# cat > test22
hahahaha
good                           #按下ctrl+d退出
[root@san01 ~]# cat test22
hahahaha
good
================================================================================

 

tac

作用:与cat输出的内容顺序上下颠倒

语法:tac filename

 

more

作用:分屏输出文档内容,可向下翻页查看。

语法:more filename

操作方式:按下空格键,向下翻屏

 

less

作用:分屏输出文档内容,可上下翻页或上下翻行查看。

语法:less filename

操作方式:

    按下空格键,向下翻屏;

    按上下箭头翻行; 

    按j键,向下移动一行,按k键,向上移动一行;

    按ctrl+f,向下翻页;

    按ctrl+b,向上翻页;

    按shift+G,前往文档最上面;

    按shift+g,前往文档最下面。

 

head

作用:标准输出文档头部N行

语法:head [选项] filename

选项:

    -n 输出文档头部n行(例如head -n 行数 filename / head -行数 filename)

PS:没有-n参数的话,默认输出前十行。

 

tail

作用:标准输出文档尾部N行

语法:tail [选项] filename

选项:

    -n 输出文档头部n行(例如tail -n 行数 filename / tail -行数 filename)

    -f 动态显示(查看文档时,如果文档内容发生改变,tail的输出会动态改变)

    -F 在-f的基础上增加一个 -retry,不常用。

PS:没有-n参数的话,默认输出后十行。