一、cp命令和mv命令的用法

1.cp命令的用法及介绍

利用cp(copy)命令可以实现文件或目录的复制

格式:

cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...

常用选项:

  • -i 如果目标已存在,覆盖前提示是否覆盖
  • -n 不覆盖,注意两者顺序
  • -r,-R 递归复制目录及内部的所有内容
  • -a 归档,相当于-dR --preserv=all,常用于备份功能
  • -d --no-dereference --preserv=links 不复制原文件,只复制链接名
  • --preserv[=ATTR_LIST]
    mode:权限
    ownership:属主属组
    timestamp:
    links
    xattr
    context
    all
  • -p 等同于--preserv=mode,ownership,timestamp
  • -v --verbose
  • -f --force
  • -u --update,只复制源比目标更新文件或者目标不不存在的文件
  • -b 目标存在,覆盖前先备份, 默认形式为filename~,只保留最近一个备份
  • --back=numbered 目标存在,覆盖前先备份加数字后缀,形式为filename.~#~,可以保留多个版本
源\目标 不存在 存在且为文件 存在且为目录
一个 新建DEST,并将SRC中内容填充至DEST中 将SRC中的内容覆盖至DEST中注意数据丢失风险!建议用-i选项 在DEST下新建与原文件同名的文件,并将SRC中内容填充至新文件中
多个文件 提示错误 提示错误 在DEST下新建与原文件同名的文件,并将原文件内容复制进新文件中
目录须使用-r选项 创建指定DEST同名目录,复制SRC目录中所有文件至DEST下 提示错误 在DEST下新建与原文件同名的目录,并将SRC中内容复制至新目录中

例:

[root@centos8 ~]# cp ~test/issue /data/issue_test.bak
[root@centos8 ~]# ll ~test/issue /data/issue_test.bak 
-rw-r--r-- 1 root root 23 May  2 15:55 /data/issue_test.bak
-rw-r--r-- 1 test test 23 May  2 15:54 /home/test/issue
[root@centos8 ~]# cp -p ~test/issue /data/issue_test2.bak
[root@centos8 ~]# ll ~test/issue /data/issue_test2.bak   
-rw-r--r-- 1 test test 23 May  2 15:54 /data/issue_test2.bak
-rw-r--r-- 1 test test 23 May  2 15:54 /home/test/issue
[root@centos8 ~]# cp /etc/sysconfig/ /data/
cp: -r not specified; omitting directory '/etc/sysconfig/'
[root@centos8 ~]# cp -r /etc/sysconfig/ /data/
[root@centos8 ~]# ll /data/          
total 12
-rw-r--r-- 1 test test   23 May  2 15:54 issue_test2.bak
-rw-r--r-- 1 root root   23 May  2 15:55 issue_test.bak
drwxr-xr-x 6 root root 4096 May  2 15:56 sysconfig
[root@centos8 ~]# cp -r /etc/sysconfig/ /data/sysconfig_bak
[root@centos8 ~]# ll /data/
total 16
-rw-r--r-- 1 test test   23 May  2 15:54 issue_test2.bak
-rw-r--r-- 1 root root   23 May  2 15:55 issue_test.bak
drwxr-xr-x 6 root root 4096 May  2 15:56 sysconfig
drwxr-xr-x 6 root root 4096 May  2 15:57 sysconfig_bak
[root@centos8 ~]# cp -b /etc/motd /data/issue
[root@centos8 ~]# ll /data/
total 16
-rw-r--r-- 1 root root    0 May  2 15:57 issue
-rw-r--r-- 1 test test   23 May  2 15:54 issue_test2.bak
-rw-r--r-- 1 root root   23 May  2 15:55 issue_test.bak
drwxr-xr-x 6 root root 4096 May  2 15:56 sysconfig
drwxr-xr-x 6 root root 4096 May  2 15:57 sysconfig_bak
[root@centos8 ~]# cp -b /etc/fstab /data/issue
cp: overwrite '/data/issue'? y
[root@centos8 ~]# ll /data/
total 20
-rw-r--r-- 1 root root  579 May  2 15:59 issue
-rw-r--r-- 1 root root    0 May  2 15:57 issue~
-rw-r--r-- 1 test test   23 May  2 15:54 issue_test2.bak
-rw-r--r-- 1 root root   23 May  2 15:55 issue_test.bak
drwxr-xr-x 6 root root 4096 May  2 15:56 sysconfig
drwxr-xr-x 6 root root 4096 May  2 15:57 sysconfig_bak
[root@centos8 ~]# cp --backup=numbered /etc/fstab /data/issue
cp: overwrite '/data/issue'? y
[root@centos8 ~]# cp --backup=numbered /etc/shadow /data/issue
cp: overwrite '/data/issue'? y
[root@centos8 ~]# cp --backup=numbered /etc/os-release /data/issue
cp: overwrite '/data/issue'? y
[root@centos8 ~]# ll /data/
total 32
-rw-r--r-- 1 root root  420 May  2 16:00 issue
-rw-r--r-- 1 root root    0 May  2 15:57 issue~
-rw-r--r-- 1 root root  579 May  2 15:59 issue.~1~
-rw-r--r-- 1 root root  579 May  2 16:00 issue.~2~
---------- 1 root root  857 May  2 16:00 issue.~3~
-rw-r--r-- 1 test test   23 May  2 15:54 issue_test2.bak
-rw-r--r-- 1 root root   23 May  2 15:55 issue_test.bak
drwxr-xr-x 6 root root 4096 May  2 15:56 sysconfig
drwxr-xr-x 6 root root 4096 May  2 15:57 sysconfig_bak

1、每天将/etc/目录下所有文件,备份到/data独立的子目录下,并要求子目录格式为backupYYYY-mm-dd,备份过程可见

[root@centos8 ~]# crontab -e
* * */1 * * cp -av /etc/ /data/backup`date +%F`

2、创建/data/rootdir目录,并复制/root下所有文件到该目录内,要求保留原有权限

[root@centos8 ~]# cp -a /root/ /data/rootdir

2.mv命令的用法及介绍

mv命令可以实现文件或目录的移动和改名

同一分区移动数据,速度很快:数据位置没有变化

不同分区移动数据,速度相对慢:数据位置发生了变化

格式:

mv [OPTION]... [-T] SOURCE DEST
mv [OPTION]... OURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...

常用选项:

  • -i 交互式
  • -f 强制
  • -b 目标存在时,覆盖前先备份
  • -u 当源文件比目标文件新或者目标文件不存在时才执行
  • --strip-trailing-slashes 删除源文件中的斜杠"/"
  • -S --suffix=SUFFIX 为备份文件指定后缀,而不使用默认的后缀
  • -t --target-directory=DIRECTORY 指定源文件要移动到目标目录

例:

# 创建两个文件
[root@centos8 ~]# touch test.txt
[root@centos8 ~]# touch test1.txt
[root@centos8 ~]# ls
test.txt test1.txt
# 将test.txt文件更名为test.txt.bak
[root@centos8 ~]# mv test.txt test.txt.bak
[root@centos8 ~]# ls
test.txt.bak test1.txt
# 如果目标文件已存在,则提示覆盖文件
[root@centos8 ~]# mv -i test.txt.bak test1.txt
mv: overwrite 'test1.txt'? y
[root@centos8 ~]# ls
test1.txt

二、I/O重定向技术详解

1.标准输入和输出

打开文件都有一个fd:file descriptor(文件描述符)

Linux给程序提供三种I/O设备

  • 标准输入(STDIN) -0 默认接受来自终端窗口的输入
  • 标准输出(STDOUT)-1 默认输出到终端窗口
  • 标准错误(STDERR)-2 默认输出到终端窗口

文件描述符

[root@centos8 ~]# ll /dev/std*
lrwxrwxrwx 1 root root 15 Apr 26 14:56 /dev/stderr -> /proc/self/fd/2
lrwxrwxrwx 1 root root 15 Apr 26 14:56 /dev/stdin -> /proc/self/fd/0
lrwxrwxrwx 1 root root 15 Apr 26 14:56 /dev/stdout -> /proc/self/fd/1
[root@centos8 ~]# ll /proc/self/fd/*
ls: cannot access '/proc/self/fd/255': No such file or directory
lrwx------ 1 root root 64 May  2 17:25 /proc/self/fd/0 -> /dev/pts/1
lrwx------ 1 root root 64 May  2 17:25 /proc/self/fd/1 -> /dev/pts/1
lrwx------ 1 root root 64 May  2 17:25 /proc/self/fd/2 -> /dev/pts/1
lr-x------ 1 root root 64 May  2 17:25 /proc/self/fd/3 -> /var/lib/sss/mc/passwd
lrwx------ 1 root root 64 May  2 17:25 /proc/self/fd/4 -> 'socket:[333513]'
lr-x------ 1 root root 64 May  2 17:25 /proc/self/fd/5 -> /var/lib/sss/mc/group
[root@centos8 ~]# ll /proc/`pidof tail`/fd
total 0
lrwx------ 1 root root 64 May  2 17:26 0 -> /dev/pts/0
lrwx------ 1 root root 64 May  2 17:26 1 -> /dev/pts/0
lrwx------ 1 root root 64 May  2 17:26 2 -> /dev/pts/0
lr-x------ 1 root root 64 May  2 17:26 3 -> /var/log/messages
lr-x------ 1 root root 64 May  2 17:26 4 -> anon_inode:inotify

找进程路径-->进入/proc目录找到对应的进程编号ll命令查看exe链接就可以找到进程目录了

[root@centos8 ~]# cd /proc/
[root@centos8 proc]# ls
1     1215  16    2247  2586   496    540    596          irq
117   1469  2167  2521  469    529    58593  devices      uptime
[root@centos8 proc]# cd 2521
[root@centos8 2521]# ls
attr             exe        mounts         projid_map    status
autogroup        fd         mountstats     root          syscall
[root@centos8 2521]# ll
lrwxrwxrwx. 1 rsqfx rsqfx 0 Mar 12 03:13 exe -> /usr/libexec/gsd-smartcard
[root@centos8 2521]# 

找当前终端的SHELL

[root@centos8 ~]# echo $$
58606

2.I/O重定向redirect

2.2.1 标准输出和错误重新定向

STDOUT和STDERR可以被重定向到指定文件,而非默认的当前终端

格式:

命令|操作符号 文件名

支持的操作符号包括:

1> 或> 把STDOUT重定向到指定文件
2>       把STDERR重定向到文件
&>       把标准输出和错误都重定向
>&      和上面功能一样,建议使用上面方式

以上如果文件已存在,文件内容会被覆盖

set -c 禁止将内容覆盖已有文件,但可追加,利用>|仍可强制覆盖
set +c 允许覆盖,默认

追加

>>可以在原有内容基础上,追加内容

把输出和错误重新定向追加到文件中

>>   追加标准输出重定向至文件
2>>   追加标准错误重定向至文件

标准输出重定向 将当前终端输出到另一个终端

[root@centos8 ~]# hostname 1> /dev/pts/2
[root@centos8 ~]# hostname > /dev/pts/2
[root@centos8 ~]# centos8
centos8

清空文件内容也可以用echo -n 不能直接用echo 因为echo默认换行

[root@centos8 ~]# echo abc > f1.txt
[root@centos8 ~]# ll f1.txt
-rw-r--r--. 1 root root 4 Mar 15 11:38 f1.txt
[root@centos8 ~]# echo > f1.txt
[root@centos8 ~]# ll f1.txt
-rw-r--r--. 1 root root 1 Mar 15 11:38 f1.txt
[root@centos8 ~]# echo -n > f1.txt
[root@centos8 ~]# ll f1.txt
-rw-r--r--. 1 root root 0 Mar 15 11:38 f1.txt

标准错误

[root@centos8 ~]# rm f1.txt 2> all.txt
y   # 输入y文件真的被删除
[root@centos8 ~]# cat all.txt   # 另一个终端查看文件内容
rm: remove regular empty file 'f1.txt'? [root@centos8 ~]# 

分别重定向

[root@centos8 ~]# ls 111.txt star > stdout.log 2> stderr.log
[root@centos8 ~]# cat stdout.log
111.txt
[root@centos8 ~]# cat stderr.log
ls: cannot access 'star': No such file or directory

正确错误都重定向到一个文件中

[root@centos8 ~]# ls 111.txt star &> all.log
[root@centos8 ~]# cat all.log
ls: cannot access 'star': No such file or directory
111.txt

合并多个程序

(CMA1;CMD2......)或者{ CMD1;CMD2;...;}合并多个程序的STDOUT,注意:用花括号时注意第一个命令与花括号之前有一个空格,然后最后一个命令要有;

[root@centos8 ~]# (ls;date) > f1.log
[root@centos8 ~]# cat f1.log
111.txt
...省略...
win.txt
Mon Mar 15 12:14:35 CST 2021
[root@centos8 ~]# { ls;date;} > f1.log
[root@centos8 ~]# cat f1.log
111.txt
...省略...
win.tx
Mon Mar 15 12:20:35 CST 2021

将标准输出和无措都重定向到同一个文件中

[root@centos8 ~]# ls /data/ /err > /data/all.log 2>&1
[root@centos8 ~]# cat /data/all.log
ls: cannot access '/err': No such file or directory
/data/:
all.log
f1.log
[root@centos8 ~]# ls /data/ /err 2> /data/all.log 1>&2
[root@centos8 ~]# cat /data/all.log
ls: cannot access '/err': No such file or directory
/data/:
all.log
f1.log
[root@centos8 ~]# ls /data/ /err &> /data/all.log
[root@centos8 ~]# cat /data/all.log
ls: cannot access '/err': No such file or directory
/data/:
all.log
f1.log
[root@centos8 ~]# ls /data/ /err 2>&1 > /data/all.log
ls: cannot access '/err': No such file or directory
[root@centos8 ~]# cat /data/all.log
/data/:
all.log
f1.log

标准输入 <后面是跟取的一个文件

[root@centos8 ~]# cat bc.txt
2+5
2*5
[root@centos8 ~]# bc < bc.txt
7
10

2.2.2 标准输入输入重定向

利用"<"可以将标准输入重定向

比如:

cat > file
test
luna

按crtl+d离开,可以使用文件来代替键盘的输入

cat < file1 > file2
cat < file1 >> file1

[root@centos8 ~]# echo 2^3 > bc.log
[root@centos8 ~]# cat bc.log
2^3
[root@centos8 ~]# bc < bc.log
8

[root@centos8 ~]# cat > mail.txt
hello
how old are you   # 按crtl+d
[root@centos8 ~]# cat < mail.txt
hello
how old are you
[root@centos8 ~]# cat mail.txt  
hello
how old are you
[root@centos8 ~]# cat < mail.txt > mial2.txt
[root@centos8 ~]# cat mial2.txt
hello
how old are you

将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中

[root@centos8 ~]# cat /etc/issue
\S
Kernel \r on an \m

[root@centos8 ~]# tr 'a-z' 'A-Z' < /etc/issue
\S
KERNEL \R ON AN \M

[root@centos8 ~]# tr 'a-z' 'A-Z' < /etc/issue > /tmp/issue.out
[root@centos8 ~]# cat /tmp/issue.out
\S
KERNEL \R ON AN \M

[root@centos8 ~]#