Linux 基础_文件管理之:增删改查、复制、移动
Linux 文件管理
1. 文件管理之:创建/复制/移动/删除
==创建==
文件touch
# touch file1.txt // 无则创建,有则修改时间
# touch file3 file4
# touch /home/file10.txt
# touch /home/file5 file6
# touch /home/{alice,zhangsan}
# touch file{1..20}
# touch file{a..z}
# touch tslite{a,b,c} // {} 集合,等价于 tslite a b c;
目录 mkdir
-v:查看每个步骤的过程
-p:层次创建目录
# mkdir dir1
# mkdir /home/dir2 /home/dir3
# mkdir /home/{dir4,dir5}
# mkdir -v /home/{dir6,dir7}
# mkdir -v /hoem/dir8/111/222
# mkdir -pv /hoem/dir8/111/222 //包括其父目录的创建,不会有任何消息输出
# mkdir -pv /home/{yang/{dir1,111},tianyun}
==复制_cp
Usage: cp [OPTION]...[-T] SOURCE DEST
-r/-R: 递归
-v: 查看复制的过程
-f: 强制
# cd
# mkdir /home/dir{1..2}
# cp -v anaconda-ks.cfg /home/dir1 // 目录,不改变目录,只改变文件路径
# cp -v anaconda-ks.cfg /home/dir1/yang.txt // 文件, 改变目录,并且改变文件名
# cp -rv /etc /home/dir1
# cp -v anaconda-ks.cfg /home/dir90 // 没有/home/dir90
# cp -v anaconda-ks.cfg /home/dir2
# cp -v file !$
# cp -rv /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/passwd /etc/hostname /home/dir2 //将多个文件拷贝到同一个目录;
# cp -rv /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/passwd /etc/hostname /etc/hosts. // 将多个文件拷贝到当前目录;
# cp -r /etc /tmp
# cp -rf /etc /tmp
# \cp -r /etc /tmp
# type -a cp
cp is aliased to `cp -i`
cp is /bin/cp
# cp -rv /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0.bak
# cp -rv /etc/sysconfig/network-scripts/{ifcfg-eht0,ifcfg-eth0.bak}
# cp -rv /etc/sysconfig/network-scripts/ifcfg-eth0{ ,-org}
# cp -rv /etc/sysconfig/network-scripts/{ifcfg-eth0,yang-eth0}
==移动 mv
Usage: mv [OPTION]... [-T] SOURCE DEST
# mv file1 /home/dir3
# mv file2 /home/dir2/file20
# mv file4 file5
==删除 rm
-r 递归
-f force 强制
-v 详细过程
示例1:
手动删除:/home/dir1 //在相对路径中删除
# cd /home
# rm -rf dir1
脚本删除:/home/dir1 //在绝对路径中删除
rm -rf /home/dir1
示例2:
# mkdir /home/dir10
# touch /home/dir10/{file2,file3,file4}
# rm -rf /home/dir10/* //不包括隐藏文件
# ls /home/dir10 -a
. .. .file4
示例3:
# rm -rf file*
# rm -rf *.pdf
2、文件管理之:查看文件内容
==文本文件(cat tac less more head tail tailf grep...)
/bin/date
/etc/hosts
/etc/hostname
/etc/sysconfig/network-scripts/ifcfg-eth0
/etc/passwd
/etc/shadow
/etc/group
/etc/grub2.cfg
/etc/resov.conf
/etc/profile //设置系统环境变量
/etc/bashrc //影响 bash shell 环境
/var/log/messages //系统主日志文件
/var/log/secure //跟安全相关的日志,如ssh登录,本地登录。。。
cat
-n 显示行号
-A 包括控制字符(换行符/制表符)
linux $
Windows ^M$
示例:b.txt为windows记事本修改
# wget ftp://10.18.40.100/b.txt
# cat -A b.txt
# yum -y install dos2unix
# dos2unix b.txt
dos2unix: converting file b.txt to Unix format ...
# cat -A b.txt
less more head tail tailf
# head /etc/passwd
# head -2 /etc/passwd
# tail /etc/passwd
# tail -1 /etc/passwd
# tail /var/log/messages
# tail -20 /var/log/messages
# tail -f /var/log/secure //-f 动态查看文件的尾部
# tail -F /var/log/secure //-F 动态查看文件的尾部
注:vim, gedit编辑文件时,索引号会改变
grep 针对文件内容进行过滤
# grep 'root' /etc/passwd
# grep '^root' /etc/passwd
# grep 'bash$' /etc/passwd
# grep 'failure' /var/log/secure