Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

2.3 File Management Class commands

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

4. What are the file management commands on Linux, their common usage and related examples.

1cp command

(1) simple understanding of cp command

[root@CentOS7 data] # whatis cp

Cp (1)-copy files and directories

Cp (1p)-copy files

[root@CentOS7 data] # which cp

Alias cp='cp-i'

/ usr/bin/cp

[root@CentOS7 data] # whereis cp

Cp: / usr/bin/cp / usr/share/man/man1/cp.1.gz / usr/share/man/man1p/cp.1p.gz

[root@CentOS7 data] # type cp

Cp is aliased to `cp-i'

(2) option

-I overwrite the prompt before, enter y enter. Does it work to return directly to the car?

-n do not overwrite, pay attention to the order of files

Recursively copy the directory and all its internal contents

-an archive, equivalent to-dr-- preserv = all

-d-- no-dereference-- preserv=links does not copy the original file, only the link name if you do not add-d to copy the linked file (soft link)

-- preserv [= ATTR_LIST] (preserve protection, maintenance)

Mode permission

Ownership main genus group

Timestamp timestamp

Links copies the symbolic link itself

Xattr extended Properties

Context security label

All

-p equals-- preserv=mode,ownership,timestamp

-v-- verbose explain what is being done

-f-- force is created after deletion

If an existing destination file cannot be opened, remove it and try again (this option is ignored when the-n option is also used)

-u-- update only copies files where the source is newer than the destination modification time (Modification Time) or where the destination does not exist

[root@CentOS7 data] # cat 1.log

[root@CentOS7 data] # cat > 1.log 111

> 22

> 33

> EOF

[root@CentOS7 data] # cat 1.log # has just been edited, mtime is late.

one hundred and eleven

twenty-two

thirty-three

[root@CentOS7 data] # cat aaa.txtl # this file mtime is earlier

Aaa

D

F

G

[root@CentOS7 data] # cp aaa.txtl 1.log

Cp: overwrite'1. Log? N

[root@CentOS7 data] # cat 1.log

one hundred and eleven

twenty-two

thirty-three

[root@CentOS7 data] # cp aaa.txtl 1.log-u

[root@CentOS7 data] # cat 1.log # View the contents of the file have not changed

one hundred and eleven

twenty-two

thirty-three

[root@CentOS7 data] # cp 1.log aaa.txtl-u

Cp: overwrite 'aaa.txtl'? Y

[root@CentOS7 data] # cat aaa.txtl # the contents of this file have been replaced

one hundred and eleven

twenty-two

thirty-three

-b destination exists, backup before overwriting, in the form of filename~

Only the last file can be backed up. If the second copy of a different file is the name of the previous file, it is still named name~

-- backup=numbered,t destination exists. Backup with digital suffix before overwriting

[root@CentOS7 data] # cp / etc/profile profile.bak-bcp: overwrite 'profile.bak'? Y [root@CentOS7 data] # lltotal 8 backup=numbered cp-1 root root 1819 Nov 12 13:05 profile.bak-rw-r--r-- 1 root root 1819 Nov 12 13:04 profile.bak~ [root@CentOS7 data] # cp-- backup=numbered / etc/profile profile.bakcp: overwrite 'profile.bak'? Y [root@CentOS7 data] # lltotal 12 lltotal RW profile.bak.~1~-1 root root 1819 Nov 12 13:08 profile.bak-rw-r--r-- 1 root root 1819 Nov 12 13:04 profile.bak~-rw-r--r-- 1 root root 1819 Nov 12 13:05 profile.bak.~1~

The classic combination is cp-av file/directory.

(3) usage:

# cp command replication may cause attribute changes

Cp ~ wang/.bashrc bashrc.bak

Ll! *

Ll ~ wang/.bashrc bashrc.bak

-rw-r--r-- 1 root root 231 Oct 22 09:17 bashrc.bak

-rw-r--r--. 1 wang wang 231 Aug 8 20:06 / home/wang/.bashrc

# # when you add the-p command, the attribute remains unchanged

Cp-p ~ wang/.bashrc bashrc.bak1

Ll! *

Ll-p ~ wang/.bashrc bashrc.bak1

-rw-r--r-- 1 wang wang 231 Aug 8 20:06 bashrc.bak1

-rw-r--r--. 1 wang wang 231 Aug 8 20:06 / home/wang/.bashrc

When copying some special files, the file type will also change.

[root@CentOS7 data] # ll / etc/redhat-release

Lrwxrwxrwx. 1 root root 14 Oct 14 08:52 / etc/redhat-release-> centos-release

[root@CentOS7 data] # ll redhat

-rw-r--r-- 1 root root 37 Oct 22 13:18 redhat

# # when copying a directory, you need to add the option-rscarp R

[root@CentOS7 data] # cp / boot/grub2 / data/gb.bak

Cp: omitting directory'/ boot/grub2'

[root@CentOS7 data] # cp-r / boot/grub2 / data/gb.bak

[root@CentOS7 data] # ls

Gb.bak

There may be two results for executing the same command twice. See below.

[root@CentOS7 data] # cp / root/Downloads/ / data/d-r

[root@CentOS7 data] # ll / data/d

Total 0

[root@CentOS7 data] # ls / data/d

[root@CentOS7 data] # cp / root/Downloads/ / data/d-r

[root@CentOS7 data] # ls / data/d

Downloads

2mv command

-I

-f

-b

-v

-u

3rm command

-r recursive operation

-f enforces and does not interact.

-v shows the running process

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report