linux经常使用shell命令之文件操做命令

# touch 新建空文件,或更新文件时间标记
touch path/filename
user@computer: ~$ touch myfile  # 在当前目录下新建myfile
user@computer: ~$ touch /user/local/txt/myfile  # 在指定目录下新建myfile
user@computer: ~$ touch -c -t 06071700 /myfile  # 将myfile的时间记录修改成6月7日17:00
复制代码
# cp 复制文件或目录
cp [选项] source object
经常使用命令选项
-r:递归复制整个目录树
-f:   强制覆盖同名文件
user@computer: ~$ cp /etc/passwd  ~/test
user@computer: ~$ cp -r /etc /tmp
复制代码
# rm 删除文件或目录
rm  [选项] [file|dir]
经常使用命令选项
-r:递归删除整个目录树
-f: 即便文件的属性设置为只读,亦直接删除,无需逐一确认
user@computer: ~$ rm  ~/file
user@computer: ~$ rm -r ~/directory  # 删除目录时,应该加上-r选项,不然会失败
复制代码
# mv 移动文件或目录; 若目标位置与源位置相同,则至关于更名
mv [选项] 源文件或目录 目标文件或目录
user@computer: ~$ mv /root/pic/*.png /usr/local/share/pic  # 将/root/pic目录下的全部后缀名为”*.png”的文件移动到/usr/local/share/pic目录下
user@computer: ~$ mv /root/pic/kpic.png /root/pic/life.png # 把kpic.png文件更名为life.png
复制代码
# find 查找文件或目录
find [查找范围] [查找条件]
经常使用查找条件
-name:按文件名称查找
-user:按文件属主查找
-type:按文件类型查找 取值:[f|d|l|p]	
-size: 按大小查找 取值:[+|-] n 单位为字节或块

user@computer: ~$ find /etc -name p* -type f  # 查找etc下以p开头的文件
user@computer: ~$ find /etc -user root  # 查找etc下属主为root的文件或目录

-o:逻辑或
-a:逻辑与
user@computer: ~$ find /etc -size +2048 -a -size -20480  # 在/etc目录下查找大于1MB小于10MB的文件

-exec: 对查找到的结果进行操做
find [条件] -exec shell命令 {} \;  # {}表示找到的结果集
user@computer: ~$ find / -type f –size 0 –exec ls –l {} \;  # 查看文件长度为0的普通文件,并列出完整路径
复制代码
# ln 连接文件,给系统中已有的某个文件指定另一个可用于访问的名称。 
默认建立文件的一个硬连接
-s 建立一个软连接,其做用至关于windows中的快捷方式
user@computer: ~$ ln file1 file2  # 建立了file1的硬连接file2 删除其中之一对双方均无影响
user@computer: ~$ ln –s file1 file3  # 建立file1的软链接file3,删除file1后file3失效,若是从新给一个与file1同路径同名文件,连接文件又会恢复; 删除file3对file1无影响
复制代码
相关文章
相关标签/搜索