码迷,mamicode.com
首页 > 系统相关 > 详细

Linux基础命令2:cd、pwd、ls、stat、touch、alias

时间:2016-08-29 20:52:48      阅读:348      评论:0      收藏:0      [点我收藏+]

标签:cd   touch   stat   pwd   


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]...

 

选项:

ls [options] [files_or_dirs]

ls -a  包含隐藏文件

ls -l  显示额外的信息

ls -R  目录递归通过

ls -ld 目录和符号链接信息

ls -1  文件分行显示,每行只显示一个列表

ls -S  按从大到小排序

ls -u  配合-t选项,显示并按atime从新到旧排序

ls -U  不排序按目录存放顺序显示

 

示例:

[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


本文出自 “Linux路上” 博客,请务必保留此出处http://dreamlinuxc.blog.51cto.com/5733156/1844016

Linux基础命令2:cd、pwd、ls、stat、touch、alias

标签:cd   touch   stat   pwd   

原文地址:http://dreamlinuxc.blog.51cto.com/5733156/1844016

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!