为cp和mv命令添加进度条

Advanced Copy 是GNU cpGNU mv 程序的 mod 。它添加了一个进度条,并提供有关复制或移动文件和文件夹时发生的情况的一些信息。不仅是进度条,它还显示数据传输速率、估计剩余时间和当前正在复制的文件名。

安装高级复制补丁,向 cp 和 mv 命令添加进度条

cp 和 mv 命令是GNU coreutils提供。默认安装是的8.32-34版本。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@rocky9 ~]# rpm -qf /usr/bin/cp /usr/bin/mv 
coreutils-8.32-34.el9.x86_64
coreutils-8.32-34.el9.x86_64

[root@rocky9 ~]# rpm -qi coreutils
Name : coreutils
Version : 8.32
Release : 34.el9
Architecture: x86_64
Install Date: Tue Apr 9 11:18:00 2024
Group : Unspecified
Size : 5966362
License : GPLv3+
Signature : RSA/SHA256, Mon Apr 24 07:30:21 2023, Key ID 702d426d350d275d
Source RPM : coreutils-8.32-34.el9.src.rpm
Build Date : Mon Apr 24 07:25:45 2023
Build Host : pb-b87dc2e4-4d0e-4d7e-a608-9273f3ddebe5-b-x86-64
Packager : Rocky Linux Build System (Peridot) <releng@rockylinux.org>
Vendor : Rocky Enterprise Software Foundation
URL : https://www.gnu.org/software/coreutils/
Summary : A set of basic GNU tools commonly used in shell scripts
Description :
These are the GNU core utilities. This package is the combination of
the old GNU fileutils, sh-utils, and textutils packages.

可以从GNU coreutils下载新的版本,本次以最新的coreutils-9.5为例。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
## 安装需要的工具
[root@rocky9 ~]# yum install -y tar patch wget gcc gcc-c++

## 下载解压coreutils-9.5
[root@rocky9 ~]# wget http://ftp.gnu.org/gnu/coreutils/coreutils-9.5.tar.xz
[root@rocky9 ~]# tar xvJf coreutils-9.5.tar.xz
[root@rocky9 ~]# cd coreutils-9.5/

## 下载补丁包advcpmv-0.9-9.5,并应用
[root@rocky9 coreutils-9.5]# wget https://raw.githubusercontent.com/jarun/advcpmv/master/advcpmv-0.9-9.5.patch
[root@rocky9 coreutils-9.5]# patch -p1 -i advcpmv-0.9-9.5.patch
patching file src/copy.c
Hunk #1 succeeded at 114 (offset -7 lines).
Hunk #2 succeeded at 437 (offset -7 lines).
Hunk #3 succeeded at 468 (offset -7 lines).
Hunk #4 succeeded at 607 (offset -7 lines).
Hunk #5 succeeded at 625 (offset -7 lines).
Hunk #6 succeeded at 809 (offset -7 lines).
Hunk #7 succeeded at 888 (offset -7 lines).
Hunk #8 succeeded at 966 (offset -7 lines).
Hunk #9 succeeded at 1942 (offset -3 lines).
Hunk #10 succeeded at 2023 (offset -3 lines).
Hunk #11 succeeded at 2044 (offset -3 lines).
patching file src/copy.h
Hunk #1 succeeded at 256 (offset 7 lines).
Hunk #2 succeeded at 333 (offset 1 line).
patching file src/cp.c
Hunk #1 succeeded at 141 with fuzz 2 (offset 1 line).
Hunk #2 succeeded at 186 (offset 3 lines).
Hunk #3 succeeded at 686 (offset 16 lines).
Hunk #4 succeeded at 902 with fuzz 1 (offset 17 lines).
Hunk #5 succeeded at 987 (offset 17 lines).
Hunk #6 succeeded at 1128 (offset 19 lines).
Hunk #7 succeeded at 1190 (offset 19 lines).
Hunk #8 succeeded at 1383 (offset 35 lines).
patching file src/mv.c
Hunk #1 succeeded at 80 with fuzz 1 (offset 1 line).
Hunk #2 succeeded at 172 (offset 1 line).
Hunk #3 succeeded at 401 (offset 4 lines).
Hunk #4 succeeded at 465 (offset 5 lines).
Hunk #5 succeeded at 478 (offset 5 lines).

## 编译coreutils-9.5
[root@rocky9 coreutils-9.5]# export FORCE_UNSAFE_CONFIGURE=1
[root@rocky9 coreutils-9.5]# ./configure
[root@rocky9 coreutils-9.5]# make

拷贝编译好的cp和mv命令到$PATH路径中,重命名为gcp和gmv

1
2
[root@rocky9 coreutils-9.5]# cp ./src/cp /usr/local/bin/gcp
[root@rocky9 coreutils-9.5]# cp ./src/mv /usr/local/bin/gmv

为新命令设置别名

1
2
3
4
[root@rocky9 coreutils-9.5]# echo "alias cp='/usr/local/bin/gcp -ig'" >> /etc/profile
[root@rocky9 coreutils-9.5]# echo "alias mv='/usr/local/bin/gmv -ig'" >> /etc/profile

[root@rocky9 coreutils-9.5]# source ~/.bashrc

测试

1
2
3
4
5
6
[root@rocky9 ~]# which cp 
alias cp='/usr/local/bin/gcp -g'
/usr/local/bin/gcp
[root@rocky9 ~]# which mv
alias mv='/usr/local/bin/gmv -g'
/usr/local/bin/gmv
1
2
3
4
[root@rocky9 ~]# cp ubuntu-20.04.4-live-server-amd64.iso  /tmp
copying at 64.6 MiB/s (about 0h 0m 12s remaining)
ubuntu-20.04.4-live-server-amd64.iso 176.8 MiB / 1.2 GiB
[==================> ] 13.9 %

现cp命令是高版本中的二进制文件,可看到cp命令选项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
[root@rocky9 ~]# cp --help
Usage: /usr/local/bin/gcp [OPTION]... [-T] SOURCE DEST
or: /usr/local/bin/gcp [OPTION]... SOURCE... DIRECTORY
or: /usr/local/bin/gcp [OPTION]... -t DIRECTORY SOURCE...
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.
-a, --archive same as -dR --preserve=all
--attributes-only don't copy the file data, just the attributes
--backup[=CONTROL] make a backup of each existing destination file
-b like --backup but does not accept an argument
--copy-contents copy contents of special files when recursive
-d same as --no-dereference --preserve=links
--debug explain how a file is copied. Implies -v
-f, --force if an existing destination file cannot be
opened, remove it and try again (this option
is ignored when the -n option is also used)
-g, --progress-bar add a progress bar.
Note that this doesn't work with reflink,
reflink will be automatically disabled
-i, --interactive prompt before overwrite (overrides a previous -n
option)
-H follow command-line symbolic links in SOURCE
-l, --link hard link files instead of copying
-L, --dereference always follow symbolic links in SOURCE
-n, --no-clobber (deprecated) silently skip existing files.
See also --update
-P, --no-dereference never follow symbolic links in SOURCE
-p same as --preserve=mode,ownership,timestamps
--preserve[=ATTR_LIST] preserve the specified attributes
--no-preserve=ATTR_LIST don't preserve the specified attributes
--parents use full source file name under DIRECTORY
-R, -r, --recursive copy directories recursively
--reflink[=WHEN] control clone/CoW copies. See below
--remove-destination remove each existing destination file before
attempting to open it (contrast with --force)
--sparse=WHEN control creation of sparse files. See below
--strip-trailing-slashes remove any trailing slashes from each SOURCE
argument
-s, --symbolic-link make symbolic links instead of copying
-S, --suffix=SUFFIX override the usual backup suffix
-t, --target-directory=DIRECTORY copy all SOURCE arguments into DIRECTORY
-T, --no-target-directory treat DEST as a normal file
--update[=UPDATE] control which existing files are updated;
UPDATE={all,none,none-fail,older(default)}.
-u equivalent to --update[=older]. See below
-v, --verbose explain what is being done
--keep-directory-symlink follow existing symlinks to directories
-x, --one-file-system stay on this file system
-Z set SELinux security context of destination
file to default type
--context[=CTX] like -Z, or if CTX is specified then set the
SELinux or SMACK security context to CTX
--help display this help and exit
--version output version information and exit


为cp和mv命令添加进度条
https://www.xcjyc.top/2024/05/09/为cp和mv命令添加进度条/
作者
XCJYC
发布于
2024年5月9日
许可协议