linux文件目录类命令(包含修改提示符和修改历史格式)

pwd 显示当前工作目录的绝对路径

pwd:print working directory 打印工作目录

  1. 基本语法
    pwd (功能描述:显示当前工作目录的绝对路径)
  2. 案例实操
    (1)显示当前工作目录的绝对路径
    [root@hadoop101 ~]# pwd
    /root
    

ls 列出目录的内容

ls:list 列出目录内容

  1. 基本语法
    ls [选项] [目录或是文件]
  2. 选项说明
选项功能
-a全部的文件,连同隐藏档( 开头为 . 的文件) 一起列出来(常用)
-l长数据串列出,包含文件的属性与权限等等数据;(常用)
  1. 显示说明
    每行列出的信息依次是: 文件类型与权限 链接数 文件属主 文件属组 文件大小用byte来表示 建立或最近修改的时间 名字
  2. 案例实操
    (1)查看当前目录的所有内容信息
[root@hadoop100 ~]# ls
anaconda-ks.cfg      install.log.syslog  模板  文档  桌面
forlearnsmartd.conf  learndir            视频  下载
install.log          公共的              图片  音乐
[root@hadoop100 ~]# ll
总用量 108
-rw-------. 1 root root  1248 2月  23 08:16 anaconda-ks.cfg
-rw-r--r--. 1 root root  6745 2月  23 14:40 forlearnsmartd.conf
-rw-r--r--. 1 root root 41954 2月  23 08:15 install.log
-rw-r--r--. 1 root root  9154 2月  23 08:11 install.log.syslog
drwxr-xr-x. 3 root root  4096 2月  24 10:39 learndir
drwxr-xr-x. 2 root root  4096 2月  23 00:28 公共的
drwxr-xr-x. 2 root root  4096 2月  23 00:28 模板
drwxr-xr-x. 2 root root  4096 2月  23 00:28 视频
drwxr-xr-x. 2 root root  4096 2月  23 00:28 图片
drwxr-xr-x. 2 root root  4096 2月  23 00:28 文档
drwxr-xr-x. 2 root root  4096 2月  23 00:28 下载
drwxr-xr-x. 2 root root  4096 2月  23 00:28 音乐
drwxr-xr-x. 3 root root  4096 2月  23 18:23 桌面

cd 切换目录

cd:Change Directory切换路径

  1. 基本语法
    cd [参数]
  2. 参数说明
参数功能
cd 绝对路径切换路径
cd相对路径切换路径
cd ~或者cd回到自己的家目录
cd -回到上一次所在目录
cd …回到当前目录的上一级目录
cd -P跳转到实际物理路径,而非快捷方式路径

3.案例实操
(1)使用绝对路径切换到root目录

[root@hadoop101 ~]# cd /root/

(2)使用相对路径切换到“公共的”目录

[root@hadoop101 ~]# cd 公共的/

(3)表示回到自己的家目录,亦即是 /root 这个目录

[root@hadoop101 公共的]# cd ~

(4)cd- 回到上一次所在目录

[root@hadoop101 ~]# cd -

(5)表示回到当前目录的上一级目录,亦即是 “/root/公共的”的上一级目录的意思;

[root@hadoop101 公共的]# cd ..

mkdir 创建一个新的目录

mkdir:Make directory 建立目录

  1. 基本语法
    mkdir [选项] 要创建的目录
  2. 选项说明
选项功能
-p创建多层目录
  1. 案例实操
    (1)创建一个目录
[root@hadoop101 ~]# mkdir xiyou
[root@hadoop101 ~]# mkdir xiyou/mingjie

(2)创建一个多级目录

[root@hadoop101 ~]# mkdir -p xiyou/dssz/meihouwang

rmdir 删除一个空的目录

rmdir:Remove directory 移动目录

  1. 基本语法:
    rmdir 要删除的空目录
  2. 案例实操
    (1)删除一个空的文件夹
[root@hadoop101 ~]# rmdir xiyou/dssz/meihouwang

touch 创建空文件

  1. 基本语法
    touch 文件名称
  2. 案例实操
[root@hadoop101 ~]# touch xiyou/dssz/sunwukong.txt

cp 复制文件或目录

  1. 基本语法
    cp [选项] source dest (功能描述:复制source文件到dest)
  2. 选项说明
选项功能
-r递归复制整个文件夹
  1. 参数说明
参数功能
source源文件
dest目标文件
  1. 经验技巧
    强制覆盖不提示的方法:\cp
  2. 案例实操
    (1)复制文件
 [root@hadoop101 ~]# cp xiyou/dssz/suwukong.txt xiyou/mingjie/

(2)递归复制整个文件夹

 [root@hadoop101 ~]# cp -r xiyou/dssz/ ./
 

rm 移除文件或目录

  1. 基本语法
    rm [选项] deleteFile (功能描述:递归删除目录中所有内容)
  2. 选项说明
选项功能
-r递归删除目录中所有内容
-f强制执行删除操作,而不提示用于进行确认。
-v显示指令的详细执行过程
  1. 案例实操
    (1)删除目录中的内容
[root@hadoop101 ~]# rm xiyou/mingjie/sunwukong.txt

(2)递归删除目录中所有内容

[root@hadoop101 ~]# rm -rf dssz/

mv 移动文件与目录或重命名

  1. 基本语法
    (1)mv oldNameFile newNameFile (功能描述:重命名)
    (2)mv /temp/movefile /targetFolder (功能描述:移动文件)
  2. 案例实操
    (1)重命名
[root@hadoop101 ~]# mv xiyou/dssz/suwukong.txt xiyou/dssz/houge.txt

(2)移动文件

[root@hadoop101 ~]# mv xiyou/dssz/houge.txt ./

cat 查看文件内容

查看文件内容,从第一行开始显示。

  1. 基本语法
    cat [选项] 要查看的文件
  2. 选项说明
选项功能描述
-n显示所有行的行号,包括空行。
  1. 经验技巧
    一般查看比较小的文件,一屏幕能显示全的。
  2. 案例实操
    (1)查看文件内容并显示行号
[atguigu@hadoop101 ~]$ cat -n houge.txt 

more 文件内容分屏查看器

more指令是一个基于VI编辑器的文本过滤器,它以全屏幕的方式按页显示文本文件的内容。more指令中内置了若干快捷键,详见操作说明。

  1. 基本语法
    more 要查看的文件
  2. 操作说明
操作功能说明
空白键 (space)代表向下翻一页;
Enter代表向下翻『一行』;
q代表立刻离开 more ,不再显示该文件内容。
Ctrl+F向下滚动一屏
Ctrl+B返回上一屏
=输出当前行的行号
:f输出文件名和当前行的行号

3.案例实操
(1)采用more查看文件

[root@hadoop101 ~]# more smartd.conf

less 分屏显示文件内容

less指令用来分屏查看文件内容,它的功能与more指令类似,但是比more指令更加强大,支持各种显示终端。less指令在显示文件内容时,并不是一次将整个文件加载之后才显示,而是根据显示需要加载内容,对于显示大型文件具有较高的效率。

  1. 基本语法
    less 要查看的文件
  2. 操作说明
操作功能说明
空白键向下翻动一页;
[pagedown]向下翻动一页
[pageup]向上翻动一页;
/字串向下搜寻『字串』的功能;n:向下查找;N:向上查找;
?字串向上搜寻『字串』的功能;n:向上查找;N:向下查找;
q离开 less 这个程序;

4.案例实操
(1)采用less查看文件

[root@hadoop101 ~]# less smartd.conf

echo

echo输出内容到控制台

  1. 基本语法
    echo [选项] [输出内容]
    选项:
    -e: 支持反斜线控制的字符转换
控制字符作用
\输出\本身
\n换行符
\t制表符,也就是Tab键
  1. 案例实操
[atguigu@hadoop101 ~]$ echo "hello\tworld"
hello\tworld
[atguigu@hadoop101 ~]$ echo -e "hello\tworld"
hello		world

查看当前的shell

[root@hadoop100 ~]# echo $SHELL
/bin/bash

查看shell的类型,直接输入即可执行

[root@hadoop100 ~]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/dash
/bin/tcsh
/bin/csh
[root@hadoop100 ~]# /bin/bash

显示10-20之间的数,+2递增

[root@hadoop100 ~]# echo {10..20..2}
10 12 14 16 18 20

[root@hadoop100 ~]# echo file{a,b,c}
filea fileb filec

笛卡尔积显示

[root@hadoop100 ~]# echo file{a,b,c}.{log,txt}
filea.log filea.txt fileb.log fileb.txt filec.log filec.txt

创建笛卡尔积中的所有文件

[root@hadoop100 ~]# touch file{a,b,c}.{log,txt}

修改提示符

提示符的格式,PS1变量名

[root@hadoop100 ~]# echo $PS1
[\u@\h \W]\$

提示符的格式,你可以在bash中查找:

[root@hadoop100 ~]# man bash

找到这里,查找PROMPTING

       PS1    The  value  of  this  parameter  is expanded (see
              PROMPTING below) and used as the  primary  prompt
              string.  The default value is ‘‘\s-\v\$ ’’.
       PS2    The  value  of this parameter is expanded as with
              PS1 and used as the secondary prompt string.  The
              default is ‘‘> ’’.
       PS3    The value of this parameter is used as the prompt
              for the select command (see SHELL GRAMMAR above).
       PS4    The  value  of this parameter is expanded as with
              PS1 and the value is printed before each  command
              bash  displays  during  an  execution trace.  The
              first character of  PS4  is  replicated  multiple
              times,  as necessary, to indicate multiple levels
              of indirection.  The default is ‘‘+ ’’.
       SHELL  The full pathname to the shell is  kept  in  this
              environment  variable.  If it is not set when the
              shell starts, bash assigns to it the  full  path-
              name of the current user’s login shell.
       TIMEFORMAT
              The  value  of this parameter is used as a format
              string specifying how the timing information  for
              pipelines  prefixed  with  the time reserved word
              should be displayed.  The % character  introduces
              an  escape  sequence  that  is expanded to a time
              value or other information.  The escape sequences
              and  their  meanings  are  as follows; the braces
              denote optional portions.
              %%        A literal %.
              %[p][l]R  The elapsed time in seconds.
/PROMPTING

找到,介绍了格式

PROMPTING
       When  executing interactively, bash displays the primary
       prompt PS1 when it is ready to read a command,  and  the
       secondary  prompt  PS2  when it needs more input to com-
       plete a command.  Bash allows these prompt strings to be
       customized  by  inserting  a number of backslash-escaped
       special characters that are decoded as follows:
              \a     an ASCII bell character (07)
              \d     the date in "Weekday  Month  Date"  format
                     (e.g., "Tue May 26")
              \D{format}
                     the  format  is  passed to strftime(3) and
                     the result is  inserted  into  the  prompt
                     string;  an  empty  format  results  in  a
                     locale-specific time representation.   The
                     braces are required
              \e     an ASCII escape character (033)
              \h     the hostname up to the first ‘.’
              \H     the hostname
              \j     the  number  of  jobs currently managed by
                     the shell
              \l     the  basename  of  the  shell’s   terminal
                     device name
              \n     newline
              \r     carriage return
              \s     the  name of the shell, the basename of $0
                     (the portion following the final slash)
              \t     the current time in 24-hour HH:MM:SS  for-
                     mat
:

PS1="{}":修改提示符

/etc/profile.d/env.sh文件中
加入:PS1="{}"
:永久修改提示符,写入到文件中;

head 显示文件头部内容

head用于显示文件的开头部分内容,默认情况下head指令显示文件的前10行内容。

  1. 基本语法
    head 文件 (功能描述:查看文件头10行内容)
    head -n 5 文件 (功能描述:查看文件头5行内容,5可以是任意行数)
  2. 选项说明
选项功能
-n <行数>指定显示头部内容的行数
  1. 案例实操
    (1)查看文件的头2行
[root@hadoop101 ~]# head -n 2 smartd.conf

tail 输出文件尾部内容

tail用于输出文件中尾部的内容,默认情况下tail指令显示文件的后10行内容。

  1. 基本语法
    (1)tail 文件 (功能描述:查看文件后10行内容)
    (2)tail -n 5 文件 (功能描述:查看文件后5行内容,5可以是任意行数)
    (3)tail -f 文件 (功能描述:实时追踪该文档的所有更新)
  2. 选项说明
选项功能
-n<行数>输出文件尾部n行内容
-f显示文件最新追加的内容,监视文件变化

3.案例实操
(1)查看文件尾1行内容

[root@hadoop101 ~]# tail -n 1 smartd.conf 

(2)实时追踪该档的所有更新

[root@hadoop101 ~]# tail -f houge.txt

> 覆盖 和 >> 追加

  1. 基本语法
    (1)ll >文件 (功能描述:列表的内容写入文件a.txt中(覆盖写))
    (2)ll >>文件 (功能描述:列表的内容追加到文件aa.txt的末尾)
    (3)cat 文件1 > 文件2 (功能描述:将文件1的内容覆盖到文件2)
    (4)echo “内容” >> 文件
  2. 案例实操
    (1)将ls查看信息写入到文件中
[root@hadoop101 ~]# ls -l>houge.txt

(2)将ls查看信息追加到文件中

[root@hadoop101 ~]# ls -l>>houge.txt

(3)采用echo将hello单词追加到文件中

[root@hadoop101 ~]# echo hello>>houge.txt

ln 软链接

软链接也成为符号链接,类似于windows里的快捷方式,有自己的数据块,主要存放了链接其他文件的路径。

  1. 基本语法
    ln -s [原文件或目录] [软链接名] (功能描述:给原文件创建一个软链接)
  2. 经验技巧
    删除软链接: rm -rf 软链接名,而不是rm -rf 软链接名/
    查询:通过ll就可以查看,列表属性第1位是l,尾部会有位置指向。
  3. 案例实操
    (1)创建软连接
[root@hadoop101 ~]# mv houge.txt xiyou/dssz/
[root@hadoop101 ~]# ln -s xiyou/dssz/houge.txt ./houzi
[root@hadoop101 ~]# ll
lrwxrwxrwx. 1 root    root      20 6月  17 12:56 houzi -> xiyou/dssz/houge.txt

(2)删除软连接

[root@hadoop101 ~]# rm -rf houzi

(3)进入软连接实际物理路径

[root@hadoop101 ~]# ln -s xiyou/dssz/ ./dssz
[root@hadoop101 ~]# cd -P dssz/

history 查看已经执行过历史命令

  1. 基本语法
    history (功能描述:查看已经执行过历史命令)
  2. 案例实操
    (1)查看已经执行过的历史命令
[root@hadoop101 test1]# history

补充:
history:列出所有执行的命令,只记录最近的一千条历史,在HISTSIZE变量中
HISTSIZE 变量在/etc/prifile中,可以修改。

命令执行
!+历史命令编号重新执行命令
!-编号执行倒数第编号条命令
ctrl+p+回车执行前一条命令
!+字符串重复前一个以字符串开头的命令
!?+字符串包含字符串的命令
!+字符串:p仅打印,并不执行
!$:p打印输出!$(上一条命令的最后一个参数)的内容
!*:p打印输出!*(上一条命令的所有参数)的内容
^+字符串删除上一条命令中的第一个字符串
^+字符串1+(shift+6)字符串2将上一条命令的第一个字符串1替换为字符串2
!:gs/字符串/字符串2将上一条命令中所有字符串1替换为字符串2
history -d 12删除编号为12的命令历史
history -a将内存中的命令写到文件中
history -r将文件中的内容写到内存中
history -w将当前新命令追加到文件中
history -n将新增加的历史增加到历史列表中
history -p +“command” “command”执行命令但不写到历史中
history -s + “string”伪造历史,但并不执行

执行时历史命令放在内存中,退出系统后,将内存中历史命令写进文件中,命令存放在磁盘中用户家目录中的.bash_history隐藏文件中,当用户登录时,从此文件中加载到内存中,而执行history -c后是清除内存中的命令历史,从而用户退出时上一次的命令历史就被清掉,无法加载到文件中去

1)rm .bash_history
2)history -c
:删除历史文件,同时清除内存中的历史记录,执行顺序很重要,否则会暴露删除文件命令,在重新生成的.bash_history文件中。

修改历史格式
修改历史格式,想要永久更改,需要写入配置文件/etc/profile.d/env.sh文件中
命令历史相关的环境变量:
HISTSIZE:命令历史记录的条数
HISTFILE:指定历史文件,默认为~/.bash_history
HISTFILESIZE:命令历史文件记录历史的条数
HISTTIMEFORMAT="%F %T ":显示时间
HISTIGNORE="STR1:STR2*…"忽略str1命令,str2开头的历史
HISTCONTROL=
ignorespace:忽略所有以空格开头的命令
ignoredups:忽略重复的命令,连续相同为重复
ignoreboth:相当于ignoredups和ignorespace的组合
erasedups:删除重复命令,可以不连续的
系统的配置文件/etc/profile文件中

例如:HISTTIMEFORMAT="%F %T "修改历史格式

大部分引用自尚硅谷课件

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值