Linux基础命令cp之拷贝隐藏文件

创建一个用户名为test211的普通用户

[23:35:09 root@C8[ ~]#useradd test211
[23:37:37 root@C8[ ~]#getent passwd test211
test211:x:1000:1000::/home/test211:/bin/bash

创建成功后发现test211家目录中已经有3个隐藏文件

[23:37:51 root@C8[ ~]#ll -a /home/test211/
total 12
drwx------. 2 test211 test211  62 Jul 12 23:37 .
drwxr-xr-x. 3 root    root     21 Jul 12 23:37 ..
-rw-r--r--. 1 test211 test211  18 Nov  9  2019 .bash_logout
-rw-r--r--. 1 test211 test211 141 Nov  9  2019 .bash_profile
-rw-r--r--. 1 test211 test211 312 Nov  9  2019 .bashrc

删除test211的家目录

[23:38:17 root@C8[ ~]#rm -rf /home/test211/
[23:38:46 root@C8[ ~]#ll -a /home/test211/
ls: cannot access '/home/test211/': No such file or directory

给test211一个密码用来登录

[23:40:28 root@C8[ ~]#echo 666 | passwd --stdin test211
Changing password for user test211.
passwd: all authentication tokens updated successfully.

再另一侧使用test211登录后显示找不到家目录

Activate the web console with: systemctl enable --now cockpit.socket

Could not chdir to home directory /home/test211: No such file or directory
[23:42:31 test211@C8[ /]$pwd
/

手动创建test211家目录

[23:47:54 root@C8[ ~]#mkdir /home/test211
[23:49:29 root@C8[ ~]#ll -a /home/test211
total 0
drwxr-xr-x. 2 root root  6 Jul 12 23:49 .
drwxr-xr-x. 3 root root 21 Jul 12 23:49 ..

用户test211登录,家中还是没有文件

Activate the web console with: systemctl enable --now cockpit.socket

Last login: Sun Jul 12 23:42:31 2020 from 192.168.50.200
[23:58:00 test211@C8[ ~]$ll -a
total 0
drwxr-xr-x. 2 root root  6 Jul 12 23:49 .
drwxr-xr-x. 3 root root 21 Jul 12 23:49 ..

创建用户时同时创建的家目录中的默认模板文件来在/etc/skel/

[23:47:47 root@C8[ ~]#ll -a /etc/skel/
total 24
drwxr-xr-x.   2 root root   62 Mar 16 13:51 .
drwxr-xr-x. 103 root root 8192 Jul 12 23:42 ..
-rw-r--r--.   1 root root   18 Nov  9  2019 .bash_logout
-rw-r--r--.   1 root root  141 Nov  9  2019 .bash_profile
-rw-r--r--.   1 root root  312 Nov  9  2019 .bashrc

我们将skel中的文件拷贝过去
使用cp -r /[路径]/.[^.]* 拷贝目录下所有隐藏文件,只拷贝隐藏文件因为是点开头

[00:02:10 root@C8[ ~]#cp -r /etc/skel/.[^.]* /home/test211/
[00:03:03 root@C8[ ~]#ll -a /home/test211/
total 12
drwxr-xr-x. 2 root root  62 Jul 13 00:02 .
drwxr-xr-x. 3 root root  21 Jul 12 23:49 ..
-rw-r--r--. 1 root root  18 Jul 13 00:03 .bash_logout
-rw-r--r--. 1 root root 141 Jul 13 00:03 .bash_profile
-rw-r--r--. 1 root root 312 Jul 13 00:03 .bashrc

如果想同时拷贝非隐藏文件,只需加一个点即可
使用cp -r /[路径]/. 即可以拷贝隐藏及非隐藏文件

[00:03:54 root@C8[ ~]#touch /etc/skel/null.test ##创建一个文件
[00:07:44 root@C8[ ~]#ll -a /etc/skel/
total 24
drwxr-xr-x.   2 root root   79 Jul 13 00:07 .
drwxr-xr-x. 103 root root 8192 Jul 12 23:42 ..
-rw-r--r--.   1 root root   18 Nov  9  2019 .bash_logout
-rw-r--r--.   1 root root  141 Nov  9  2019 .bash_profile
-rw-r--r--.   1 root root  312 Nov  9  2019 .bashrc
-rw-r--r--.   1 root root    0 Jul 13 00:07 null.test
[00:11:34 root@C8[ ~]#rm -rf /home/test211/*.* ##删掉文件夹下所有文件
[00:11:53 root@C8[ ~]#ll -a /home/test211/
total 0
drwxr-xr-x. 2 root root  6 Jul 13 00:11 .
drwxr-xr-x. 3 root root 21 Jul 12 23:49 ..
[00:12:09 root@C8[ ~]#cp -r /etc/skel/. /home/test211/ ##拷贝skel下所有隐藏和非隐藏文件到用户test211家目录下
[00:13:04 root@C8[ ~]#ll -a /home/test211/
total 12
drwxr-xr-x. 2 root root  79 Jul 13 00:13 .
drwxr-xr-x. 3 root root  21 Jul 12 23:49 ..
-rw-r--r--. 1 root root  18 Jul 13 00:13 .bash_logout
-rw-r--r--. 1 root root 141 Jul 13 00:13 .bash_profile
-rw-r--r--. 1 root root 312 Jul 13 00:13 .bashrc
-rw-r--r--. 1 root root   0 Jul 13 00:13 null.test

点 "." 包括隐藏和非隐藏文件

可以拷文件夹过去改个名即可

[00:13:13 root@C8[ ~]#rm -rf /home/test211/
[00:16:36 root@C8[ ~]#cp -r /etc/skel /home/test211
[00:16:59 root@C8[ ~]#ll -a /home/test211/
total 12
drwxr-xr-x. 2 root root  79 Jul 13 00:16 .
drwxr-xr-x. 3 root root  21 Jul 13 00:16 ..
-rw-r--r--. 1 root root  18 Jul 13 00:16 .bash_logout
-rw-r--r--. 1 root root 141 Jul 13 00:16 .bash_profile
-rw-r--r--. 1 root root 312 Jul 13 00:16 .bashrc
-rw-r--r--. 1 root root   0 Jul 13 00:16 null.test
* * * 胖并快乐着的死肥宅 * * *
原文地址:https://www.cnblogs.com/bpzblog/p/13290887.html