Linux基础-文件管理命令

文件管理命令:

  1. touch: 创建或更新文件时间
    创建文件
    [root@bogon ssx-linux]# touch file{1..6}
    [root@bogon ssx-linux]# ls
    a b c d e file1 file2 file3 file4 file5 file6
    [root@bogon ssx-linux]# touch file{a,b,c}
    [root@bogon ssx-linux]# ls
    a b c d e file1 file2 file3 file4 file5 file6 filea fileb filec
    [root@bogon ssx-linux]# rm -f file{a..c}
    [root@bogon ssx-linux]# ls
    a b c d e file1 file2 file3 file4 file5 file6
    [root@bogon ssx-linux]# touch file{a..z}
    [root@bogon ssx-linux]# ls
    a c e file2 file4 file6 fileb filed filef fileh filej filel filen filep filer filet filev filex filez
    b d file1 file3 file5 filea filec filee fileg filei filek filem fileo fileq files fileu filew filey
    [root@bogon ssx-linux]#

        ***更新文件时间***

    [root@bogon ssx-linux]# ll
    total 0
    drwxr-xr-x 3 root root 15 Feb 1 06:41 a
    drwxr-xr-x 2 root root 6 Feb 1 06:41 b
    drwxr-xr-x 2 root root 6 Feb 1 06:41 c
    drwxr-xr-x 2 root root 6 Feb 1 06:41 d
    drwxr-xr-x 2 root root 6 Feb 1 06:41 e
    -rw-r--r-- 1 root root 0 Feb 1 07:08 file1
    -rw-r--r-- 1 root root 0 Feb 1 07:08 file2
    -rw-r--r-- 1 root root 0 Feb 1 07:08 file3
    -rw-r--r-- 1 root root 0 Feb 1 07:08 file4
    -rw-r--r-- 1 root root 0 Feb 1 07:08 file5
    -rw-r--r-- 1 root root 0 Feb 1 07:08 file6
    [root@bogon ssx-linux]# touch file{1..6}
    [root@bogon ssx-linux]# ll
    total 0
    drwxr-xr-x 3 root root 15 Feb 1 06:41 a
    drwxr-xr-x 2 root root 6 Feb 1 06:41 b
    drwxr-xr-x 2 root root 6 Feb 1 06:41 c
    drwxr-xr-x 2 root root 6 Feb 1 06:41 d
    drwxr-xr-x 2 root root 6 Feb 1 06:41 e
    -rw-r--r-- 1 root root 0 Feb 1 07:12 file1
    -rw-r--r-- 1 root root 0 Feb 1 07:12 file2
    -rw-r--r-- 1 root root 0 Feb 1 07:12 file3
    -rw-r--r-- 1 root root 0 Feb 1 07:12 file4
    -rw-r--r-- 1 root root 0 Feb 1 07:12 file5
    -rw-r--r-- 1 root root 0 Feb 1 07:12 file6
    [root@bogon ssx-linux]#

  2. stat
    [root@bogon ssx-linux]# stat file6
    File: ‘file6’
    Size: 0 Blocks: 0 IO Block: 4096 regular empty file
    Device: fd00h/64768d Inode: 18796551 Links: 1
    Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
    Access: 2019-02-01 07:12:12.448044258 +0800
    Modify: 2019-02-01 07:12:12.448044258 +0800
    Change: 2019-02-01 07:12:12.448044258 +0800
    Birth: -
    [root@bogon ssx-linux]# vi file6
    [root@bogon ssx-linux]# stat file6
    File: ‘file6’
    Size: 6 Blocks: 8 IO Block: 4096 regular file
    Device: fd00h/64768d Inode: 18796553 Links: 1
    Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
    Access: 2019-02-01 07:15:25.168046803 +0800
    Modify: 2019-02-01 07:15:25.168046803 +0800
    Change: 2019-02-01 07:15:25.173046803 +0800
    Birth: -
    [root@bogon ssx-linux]#

  3. lrzsz
    --yum install -y lrzsz

--上传
rz -E
--回车后选择需要上传的文件
sz test.txt
--回车后选择需要存储下载文件的位置

4.file查看文件类型
[root@wll ~]# file file
file: cannot open (No such file or directory)
[root@wll ~]# file file1
file1: empty
[root@wll ~]# echo "123" > file2
[root@wll ~]# file file2
file2: ASCII text
[root@wll ~]#

5.文件复制命令cp
cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...

[root@oldboy ~]# touch {1..3}.txt

//将1.txt文件复制至/tmp目录中
[root@oldboy ~]# cp 1.txt /tmp/

//将2.txt文件复制至/tmp目录中修改名称为2.txt.bak(另存为)
[root@oldboy ~]# cp 2.txt /tmp/2.txt.bak

//如果复制过想保持原来的属性, -p保持原文件或目录的属性
[root@bogon ssx-linux]# ll file1
-rw-r--r-- 1 root root 0 Feb 1 07:12 file1
[root@bogon ssx-linux]# cp file1 b/
[root@bogon ssx-linux]# ll b
total 0
-rw-r--r-- 1 root root 0 Feb 1 07:19 file1
[root@bogon ssx-linux]# cp file1 c/ -p
[root@bogon ssx-linux]# ll c
total 0
-rw-r--r-- 1 root root 0 Feb 1 07:12 file1
[root@bogon ssx-linux]#

//复制目录只需要使用-r参数, 递归复制
[root@oldboy ~]# cp a /tmp/
cp: omitting directory ‘a’
[root@oldboy ~]# cp -r a /tmp/

//将多个文件拷贝同一个目录,显示详细信息
[root@oldboy ~]# cp -v ?.txt /tmp/
‘1.txt’ -> ‘/tmp/1.txt’
‘2.txt’ -> ‘/tmp/2.txt’
‘3.txt’ -> ‘/tmp/3.txt’
‘4.txt’ -> ‘/tmp/4.txt’

//强制覆盖拷贝文件,不提示;因为系统在安装的时候,cp - i 的 alias 就是 cp,所以使用绝对路径的cp命令,或者将cp转义去掉alias作用
[root@wll ~]# cp -rvf test.txt findtest/
cp: overwrite ‘findtest/test.txt’? y
‘test.txt’ -> ‘findtest/test.txt’
[root@wll ~]# 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@wll ~]# which cp
alias cp='cp -i'
/usr/bin/cp
[root@wll ~]# /usr/bin/cp test.txt findtest/
[root@wll ~]# /usr/bin/cp -v test.txt findtest/
‘test.txt’ -> ‘findtest/test.txt’
[root@wll ~]# \cp -v test.txt findtest/
‘test.txt’ -> ‘findtest/test.txt’
[root@wll ~]#

通配符:
?通配任意单个字符
* 通配任意多个字符
//

cp -v /etc/hosts /etc/hosts.bak

cp -v /etc/{hosts,hosts.bak}

cp -v /etc/hosts{,.bak}

6.文件目录移动命令mv(剪切),重命名
mv [OPTION]... [-T] SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...

//移动文件至tmp目录

mv file /tmp/

//移动文件至tmp目录并修改名称为file_test

mv file /tmp/file_test

//移动目录至/tmp目录下

mv dir/ /tmp/

//移动多个文件或多个目录至同一个目录

touch file{1..3}

mv file1 file2 file3 /opt/

mkdir dir{1..3}

mv dir1/ dir2/ dir3/ /opt

7.文件目录删除命令rm remove
rm [OPTION]... FILE...
-r 递归
-f 强制删除
-v 详细过程

//删除文件, 默认会提醒是否删除文件

rm anan

//强制删除文件, 不提醒

rm anan -f

//删除目录,会提醒

rm -r dir/

//强制删除目录,不提醒

rm -rf dir/

//示例

mkdir /home/dir10

touch /home/dir10/{file2,file3,.file4}

rm -rf /home/dir10/* //不包括隐藏文件

ls /home/dir10/ -a

. .. .file4

//示例2

touch file{1..10}

touch {1..10}.pdf

rm -rf file*

rm -rf *.pdf

猜你喜欢

转载自www.cnblogs.com/s-sx/p/11822359.html