Linux中的文件操作

文件查看

ls
list:列出当前路径下的文件
[root@localhost ~]# ls --help
Usage: ls [OPTION]... [FILE]... 
​
[]表示可有可无
option都是带-的
​
方法一:
Usage: ls 
[root@localhost ~]# ls
anaconda-ks.cfg  Documents  initial-setup-ks.cfg  Pictures  Templates
Desktop          Downloads  Music                 Public    Videos
​
方法二:
Usage: ls [OPTION]...
-a all 列出当前路径下所有文件,包含.或..开头的隐藏文件
[root@localhost ~]# ls -a 
.                .bashrc  Documents             .local     Videos
..               .cache   Downloads             Music      .viminfo
anaconda-ks.cfg  .config  .esd_auth             Pictures
.bash_history    .cshrc   .ICEauthority         Public
.bash_logout     .dbus    initial-setup-ks.cfg  .tcshrc
.bash_profile    Desktop  .lesshst              Templates
​
-l 长格式显示文件信息
[root@localhost ~]# ls -l
total 8
-rw-------. 1 root root 1852 Sep  7  2021 anaconda-ks.cfg
-:表示文件属性
  -:表示普通文件:黑色
  d:目录:深蓝色
  l:链接文件:浅蓝色
rw-------:文件权限
1:节点数
root:拥有人/所属者/所属用户
root:拥有组/所属组
1852:文件大小
Sep  7  2021:最后一次修改时间
anaconda-ks.cfg:文件名
​
Usage: ls [OPTION]...[file]
-h human 人性化显示文件大小,必须配合-l使用
[root@localhost ~]# ls -lh anaconda-ks.cfg 
-rw-------. 1 root root 1.9K Sep  7  2021 anaconda-ks.cfg
​
-d 查看目录本身的属性,需要配合-l使用
[root@localhost ~]# ls -l anaconda-ks.cfg 
-rw-------. 1 root root 1852 Sep  7  2021 anaconda-ks.cfg
[root@localhost ~]# ls -l Music
total 0
[root@localhost ~]# ls -ld Music
​
ls 
ls -a
ls -l
ls -lh
ls -ld

目录管理命令

pwd

print working directrory打印当前工作路径

[root@localhost etc]# pwd
/etc
[root@localhost etc]# cd /tmp/
[root@localhost tmp]# pwd
/tmp
cd

change directory 切换路径

cd ..返回上一级目录
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cd ..
[root@localhost sysconfig]# pwd
/etc/sysconfig
[root@localhost sysconfig]# cd ..
[root@localhost etc]# cd ..
[root@localhost /]# cd ..
[root@localhost /]# 
​
cd - 返回上一次进入的路径(相当于遥控器上的返回键)
[root@localhost ~]# cd /tmp/
[root@localhost tmp]# cd /var/
[root@localhost var]# cd /etc/
[root@localhost etc]# cd -
/var
[root@localhost var]# pwd
/var
[root@localhost var]# cd -
/etc
[root@localhost etc]# cd -
/var
​
cd 回家
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cd
[root@localhost ~]# 
mkdir

make directory 创建目录

[root@localhost ~]# mkdir --help
Usage: mkdir [OPTION]... DIRECTORY...
​
[root@localhost ~]# mkdir /test
[root@localhost ~]# mkdir /abc
[root@localhost ~]# cd /
[root@localhost /]# ls
bin   dev  home  lib64  mnt  proc  run   srv  test  usr  abc
boot  etc  lib   media  opt  root  sbin  sys  tmp   var
​
-p 递归创建目录
[root@localhost ~]# mkdir  /test1
[root@localhost ~]# mkdir /test1/test2
[root@localhost ~]# mkdir /test3/test4
mkdir: cannot create directory ‘/test3/test4’: No such file or directory
[root@localhost ~]# mkdir -p /test3/test4

rm

删除文件
[root@localhost ~]# rm a.txt 
rm: remove regular empty file ‘a.txt’? y
[root@localhost ~]# ls
1.txt            Documents             Music     Templates
anaconda-ks.cfg  Downloads             Pictures  test
Desktop          initial-setup-ks.cfg  Public    Videos
[root@localhost ~]# rm -f 1.txt 
​
删除目录
[root@localhost ~]# rm -rf /xiaogege/
[root@localhost ~]# cd /
[root@localhost /]# ls
bin   etc   lib64  opt   run   sys    test3  var
boot  home  media  proc  sbin  test   tmp    wangke
dev   lib   mnt    root  srv   test1  usr
​
​

文件管理命令

touch
命令格式: touch [OPTION]... FILE...
​
创建文件:touch 文件名
[root@localhost ~]# touch 1.txt
[root@localhost ~]# ls
1.txt            Desktop    Downloads             Music     Public     Videos
anaconda-ks.cfg  Documents  initial-setup-ks.cfg  Pictures  Templates
[root@localhost ~]# ls -l 1.txt 
-rw-r--r--. 1 root root 0 Oct 19 18:15 1.txt
​
刷新时间戳
[root@localhost ~]# touch 1.txt 
[root@localhost ~]# ls -l 1.txt 
-rw-r--r--. 1 root root 0 Oct 19 18:17 1.txt
cp

copy:复制

命令格式:cp [OPTION]... SOURCE... DIRECTORY
 
复制文件
[root@localhost ~]# cp b.txt /tmp
[root@localhost ~]# cd /tmp/
[root@localhost tmp]# ls
anaconda.log
b.txt
​
复制目录
[root@localhost ~]# cp Desktop /tmp
cp: omitting directory ‘Desktop’
[root@localhost ~]# cp -r Desktop /tmp
[root@localhost ~]# cd /tmp/
[root@localhost tmp]# ls
anaconda.log
b.txt
Desktop

mv

move:1、剪切;2、重命名

命令格式:mv [OPTION]... SOURCE... DIRECTORY
剪切
[root@localhost ~]# ls
anaconda-ks.cfg  Desktop    Downloads             Music     Public     Videos
b.txt            Documents  initial-setup-ks.cfg  Pictures  Templates
[root@localhost ~]# mv b.txt /tmp/
[root@localhost ~]# cd /tmp/
[root@localhost tmp]# ls
b.txt
Desktop
ssh-FJUnqGbchwE3
systemd-private-cc280081f4ff4ee9beae5b75aa3531d0-colord.service-dZaikh
systemd-private-cc280081f4ff4ee9beae5b75aa3531d0-cups.service-mzwSaf
systemd-private-cc280081f4ff4ee9beae5b75aa3531d0-rtkit-daemon.service-qyhnAs
tracker-extract-files.0
VMwareDnD
yum_save_tx.2023-10-19.19-03.zMRnw0.yumtx
[root@localhost tmp]# cd
[root@localhost ~]# ls
anaconda-ks.cfg  Documents  initial-setup-ks.cfg  Pictures  Templates
Desktop          Downloads  Music                 Public    Videos
​
重命名
[root@localhost ~]# mv 1.txt /opt/10.txt
[root@localhost ~]# ls
anaconda-ks.cfg  Documents  initial-setup-ks.cfg  Pictures  Templates
Desktop          Downloads  Music                 Public    Videos
[root@localhost ~]# cd /opt/
[root@localhost opt]# ls
10.txt  rh

文件查看命令

cat:一次性输出,只适合查看短文件
more:只能下翻,不能上翻
less:既能上翻,又能下翻
head:查看文件前10行
    -n 指定行数
tail:查看文件尾10行
    -n 指定行数
  • 19
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值