Linux常用文件目录指令(实操)

Linux文件目录指令

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

[root@rootylq ~]# cd /home/ylq
[root@rootylq ylq]# pwd
/home/ylq
绝对路径vs相对路径

绝对路径:从根目录下开始访问

相对路径:从当前目录下开始访问

显示文件 ls

-a 显示当前目录所有文件和目录,包括隐藏文件

[root@rootylq ylq]# ls -a
.  ..  .bash_history  .bash_logout  .bash_profile  .bashrc  hello.cpp

-l 以列表方式显示信息

[root@rootylq ylq]# ls -l
total 4
-rw-r--r-- 1 root root 87 Jan  4 17:49 hello.cpp

-la 组合以列表方式显示所有信息

[root@rootylq ylq]# ls -la
total 28
drwx------  2 ylq  ylq  4096 Jan  4 17:49 .
drwxr-xr-x. 4 root root 4096 Jan  4 15:50 ..
-rw-------  1 ylq  ylq    82 Jan  4 15:11 .bash_history
-rw-r--r--  1 ylq  ylq    18 Nov 25  2021 .bash_logout
-rw-r--r--  1 ylq  ylq   193 Nov 25  2021 .bash_profile
-rw-r--r--  1 ylq  ylq   231 Nov 25  2021 .bashrc
-rw-r--r--  1 root root   87 Jan  4 17:49 hello.cpp

切换目录 cd

  • cd ~ 或cd 回到自己家目录
  • cd … 回到当前目录的上一层目录
  • cd / 回到根目录
[root@rootylq ylq]# cd
[root@rootylq ~]# cd /
[root@rootylq /]# cd ~
[root@rootylq ~]# 
[root@rootylq ylq]# cd ../../root
[root@rootylq ~]# pwd
/root

创建目录 mkdir

创建一个目录
[root@rootylq ylq]# mkdir arc
[root@rootylq ylq]# ls
arc  hello.cpp

创建多级目录
[root@rootylq ylq]# mkdir /home/ylq/hello/arc
mkdir: cannot create directory ‘/home/ylq/hello/arc’: No such file or directory
[root@rootylq ylq]# mkdir -p /home/ylq/hello/arc
[root@rootylq ylq]# ls
arc  hello  hello.cpp

删除空目录 rmdir

rmdir 删除的是空目录,如果目录下有内容时将无法删除

tips: 如果需要删除非空目录,需要使用rm-rf 删除要删除的目录

[root@rootylq ylq]# rmdir /home/ylq/hello
rmdir: failed to remove ‘/home/ylq/hello’: Directory not empty
[root@rootylq ylq]# rmdir /home/ylq/hello/arc

创建空文件 touch

[root@rootylq ylq]# touch nihao.txt
[root@rootylq ylq]# ls
hello.cpp  nihao.txt

拷贝指令 cp

cp [选项] source dest

常用选项:

-r 递归复制整个文件夹

拷贝单个文件
[root@rootylq ylq]# ls
bbb  hello.cpp  nihao.txt
[root@rootylq ylq]# cp hello.cpp bbb/
[root@rootylq ylq]# cd ls
-bash: cd: ls: No such file or directory
[root@rootylq ylq]# cd bbb
[root@rootylq bbb]# ls
hello.cpp

递归复制整个文件夹
[root@rootylq ylq]# mkdir -p ylq/test/example
[root@rootylq ylq]# ls
bbb  hello.cpp  nihao.txt  ylq
[root@rootylq ylq]# cp -r bbb ylq/test/example
[root@rootylq ylq]# cd ylq/test/example
[root@rootylq example]# ls
bbb
[root@rootylq example]# cd bbb
[root@rootylq bbb]# ls
hello.cpp  ylq

强行复制不接受覆盖提醒 \cp -r bbb ylq\test\example

移除文件或目录 rm

rm [选项] 要删除的文件或目录

-r 递归删除整个文件夹

-f 强制删除但不提醒

[root@rootylq ylq]# ls
bbb  hello.cpp  nihao.txt
[root@rootylq ylq]# rm nihao.txt
rm: remove regular empty file ‘nihao.txt’? y
[root@rootylq ylq]# rm -rf bbb
[root@rootylq ylq]# ls
hello.cpp

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

重命名

如果souce和dst 在同一级目录,则被判定为重命名

[ylq@rootylq ~]$ ls
hello.cpp
[ylq@rootylq ~]$ move hello.cpp arc.cpp
-bash: move: command not found
[ylq@rootylq ~]$ mv hello.cpp arc.cpp
[ylq@rootylq ~]$ ls
arc.cpp

移动文件

若不在同级目录,则视为移动文件

[ylq@rootylq ~]$ mv arc.cpp test/
[ylq@rootylq ~]$ ls
test
[ylq@rootylq ~]$ ll test/
total 4
-rw-r--r-- 1 root root 87 Jan  4 17:49 arc.cpp

移动并且重命名文件
[ylq@rootylq ~]$ mv test/arc.cpp hello.cpp
[ylq@rootylq ~]$ ls
hello.cpp  test
[ylq@rootylq ~]$ ll test/
total 0

查看文件内容 cat

cat只能浏览文件,但并不能修改文件,为了浏览方便,一般将会带上 管道命令|more

-n 显示行号

[ylq@rootylq opt]$ cat /etc/profile -n
     1	# /etc/profile
     2	

管道命令|more 将前一个结果交给下一个命令进行执行

cat -n /etc/profile |more 可进行指令交互

全屏查看文件more 指令

more指令是一个基于VI编辑器的文本过滤器,其以全屏幕的方式按页显示文本文件的内容。more指令中内置了若干快捷键(交互指令)

  • space 代表向下翻一页
  • enter 代表向下翻一行
  • q 立刻离开more,不再显示文件内容
  • Ctrl+F 向下滚动一屏
  • Ctrl+B 向上滚动一屏
  • = 输出当前行号
  • :f 输出文件名和当前行的行号
[ylq@rootylq opt]$ more  /etc/profile

分屏查看文件 Less 指令

less指令分屏查看文件内容,功能与more类似,但是相比more指令更加强大,支持各种显示终端

less指令在显示文件内容时,并不是一次将整个文件加载后显示,而是根据显示需要加载内容,对于显示大型文件具有较高的效率

  • space 向下翻动一页
  • pagedown 向下翻页
  • pageup 向上翻页
  • /字符 向下搜索字符 n:向下查找 N:向上查找
  • ?字符 向上搜索字符 n:向上查找 N:向下查找
  • q 离开less程序

输出内容到控制台 echo

#输出环境变量
[ylq@rootylq opt]$ echo $PATH
/root/jdk1.8.0_351/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ylq/.local/bin:/home/ylq/bin
[ylq@rootylq opt]$ echo $HOSTNAMe

#输出主机名
[ylq@rootylq opt]$ echo $HOSTNAME
rootylq
#打印字符串
[ylq@rootylq opt]$ echo "hellpworld"
hellpworld

显示文件的开头部分 head

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

[ylq@rootylq opt]$ head /etc/profile
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

显示文件头指定行
[ylq@rootylq opt]$ head -n 5 /etc/profile
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

显示文件尾部内容 tail

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

[ylq@rootylq opt]$ tail /etc/profile
done

unset i
unset -f pathmunge

export JAVA_HOME=/root/jdk1.8.0_351
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JRE_HOME=$JAVA_HOME/jre 

显示文件尾指定行
[ylq@rootylq opt]$ tail -n 5 /etc/profile
export JAVA_HOME=/root/jdk1.8.0_351
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JRE_HOME=$JAVA_HOME/jre 

显示文件的所有更新(实时监控)
[ylq@rootylq ~]$ tail -f hello.cpp 

输出重定向 > 和 追加 >>

“>” 相当于覆盖指定文件的内容

”>>“ 相当于将内容追加到文件的末尾

 echo "cout<<"yes"" >>hello.cpp
# ls -la >hello.cpp
[root@rootylq ylq]# cal >> /home/mycal
[root@rootylq ylq]# ls
hello.cpp  mycal  opt
[root@rootylq ylq]# cd ..
[root@rootylq home]# ls
arc  mycal  ylq
[root@rootylq home]# cat mycal
    January 2023    
Su Mo Tu We Th Fr Sa
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31

软连接/符号连接 ln

软连接也称为符号连接,类似与window中的快捷方式,主要存放了连接其他文件的信息

类似与快捷方式

在home目录创建软连接连接到root目录
[root@rootylq home]# clear
[root@rootylq home]# ln -s /root /home/myroot
[root@rootylq home]# ls
arc  mycal  myroot  ylq

删除软连接
[root@rootylq home]# rm /home/myroot
rm: remove symbolic link ‘/home/myroot’? y
[root@rootylq home]# ls
arc  mycal  ylq

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

查看已经执行过的历史命令,也可以执行历史命令

history
执行曾经执行过的指令
[root@rootylq home]# !6
ls /
bin  boot  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var  www
[root@rootylq home]# !569
ls
arc  mycal  ylq

查找指令

find

find指令将从指定目录下递归向下遍历其各个子目录,将满足条件的文件或者目录显示在终端

find [搜索范围] [选项]

  • -name<查询方式> 按照指定的文件名查找文件
  • -user<用户名> 查找属于指定用户名的所有文件
  • -size<文件大小> 按照指定文件大小查找文件
    • ”+“大于
    • ”-“小于
    • ”n“等于
[root@rootylq home]# find /home -name hello.cpp
/home/ylq/hello.cpp
[root@rootylq home]# find /home -name *.txt
[root@rootylq home]# find /home -name *.cpp
/home/ylq/hello.cpp
[root@rootylq home]# find /opt -usr nobody
find: unknown predicate `-usr'
[root@rootylq home]# find /opt -user nobody
[root@rootylq home]# find /opt -user nobady
find: ‘nobady’ is not the name of a known user
[root@rootylq home]# find /opt -user ylq
[root@rootylq home]# find /opt -user root |more
/opt
/opt/threatbook
/opt/threatbook/OneAV
/opt/threatbook/OneAV/oneav

[root@rootylq home]# find /root -size +200k|more
locate

locate指令可以快速定位文件路径,locate指令利用实现建立的系统中所有文件名称及路径的locate数据库实现快速定位给定的文件。Locate指令无需遍历整个文件系统,查询速度较快,为了保证查询结果的准确度,管理员必须定期更新locate时刻

  • 由于locate指令基于数据库进行查询,所以第一次运行前,必须使用updatedb指定创建locate数据库
[root@rootylq ~]# updatedb
[root@rootylq ~]# locate hello.cpp
/home/ylq/hello.cpp

which

可以查看某个指令在那个目录下

[root@rootylq ~]# clear
[root@rootylq ~]# which ls
alias ls='ls --color=auto'
	/usr/bin/ls
[root@rootylq ~]# which reboot
/usr/sbin/reboot
[root@rootylq ~]# which updatedb
/usr/bin/updatedb

grep指令和管道符号|

grep指令实现过滤查找,|表示将前一个命令的处理结果输出传递给后面的命令处理

  • -n 显示匹配行及行号
  • -i 忽略字母大小写
[ylq@rootylq ~]$ cat hello.cpp | grep "cout"
	cout<<"hello,world!"<<endl;
cout<<yes
[ylq@rootylq ~]$ cat hello.cpp | grep -n -i  "cout"
8:	cout<<"hello,world!"<<endl;
10:cout<<yes
[ylq@rootylq ~]$ 


[ylq@rootylq ~]$ grep -n "yes" /home/ylq/hello.cpp 
10:cout<<yes

压缩与解压

gzip/gunzip

gzip 用于压缩文件

gunzip 用于解压文件

[ylq@rootylq ~]$ clear
[ylq@rootylq ~]$ gzip hello.cpp
[ylq@rootylq ~]$ ls
hello.cpp.gz
[ylq@rootylq ~]$ ll
total 4
-rw-r--r-- 1 ylq ylq 119 Jan  5 13:24 hello.cpp.gz
[ylq@rootylq ~]$ gunzip hello.cpp.gz 
[ylq@rootylq ~]$ ll
total 4
-rw-r--r-- 1 ylq ylq 97 Jan  5 13:24 hello.cpp
[ylq@rootylq ~]$ 

zip/unzip 指令

zip 用于压缩文件 unzip 用于解压文件 在项目打包发布很有用

zip

  • -r 递归压缩,即压缩目录

unzip

  • -d<目录> 指定解压后文件的存放目录
[ylq@rootylq ~]$ ls
hello.cpp  proj
[ylq@rootylq ~]$ zip -r myhome.zip /home/
  adding: home/ (stored 0%)
  adding: home/ylq/ (stored 0%)
  adding: home/ylq/.lesshst (stored 0%)
  adding: home/ylq/.bash_logout (stored 0%)
  adding: home/ylq/.bash_history (deflated 30%)
  adding: home/ylq/.bashrc (deflated 23%)
  adding: home/ylq/hello.cpp (deflated 6%)
  adding: home/ylq/.bash_profile (deflated 21%)
  adding: home/ylq/proj/ (stored 0%)
  adding: home/ylq/proj/hello.cpp (deflated 6%)
  adding: home/arc/ (stored 0%)
  adding: home/mycal (deflated 61%)
[ylq@rootylq ~]$ ls
hello.cpp  myhome.zip  proj
[ylq@rootylq ~]$ unzip -d /home/ylq/proj myhome.zip
Archive:  myhome.zip
   creating: /home/ylq/proj/home/
   creating: /home/ylq/proj/home/ylq/
 extracting: /home/ylq/proj/home/ylq/.lesshst  
 extracting: /home/ylq/proj/home/ylq/.bash_logout  
  inflating: /home/ylq/proj/home/ylq/.bash_history  
  inflating: /home/ylq/proj/home/ylq/.bashrc  
  inflating: /home/ylq/proj/home/ylq/hello.cpp  
  inflating: /home/ylq/proj/home/ylq/.bash_profile  
   creating: /home/ylq/proj/home/ylq/proj/
  inflating: /home/ylq/proj/home/ylq/proj/hello.cpp  
   creating: /home/ylq/proj/home/arc/
  inflating: /home/ylq/proj/home/mycal  
[ylq@rootylq ~]$ cd proj
[ylq@rootylq proj]$ ll
total 8
-rw-r--r-- 1 ylq ylq   97 Jan  5 15:53 hello.cpp
drwxr-xr-x 4 ylq ylq 4096 Jan  5 13:43 home
[ylq@rootylq proj]$ 

tar 指令

tar 指令,打包指令,最后打包后的文件将是 .tar.gz 文件

  • -c 产生.tar打包文件
  • -v 显示详细信息
  • -f 指定压缩后的文件名
  • -z 打包同时压缩
  • -x 解包.tar文件
压缩tar.gz文件
[ylq@rootylq ~]$ tar -zcvf pc.tar.gz hello.cpp pig.txt
hello.cpp
pig.txt
[ylq@rootylq ~]$ ls
hello.cpp  myhome.zip  pc.tar.gz  pig.txt  proj
压缩目录
[ylq@rootylq ~]$ tar -zcvf myhome.tar.gz /home/ylq

解压tar.gz文件到指定位置
[ylq@rootylq ~]$ tar -zxvf pc.tar.gz -C /home/ylq/proj
hello.cpp
pig.txt
[ylq@rootylq ~]$ cd proj
[ylq@rootylq proj]$ ls
hello.cpp  home  pig.txt

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux中,常用的替换文件指令有以下几个: 1. cp命令:cp命令可以用于复制文件或目录。使用该命令可以将一个文件复制到另一个位置,如果目标文件已存在,则会覆盖目标文件。例如,将一个文件从源路径复制到目标路径:cp 源路径 目标路径。 2. mv命令:mv命令可以用于移动文件或目录,也可以用于重命名文件或目录。使用该命令可以将一个文件移动到指定目录下,或者将一个文件重命名。例如,将文件从源路径移动到目标路径:mv 源路径 目标路径。 3. sed命令:sed命令是一个流编辑器,可以用于文件的替换操作。它可以根据指定的规则对文本进行替换、删除或者插入操作。例如,将文件中的某个字符替换为另一个字符:sed 's/原字符/新字符/g' 文件名。 4. awk命令:awk命令是一个文本处理工具,也可以用于替换文件中的内容。通过指定要替换的内容和替换后的内容,awk可以对文件进行替换操作。例如,将文件中的某个字段替换为另一个字段:awk '{gsub("原字段","新字段")}' 文件名。 5. grep命令:grep命令可以根据指定的模式查找文件中的匹配项,也可以通过替换选项来替换所有匹配的内容。例如,将文件中所有匹配的字符串替换为另一个字符串:grep -rl '要替换的字符串' 目录路径 | xargs sed -i 's/要替换的字符串/替换后的字符串/g'。 总之,在Linux中,通过上述几个常用的命令,可以方便地进行文件替换操作,根据具体的需求选择合适的命令来完成替换任务。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值