cd:改变目录
语法:
cd [-L|[-P [-e]]] [dir]
选项:
-p 如果要切换到的目标目录是一个符号连接,直接切换到符号连接指向的目标目录
-L 如果要切换的目标目录是一个符号的连接,直接切换到字符连接名代表的目录,而非符号连接所指向的目标目录。 - 当仅实用"-"一个选项时,当前工作目录将被切换到环境变量"OLDPWD"所表示的目录。
用法:
cd 进入用户主目录
cd ~ 进入用户主目录
cd ~- 进入用户主目录
cd $HOME 进入用户主目录
cd ~用户名/ 进入某用户的主目录
cd ~+ 当前目录
cd . 当前目录
cd - 返回进入此目录之前所在的目录
cd .. 返回上级目录(若当前目录为“/“,则执行完后还在“/";".."为上级目录的意思)
cd ../.. 返回上两级目录
cd !$ 把上个命令的参数作为cd参数使用
相关的环境变量:
PWD: 保存了当前目录路径
OLDPWD: 上一次所在目录路径
示例:
使用绝对或相对路径:
[root@localhost ~]# cd /boot/ #切换至boot目录
[root@localhost boot]# cd ../testdir/ #切换至testdir目录
[root@localhost testdir]# cd - #切换至上一次目录
/boot
[root@localhost boot]# cd ~/ #切换到用户主目录
pwd:显示当前工作目录的绝对路径
语法:
pwd [ -L | -P ]
说明:
以绝对路径的方式显示用户当前工作目录。命令将当前目录的全路径名称(从根目录)写入标准输出。全部目录使用/分隔。第一个/表示根目录,最后一个目录是当前目录。执行pwd命令可得知目前所在的工作目录的绝对路径名称。
选项:
-L:显示当前目录的绝对路径
-P:显示当前目录的物理路径
示例:
[root@localhost ~]# pwd
/root
[root@localhost bin]# pwd
/bin
[root@localhost bin]# pwd -P #显示当前目录的物理路径
/usr/bin
ls:列出目录内容
语法:
ls [OPTION]... [FILE]...
选项:
-a:显示指定目录下所有子目录与文件,包括隐藏文件
-A:显示目录下所有子目录与文件,包括隐藏文件,但不列出“.”和“.”
-c:配合-lt,根据ctime排序并显示ctime
-d:如果参数是目录,只显示其名称而不是显示其下的各文件和子目录
-F:显示文件类型
-i:在输出的第一列显示文件的inode号
-l:以长格式来显示文件的详细信息
-r:排序时保留顺序
-t:根据修改时间排序
-s:以块数形式显示每个文件分配的尺寸
-S:根据文件大小排序
补充说明:
不同类型的文件在Linux系统中使用不同的符号表示
-:普通文件
d:目录
l:符号链接文件
p:管道文件
s:socket文件
b:块设备文件
c:字符设备文件
不同类型的文件在Linux系统中具有不同的颜色
绿色:可执行文件
红色:压缩文件
深蓝色:目录
×××:设备文件,包括块设备文件和字符设备文件
白色:一般文件
灰色:其它的一些文件
红色闪烁:代表链接的文件有问题
文件类型的标记:
/ :表示目录
@ :表示符号连接文件
* :可以执行文件
| :管道文件
= :socket文件
没有标记:普通文件
示例:
[root@localhost network-scripts]# ll ifcfg-eno16777736
-rw-r--r--. 1 root root 347 Sep 2 07:26 ifcfg-eno16777736
第1字段:-,文件类型,这里是指普通文件
第2字段:rw-r--r--. ,文件权限
第3字段:1,文件的链接数
第4字段:root:root,文件的用户所有者
第5字段:root:root,文件的组群所有者
第6字段:347:文件长度(也就是文件大小,不是文件的磁盘占用量
第7字段:Sep 2 07:26:文件的修改时间(mtime),或者是文件的最后访问时间(atime)
第8字段:ifcfg-eno16777736:文件名称
[root@localhost ~]# ls -d .* #显示当前目录下的所有文件,包括隐藏文件
. .bash_profile .dbus .ftype2.sh.swp Music software .vimrc
.. .bashrc Desktop function Pictures .ssh .whilefile.sh.swp
.211.sh.swp bin Documents .history Public .tcshrc .xauth9DWaTu
backup .cache Downloads .ICEauthority repo Videos .Xauthority
.bash_history .config .elinks .lesshst select .viminfo
.bash_logout .cshrc .esd_auth .local Server .viminfo.tmp
[root@localhost ~]# l. #显示当前目录下隐藏文件
. .bash_profile .dbus .ftype2.sh.swp Music software .vimrc
.. .bashrc Desktop function Pictures .ssh .whilefile.sh.swp
.211.sh.swp bin Documents .history Public .tcshrc .xauth9DWaTu
backup .cache Downloads .ICEauthority repo Videos .Xauthority
.bash_history .config .elinks .lesshst select .viminfo
.bash_logout .cshrc .esd_auth .local Server .viminfo.tmp
[root@localhost ~]# alias l. #l.是ls的别名,这种方式不能显示指定目录的隐藏文件
alias l.='ls -d .* --color=auto'
[root@localhost ~]# ls -d .[^.]* #只显示当前目录下的隐藏文件,以“.”开头文件
.211.sh.swp .bashrc .dbus .history .ssh .vimrc
.bash_history .cache .elinks .ICEauthority .tcshrc .whilefile.sh.swp
.bash_logout .config .esd_auth .lesshst .viminfo .xauth9DWaTu
.bash_profile .cshrc .ftype2.sh.swp .local .viminfo.tmp .Xauthority
[root@localhost testdir]# ls -d /root/.[^.]* #显示指定目录下的隐藏文件
/root/.211.sh.swp /root/.config /root/.history /root/.viminfo
/root/.bash_history /root/.cshrc /root/.ICEauthority /root/.viminfo.tmp
/root/.bash_logout /root/.dbus /root/.lesshst /root/.vimrc
/root/.bash_profile /root/.elinks /root/.local /root/.whilefile.sh.swp
/root/.bashrc /root/.esd_auth /root/.ssh /root/.xauth9DWaTu
/root/.cache /root/.ftype2.sh.swp /root/.tcshrc /root/.Xauthority
[root@localhost boot]# ls -a /root/ #显示roo目录的所有文件
. .bash_profile .dbus .ftype2.sh.swp Music software .vimrc
.. .bashrc Desktop function Pictures .ssh .whilefile.sh.swp
.211.sh.swp bin Documents .history Public .tcshrc .xauth9DWaTu
backup .cache Downloads .ICEauthority repo Videos .Xauthority
.bash_history .config .elinks .lesshst select .viminfo
.bash_logout .cshrc .esd_auth .local Server .viminfo.tmp
[root@localhost ~]# ls -d ./*/ #只显示当前目录下的目录,可以指定任意目录
./backup/ ./Desktop/ ./Downloads/ ./Music/ ./Public/ ./select/ ./software/
./bin/ ./Documents/ ./function/ ./Pictures/ ./repo/ ./Server/ ./Videos/
[root@localhost ~]# ls -d */
backup/ Desktop/ Downloads/ Music/ Public/ select/ software/
bin/ Documents/ function/ Pictures/ repo/ Server/ Videos/
[root@localhost testdir]# ls -d */ #只显示目录,如果目录下面没有目录会报错
ls: cannot access */: No such file or directory
stat:查看文件状态
语法:
stat [OPTION]... FILE...
说明:
用于显示文件的状态信息。stat命令的输出信息比ls命令的输出信息要更详细。
FILE可以指定要显示信息的普通文件或者文件系统对应的设备文件名。
选项:
-L:支持符号连接;
-f:显示文件系统状态而非文件状态;
-t:以简洁方式输出信息;
三个时间戳:
access time:访问时间, atime,读取文件内容
modify time: 修改时间, mtime,改变文件内容(数据)
change time: 改变时间, ctime,元数据发生改变
示例:
[root@localhost testdir]# ls -l abc
-rw-r--r--. 1 root root 30 Aug 29 10:10 abc
[root@localhost testdir]# stat abc
File: ‘abc’
Size: 30 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051dInode: 131 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2016-08-29 10:05:10.072157383 +0800
Modify: 2016-08-29 10:10:32.580164597 +0800
Change: 2016-08-29 10:10:32.580164597 +0800
Birth: -
[root@localhost testdir]# stat -L abc
File: ‘abc’
Size: 30 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051dInode: 131 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2016-08-29 10:05:10.072157383 +0800
Modify: 2016-08-29 10:10:32.580164597 +0800
Change: 2016-08-29 10:10:32.580164597 +0800
Birth: -
[root@localhost testdir]# stat -f abc
File: "abc"
ID: 80300000000 Namelen: 255 Type: xfs
Block size: 4096 Fundamental block size: 4096
Blocks: Total: 50347 Free: 47741 Available: 47741
Inodes: Total: 204800 Free: 204795
[root@localhost testdir]# stat -t abc
abc 30 8 81a4 0 0 803 131 1 0 0 1472436310 1472436632 1472436632 0 4096 unconfined_u:object_r:etc_runtime_t:s0
touch:创建空文件和刷新时间
语法:
touch [OPTION]... FILE...
说明:
一是用于更改文件或目录的日期时间,如不指定日期和时间,则以当时的时间为准,它们的数据将原封不动地保留下来;二是用来创建新的空文件。
选项:
-a或--time=atime或--time=access或--time=use :只更改存取时间
-c或--no-create :不建立任何文件
-d:<时间日期> 使用指定的日期时间,而非现在的时间
-f:此参数将忽略不予处理,仅负责解决BSD版本touch指令的兼容性问题
-m或--time=mtime或--time=modify :只更该变动时间;
-r <参考文件或目录> 或--refernece=<参考文件或目录> :把指定文件或目录的日期时间,统统设成和参考文件或目录的日期时间相同
-t <日期时间> :使用指定的日期时间,而非现在的时间
示例:
[root@localhost testdir]# ll abc
-rw-r--r--. 1 root root 30 Aug 29 10:10 abc
[root@localhost testdir]# stat abc
File: ‘abc’
Size: 30 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051dInode: 131 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2016-08-29 10:05:10.072157383 +0800
Modify: 2016-08-29 10:10:32.580164597 +0800
Change: 2016-08-29 10:10:32.580164597 +0800
Birth: -
[root@localhost testdir]# touch -a abc #修改atime时间
[root@localhost testdir]# stat abc
File: ‘abc’
Size: 30 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051dInode: 131 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2016-08-29 10:25:38.690184867 +0800
Modify: 2016-08-29 10:10:32.580164597 +0800
Change: 2016-08-29 10:25:38.690184867 +0800
Birth: -
[root@localhost testdir]# touch -m abc #修改mtime时间
[root@localhost testdir]# stat abc
File: ‘abc’
Size: 30 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051dInode: 131 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2016-08-29 10:25:38.690184867 +0800
Modify: 2016-08-29 10:28:15.079188365 +0800
Change: 2016-08-29 10:28:15.079188365 +0800
Birth: -
[root@localhost testdir]# touch -r abc stu #修改同步abc文件的时间与stu一致
[root@localhost testdir]# stat abc stu
File: ‘abc’
Size: 30 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051dInode: 131 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2016-08-29 10:30:45.932191740 +0800
Modify: 2016-08-29 10:32:07.551193566 +0800
Change: 2016-08-29 10:32:07.551193566 +0800
Birth: -
File: ‘stu’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051dInode: 132 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2016-08-29 10:30:45.932191740 +0800
Modify: 2016-08-29 10:32:07.551193566 +0800
Change: 2016-08-29 10:33:18.887195162 +0800
Birth: -
[root@localhost testdir]# touch -t 201606061200.30 file1 #创建文件时,指定日期时间
[root@localhost testdir]# stat file1
File: ‘file1’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051dInode: 134 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2016-06-06 12:00:30.000000000 +0800
Modify: 2016-06-06 12:00:30.000000000 +0800
Change: 2016-08-29 10:49:48.336217295 +0800
Birth: -
[root@localhost testdir]# touch -d 2008/08/08 1234
或者
[root@localhost testdir]# touch -d 08/08/2008 1234
[root@localhost testdir]# stat 1234
File: ‘1234’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051dInode: 142 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2008-08-08 00:00:00.000000000 +0800
Modify: 2008-08-08 00:00:00.000000000 +0800
Change: 2016-08-29 10:57:18.461227365 +0800
Birth: -
alias:别名
语法:
alias [-p] [name[=value] ... ]
说明:
alias命令用来设置指令的别名。我们可以使用该命令可以将一些较长的命令进行简化。使用alias时,用户必须使用单引号''将原来的命令引起来,防止特殊字符导致错误。
在命令行中定义的别名,仅对当前shell进程有效
如果想永久有效,要定义在配置文件中
仅对当前用户: ~/.bashrc
对所有用户有效: /etc/bashrc
编辑配置给出的新配置不会立即生效;
bash进程重新读取配置文件:
source /path/to/config_file
. /path/to/config_file
撤消别名: unalias
unalias [-a] name [name ...]
如果别名同原命令同名,如果要执行原命令,可使用
"\COMMAND"
'COMMAND'
/PATH/COMMAND:外部命令
选项:
-p:打印已经设置的命令别名。
示例:
[root@localhost ~]# alias #显示当前系统所有设置的别名
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias vi='vim'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@localhost testdir]# alias baketc="\cp -r /etc ./etc-`date +%F`"
[root@localhost testdir]# alias baketc
alias baketc='\cp -r /etc ./etc-2016-08-29'
[root@localhost ~]# baketc #定义别名baketc,执行baketc相当于执行上面命令
[root@localhost ~]# alias baketc
alias baketc='\cp -r /etc ./etc-2016-08-29'
[root@localhost ~]# unalias baketc #使用unalias删除设置的别名
[root@localhost ~]# alias baketc
-bash: alias: baketc: not found