Linux 目录和文件的操作 - Go语言中文社区

Linux 目录和文件的操作


CentOS-Logo

整理常用的linux命令,关于目录和文件的操作,用于巩固记忆,以备不时之需。


[root@localhost ~]

root:当前用户

localhost:主机名

~:当前所在位置

符号#:管理员

符号$:普通用户


命令字 [选项] [参数]

1.命令字:命令名称,使用命令字能唯一确定一条命令。

2.选项:调节命令的具体功能,决定这条命令如何执行。

  • 短格式选项:-字母

  • 长格式选项:--单词

一般情况下,"-"字母,"--"单词,有例外。

3.参数:命令字处理对象,通常是文件名、目录、用户等内容。

4.辅助操作

Tab:自动补齐

:强制换行

|:管道符

Ctrl + u:清空光标前所有内容

Ctrl + k:清空光标后所有内容

Ctrl + l:清屏,同clear

Ctrl + c:中断,取消编辑

Ctrl + d:登出,同exit


帮助手册

linux命令数量繁多,具体选项也各不相同,不可能全部记住,学会使用帮助手册可以大大提升效率。

--help 选项:ls --help

man 手册页:man ls

操作类型 操作键 功能
移动 ↑、↓ 上、下
翻页 Page Down 向下翻动一整页内容
^ Page Up 向上翻动一整页内容
退出 q或Q 退出手册阅读环境
查找 /-v 查找 "-v" 选项的帮助信息
^ n 看下一个匹配项
^ N 向上一个匹配项

Linux 系统目录结构

[root@localhost ~]# ls /
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

/binbinBinary的缩写, 这个目录存放着最经常使用的命令。

/boot:这里存放的是启动Linux时使用的一些核心文件,包括一些连接文件以及镜像文件。

/devdevDevice的缩写, 该目录下存放的是Linux的外部设备,在Linux中访问设备的方式和访问文件的方式是相同的。

/etc:这个目录用来存放所有的系统管理所需要的配置文件和子目录。

/home:用户的主目录,在Linux中,每个用户都有一个自己的目录,一般该目录名是以用户的账号命名的。

/lib:这个目录里存放着系统最基本的动态连接共享库,其作用类似于Windows里的DLL文件。几乎所有的应用程序都需要用到这些共享库。

/medialinux系统会自动识别一些设备,例如U盘、光驱等等,当识别后,linux会把识别的设备挂载到这个目录下。

/mnt:系统提供该目录是为了让用户临时挂载别的文件系统的,我们可以将光驱挂载在/mnt/上,然后进入该目录就可以查看光驱里的内容了。

/opt:这是给主机额外安装软件所摆放的目录。比如你安装一个ORACLE数据库则就可以放到这个目录下。默认是空的。

/proc:这个目录是一个虚拟的目录,它是系统内存的映射,我们可以通过直接访问这个目录来获取系统信息。
这个目录的内容不在硬盘上而是在内存里,我们也可以直接修改里面的某些文件,比如可以通过下面的命令来屏蔽主机的ping命令,使别人无法ping你的机器:

echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all

/root:该目录为系统管理员,也称作超级权限者的用户主目录。

/run:是一个临时文件系统,存储系统启动以来的信息。当系统重启时,这个目录下的文件应该被删掉或清除。如果你的系统上有/var/run目录,应该让它指向run

/sbins就是Super User的意思,这里存放的是系统管理员使用的系统管理程序。

/srv:该目录存放一些服务启动之后需要提取的数据。

/sys:这是linux 2.6内核的一个很大的变化。该目录下安装了2.6内核中新出现的一个文件系统sysfs

  • sysfs文件系统集成了下面3种文件系统的信息:针对进程信息的proc文件系统、针对设备的devfs文件系统以及针对伪终端的devpts文件系统。

  • 该文件系统是内核设备树的一个直观反映。

  • 当一个内核对象被创建的时候,对应的文件和目录也在内核对象子系统中被创建。

/tmp:这个目录是用来存放一些临时文件的。

/usr:这是一个非常重要的目录,用户的很多应用程序和文件都放在这个目录下,类似于windows下的program files目录。

  • /usr/bin:系统用户使用的应用程序。

  • /usr/sbin:超级用户使用的比较高级的管理程序和系统守护程序。

  • /usr/src:内核源代码默认的放置目录。

/var:这个目录中存放着在不断扩充着的东西,我们习惯将那些经常被修改的目录放在这个目录下。包括各种日志文件。

参考链接:Linux 系统目录结构 | 菜鸟教程


pwd

  • 显示出完整的当前活动目录名称
[root@localhost ~]# pwd
/root

cd

  • 切换目录

..:上一级目录,当前目录的父目录
.:当前目录
~:用户家目录

[root@localhost etc]# pwd
/etc
[root@localhost etc]# cd ~
[root@localhost ~]# pwd
/root
[root@localhost ~]# cd -
/etc
[root@localhost etc]# pwd
/etc
[root@localhost etc]# cd ..
[root@localhost /]# pwd
/

cd -:返回到上次所在的目录


ls

  • 显示目录内容

-l:以长格式显示文件和目录的列表,包括权限、大小等详细信息。
-a:显示所有子目录和文件信息,包括"."和".."。
-A:和-a类似,但不包括"."和".."。
-d:显示目录本身属性。
-h:以人性化方式显示出目录和文件的大小,显示KBMB等单位。要和-l一起使用。默认单位为B字节。
-R:递归显示目录及其子目录的所有内容。

[root@localhost ~]# ls
anaconda-ks.cfg
[root@localhost ~]# ls -l
total 4
-rw-------. 1 root root 1241 Aug 16 17:16 anaconda-ks.cfg
[root@localhost ~]# ls -a
.  ..  anaconda-ks.cfg  .bash_logout  .bash_profile  .bashrc  .cshrc  .tcshrc
[root@localhost ~]# ls -A
anaconda-ks.cfg  .bash_logout  .bash_profile  .bashrc  .cshrc  .tcshrc
[root@localhost ~]# ls -ld
dr-xr-x---. 2 root root 114 Aug 16 17:16 .
[root@localhost ~]# ls -lh
total 4.0K
-rw-------. 1 root root 1.3K Aug 16 17:16 anaconda-ks.cfg
[root@localhost ~]# ls -R /
/:
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

/boot:
config-3.10.0-693.el7.x86_64  initramfs-0-rescue-ba010ae4b2944c52b216ec6259f230c0.img  symvers-3.10.0-693.el7.x86_64.gz
efi                           initramfs-3.10.0-693.el7.x86_64.img                      System.map-3.10.0-693.el7.x86_64
grub                          initramfs-3.10.0-693.el7.x86_64kdump.img                 vmlinuz-0-rescue-ba010ae4b2944c52b216ec6259f230c0
grub2                         initrd-plymouth.img                                      vmlinuz-3.10.0-693.el7.x86_64

/boot/efi:
EFI

/boot/efi/EFI:
centos

//太长省略不写,一般没有人"ls -R /"这么写,这里纯粹写着玩的,看一下效果。
  • 颜色对应的文件类型

黑色:数据文件
蓝色:目录(文件夹)
天蓝色:软链接文件(快捷方式)
绿色:可执行文件
红色:压缩包

若是用不同的远程终端连接可能颜色会有所差别,这里以图形化安装后的终端显示颜色为准。

  • 匹配符号

?:匹配一个字符
*:匹配多个字符

[root@localhost test]# ls
temp1.txt  temp2.txt  test1.txt  test2.txt
[root@localhost test]# ls test?.txt
test1.txt  test2.txt
[root@localhost test]# ls t*
temp1.txt  temp2.txt  test1.txt  test2.txt

alias

  • 给命令起一个别名,常用于简化操作。
[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 which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@localhost ~]# alias lh='ls -lh --color=auto'
[root@localhost ~]# lh
total 4.0K
-rw-------. 1 root root 1.3K Aug 16 17:16 anaconda-ks.cfg

du

  • 统计目录及文件空间占用情况

-a:统计所有的文件
-h:友好显示,默认单位KB
-s:统计总大小

[root@localhost ~]# du -ah /root/
4.0K    /root/.bash_logout
4.0K    /root/.bash_profile
4.0K    /root/.bashrc
4.0K    /root/.cshrc
4.0K    /root/.tcshrc
4.0K    /root/anaconda-ks.cfg
24K     /root/
[root@localhost ~]# du -sh /root/
24K     /root/

mkdir

  • 创建新的目录

-p:嵌套创建多层目录

[root@localhost ~]# mkdir test1
[root@localhost ~]# ls
anaconda-ks.cfg  test1
[root@localhost ~]# mkdir test2 test3
[root@localhost ~]# ls
anaconda-ks.cfg  test1  test2  test3
[root@localhost ~]# mkdir -p test4/test5
[root@localhost ~]# ls
anaconda-ks.cfg  test1  test2  test3  test4
[root@localhost ~]# ls test4/
test5

touch

  • 创建空文件或修改时间标记
[root@localhost ~]# touch test.txt
[root@localhost ~]# ll
total 4
-rw-------. 1 root root 1241 Aug 16 17:16 anaconda-ks.cfg
-rw-r--r--. 1 root root    0 Aug 16 18:33 test.txt
[root@localhost ~]# touch test1.txt test2.txt
[root@localhost ~]# ll
total 4
-rw-------. 1 root root 1241 Aug 16 17:16 anaconda-ks.cfg
-rw-r--r--. 1 root root    0 Aug 16 18:33 test1.txt
-rw-r--r--. 1 root root    0 Aug 16 18:33 test2.txt
-rw-r--r--. 1 root root    0 Aug 16 18:33 test.txt
[root@localhost ~]# touch test.txt
[root@localhost ~]# ll
total 4
-rw-------. 1 root root 1241 Aug 16 17:16 anaconda-ks.cfg
-rw-r--r--. 1 root root    0 Aug 16 18:33 test1.txt
-rw-r--r--. 1 root root    0 Aug 16 18:33 test2.txt
-rw-r--r--. 1 root root    0 Aug 16 18:34 test.txt

cp

  • 复制文件或目录

-f:覆盖目标同名文件或目录时不提醒,强制复制。
-i:覆盖目标同名文件或目录时提醒确认。
-p:复制时保留源文件权限、属主及时间标记等属性不变。
-r:复制目录时必须使用,递归复制所有文件及子目录。

[root@localhost ~]# mkdir test1 test2
[root@localhost ~]# touch temp.txt
[root@localhost ~]# ll
total 4
-rw-------. 1 root root 1241 Aug 16 17:16 anaconda-ks.cfg
-rw-r--r--. 1 root root    0 Aug 17 10:24 temp.txt
drwxr-xr-x. 2 root root    6 Aug 17 10:24 test1
drwxr-xr-x. 2 root root    6 Aug 17 10:24 test2
[root@localhost ~]# cp temp.txt test1/
[root@localhost ~]# ll test1/temp.txt
-rw-r--r--. 1 root root 0 Aug 17 10:26 test1/temp.txt
[root@localhost ~]# cp -f temp.txt test1/
cp: overwrite ‘test1/temp.txt’? y
[root@localhost ~]# ll test1/temp.txt
-rw-r--r--. 1 root root 0 Aug 17 10:30 test1/temp.txt
[root@localhost ~]# cp -p temp.txt test1/
cp: overwrite ‘test1/temp.txt’? y
[root@localhost ~]# ll test1/temp.txt
-rw-r--r--. 1 root root 0 Aug 17 10:24 test1/temp.txt
[root@localhost ~]# cp -r test2/ test1/
[root@localhost ~]# ll test1/
total 0
-rw-r--r--. 1 root root 0 Aug 17 10:24 temp.txt
drwxr-xr-x. 2 root root 6 Aug 17 10:33 test2

参数-i默认是加上的,可在别名中查看,所以即使加上-f也不能强制覆盖。

  • 加反斜杠cp执行cp命令时不走alias
[root@localhost ~]# cp -f temp.txt test1/
[root@localhost ~]# ll test1/temp.txt
-rw-r--r--. 1 root root 0 Aug 17 10:39 test1/temp.txt

rm

  • 删除文件或目录

-f:删除时不提醒,强制删除。
-i:删除时提醒确认。
-r:删除目录时必须使用,递归删除整个目录树。

[root@localhost ~]# rm temp.txt
rm: remove regular empty file ‘temp.txt’? y
[root@localhost ~]# rm -rf test*
[root@localhost ~]# ls
anaconda-ks.cfg

参数-i默认是加上的,可在别名中查看。还有就是谨慎使用rm -rf


mv

  • 移动文件或目录、改名
[root@localhost ~]# mkdir test
[root@localhost ~]# touch test1.txt
[root@localhost ~]# ls
anaconda-ks.cfg  test  test1.txt
[root@localhost ~]# mv test1.txt test
[root@localhost ~]# cd test/
[root@localhost test]# ls
test1.txt
[root@localhost test]# mv test1.txt test2.txt
[root@localhost test]# ls
test2.txt

which

  • 查找用户所执行的命令文件存放的目录,搜索范围由PATH决定。
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost ~]# which ls
alias ls='ls --color=auto'
        /usr/bin/ls

find

  • 查找文件或目录

find [查找范围] [查找条件表达式]

-name:按名称查,可使用*?通配符。
-size:按大小查,大于用+,小于用-,单位有kBk小写)、MBGB
-user:按属主查。
-type:按类型查,f普通文件、d目录、b块设备文件、c字符设备文件。块设备指成块读取数据的设备(硬盘、内存等),字符设备指按单个字符读取数据的设备(键盘、鼠标等)。

  • 同时使用多个查找条件时,使用逻辑运算符,-a表示and,要同时满足,-o表示or,满足一个即可。
[root@localhost ~]# find /boot/ -name "vmlinuz*" -a -size +1024k
/boot/vmlinuz-3.10.0-693.el7.x86_64
/boot/vmlinuz-0-rescue-ba010ae4b2944c52b216ec6259f230c0
[root@localhost ~]# find /boot/ -name "vmlinuz*" -o -size +10M
/boot/vmlinuz-3.10.0-693.el7.x86_64
/boot/initramfs-0-rescue-ba010ae4b2944c52b216ec6259f230c0.img
/boot/vmlinuz-0-rescue-ba010ae4b2944c52b216ec6259f230c0
/boot/initramfs-3.10.0-693.el7.x86_64.img
/boot/initramfs-3.10.0-693.el7.x86_64kdump.img

cat

  • 显示并连接文件的内容
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="dhcp"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="01f37041-45dc-4840-a3b1-5fcca0d76448"
DEVICE="ens33"
ONBOOT="yes"
[root@localhost ~]# cat /etc/redhat-release /proc/version
CentOS Linux release 7.4.1708 (Core)
Linux version 3.10.0-693.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) ) #1 SMP Tue Aug 22 21:09:27 UTC 2017

more

  • 分页查看文件的内容,左下角显示百分比(看到最后会自动退出)

Enter:下一行
空格:下一页
b:上一页
q:退出

[root@localhost ~]# yum install httpd -y
[root@localhost ~]# more /etc/httpd/conf/httpd.conf
#
# This is the main Apache HTTP server configuration file.  It contains the
# configuration directives that give the server its instructions.
//省略

less

  • 类似more,但功能更多,左下角显示文件名(看到最后不会退出)

Enter:下一行
空格、Page Down:下一页
b、Page Up:上一页
q:退出
/:查找/后面跟的内容
n:查找下一个
N:查找上一个

[root@localhost ~]# less /etc/httpd/conf/httpd.conf

head

  • 查看开头部分内容,默认10行。

-n:显示n

[root@localhost ~]# head -2 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

tail

  • 查看末尾部分内容,默认10行。

-n:显示n
-f:跟踪尾部内容的动态更新,一般用来监控日志。

[root@localhost ~]# tail -2 /etc/passwd
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

wc

  • 默认统计文件内容中行数、单词个数、字节大小信息

-l:统计行数
-w:统计单词数
-c:统计字节数

[root@localhost ~]# wc /etc/hosts
  2  10 158 /etc/hosts
[root@localhost ~]# wc -l /etc/hosts
2 /etc/hosts
[root@localhost ~]# wc -w /etc/hosts
10 /etc/hosts
[root@localhost ~]# wc -c /etc/hosts
158 /etc/hosts
[root@localhost ~]# find /etc/ -name "*.conf" | wc -l
102

grep

  • 检索、过滤文件内容

-i:忽略大小写
-v:反转查找
^..:表示以..为开头
..$:表示以..为结尾
^$:表示空行

[root@localhost ~]# grep "bin/bash" /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@localhost ~]# grep -v "#" 文件 | grep -v "^$"
[root@localhost ~]# cat 文件 | grep -v "#" | grep -v "^$"

gzip & gunzip

  • gzip:压缩、解压缩格式为.gz压缩包
  • 不保留源文件

-9:提高压缩的比率
-d:解压

[root@localhost ~]# ls -lh
total 16K
-rw-------. 1 root root 1.3K Aug 16 17:16 anaconda-ks.cfg
-rw-r--r--. 1 root root  12K Aug 17 13:37 test.txt
[root@localhost ~]# gzip test.txt
[root@localhost ~]# ls -lh
total 12K
-rw-------. 1 root root 1.3K Aug 16 17:16 anaconda-ks.cfg
-rw-r--r--. 1 root root 4.5K Aug 17 13:37 test.txt.gz
[root@localhost ~]# gzip -d test.txt.gz
[root@localhost ~]# ls -lh
total 16K
-rw-------. 1 root root 1.3K Aug 16 17:16 anaconda-ks.cfg
-rw-r--r--. 1 root root  12K Aug 17 13:37 test.txt
  • gunzip:解压缩格式为.gz压缩包,和gzip -d一样。

bzip2 & bunzip2

  • bzip2:压缩、解压缩格式为.bz2压缩包,类似gzip,但压缩效率比gzip好。
  • 不保留源文件
[root@localhost ~]# bzip2 test.txt
[root@localhost ~]# ls -lh
total 12K
-rw-------. 1 root root 1.3K Aug 16 17:16 anaconda-ks.cfg
-rw-r--r--. 1 root root 4.3K Aug 17 13:37 test.txt.bz2
[root@localhost ~]# bzip2 -d test.txt.bz2
[root@localhost ~]# ls -lh
total 16K
-rw-------. 1 root root 1.3K Aug 16 17:16 anaconda-ks.cfg
-rw-r--r--. 1 root root  12K Aug 17 13:37 test.txt
  • bunzip2:解压缩格式为.bz2压缩包,和bzip2 -d一样。

tar

  • 归档和释放工具
  • 保留源文件

-z:调用gzip程序进行压缩或解压。
-j:调用bzip2程序进行压缩或解压。
-c:创建.tar格式的包文件。
-x:解开.tar格式的包文件。
-v:输出详细信息。
-f:表示使用归档文件。
-C:解压时指定释放的目标文件夹。
-t:列表查看包内的文件。
-p:打包时保留文件及目录权限。
-P:打包时保留文件及目录的绝对路径。

[root@localhost ~]# mkdir test
[root@localhost ~]# cd test/
[root@localhost test]# touch test1.txt test2.txt test3.txt
[root@localhost test]# ls
test1.txt  test2.txt  test3.txt
[root@localhost ~]# tar -zcvf test.tar.gz test/
test/
test/test1.txt
test/test2.txt
test/test3.txt
[root@localhost ~]# ls
anaconda-ks.cfg  test  test.tar.gz
[root@localhost ~]# tar -zxvf test.tar.gz -C /opt/
test/
test/test1.txt
test/test2.txt
test/test3.txt
[root@localhost ~]# ls -R /opt/
/opt/:
test

/opt/test:
test1.txt  test2.txt  test3.txt
[root@localhost ~]# tar -jcvf test.tar.bz2 test/
test/
test/test1.txt
test/test2.txt
test/test3.txt
[root@localhost ~]# ls
anaconda-ks.cfg  test  test.tar.bz2
[root@localhost ~]# tar -jxvf test.tar.bz2 -C /opt/
test/
test/test1.txt
test/test2.txt
test/test3.txt
[root@localhost ~]# ls -R /opt/
/opt/:
test

/opt/test:
test1.txt  test2.txt  test3.txt

echo

>:重定向(会覆盖)
>>:追加(不会覆盖)

[root@localhost ~]# echo "this is test file1" > test.txt
[root@localhost ~]# cat test.txt
this is test file1
[root@localhost ~]# echo "this is test file2" > test.txt
[root@localhost ~]# cat test.txt
this is test file2
[root@localhost ~]# echo "this is test file3" >> test.txt
[root@localhost ~]# cat test.txt
this is test file2
this is test file3

ln

  • 在文件之间建立连接

-s:创建软链接(快捷方式)

  • ls -i:查看文件节点序列号
[root@localhost ~]# ls -i test.txt
33574994 test.txt
[root@localhost ~]# ll
total 16
-rw-------. 1 root root  1241 Aug 16 17:16 anaconda-ks.cfg
-rw-r--r--. 1 root root 11753 Aug 17 13:37 test.txt
  • 创建软链接(快捷方式,源文件删除影响访问)
[root@localhost ~]# ln -s test.txt stest.txt
[root@localhost ~]# ll
total 16
-rw-------. 1 root root  1241 Aug 16 17:16 anaconda-ks.cfg
lrwxrwxrwx. 1 root root     8 Aug 17 13:52 stest.txt -> test.txt
-rw-r--r--. 1 root root 11753 Aug 17 13:37 test.txt
  • 创建硬链接(创建别名,源文件删除不影响访问)
[root@localhost ~]# ln test.txt htest.txt
[root@localhost ~]# ll
total 28
-rw-------. 1 root root  1241 Aug 16 17:16 anaconda-ks.cfg
-rw-r--r--. 2 root root 11753 Aug 17 13:37 htest.txt
lrwxrwxrwx. 1 root root     8 Aug 17 13:52 stest.txt -> test.txt
-rw-r--r--. 2 root root 11753 Aug 17 13:37 test.txt

history

-c:清除历史记录

[root@localhost ~]# history
    1  ls
    2  ls -l
    3  history
[root@localhost ~]# history -c
[root@localhost ~]# history
    1  history

vi & vim


最后

还是那句话,linux命令数量繁多,具体选项也各不相同,不可能全部记住,学会使用帮助手册可以大大提升效率。

版权声明:本文来源博客园,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://www.cnblogs.com/llife/p/11343256.html
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2019-11-09 11:06:10
  • 阅读 ( 1371 )
  • 分类:Linux

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢