小白易患错误之绝对路径和相对路径的操作错误

小白易患错误之绝对路径和相对路径的操作错误

作为一个不安稳的小白,一天都在那路乱折腾,恰巧,老师课程题目中有一题将/etc/skel 这个目录的文件除了..和. 复制到/home/USRNAEM 的家目录下。然后自以为是不按照老师的方法,自己折腾用了这样一条命令

[root@local skel]# ls -A
.bash_logout  .bash_profile  .bashrc
[root@local skel]# cp $(ls -A) /home/mao1/
[root@local skel]# cd /home/mao1
[root@local mao1]# ls -A
.bash_logout  .bash_profile  .bashrc

然后我又在这个skel目录下建立一个目录,确认目录是否能这样被引用。

[root@local skel]# mkdir .test.dir
[root@local skel]# mkdir test1.dir
[root@local skel]# ls -A
.bash_logout  .bash_profile  .bashrc  test1.dir  .test.dir
[root@local skel]# cp -r $(ls -A) /home/mao1
cp: overwrite `/home/mao1/.bash_logout'? y
cp: overwrite `/home/mao1/.bash_profile'? y
cp: overwrite `/home/mao1/.bashrc'? y
[root@local skel]# ls -a /home/mao1/
.  ..  .bash_logout  .bash_profile  .bashrc  test1.dir  .test.dir

也能成功,然后我又折腾了一下,到一个下一个用户的家目录,mao2(前面是mao1,请注意)。又执行这条命令

[root@local ~]# cd /home/mao2/
[root@local mao2]# ls -A
[root@local mao2]# cp -r $(ls -A /etc/skel/) /home/mao2/
cp: cannot stat `.bash_logout': No such file or directory
cp: cannot stat `.bash_profile': No such file or directory
cp: cannot stat `.bashrc': No such file or directory
cp: cannot stat `test1.dir': No such file or directory
cp: cannot stat `.test.dir': No such file or directory

居然给我反馈找不到文件和目录,我瞬间懵逼了。why? 一模一样的操作,居然前面能执行就换个目录就出错。瞬间感觉没爱了。然后隔壁老王让我排错,天生作为一个trouble maker。走上一条不属我的排错路,各种百度,google都无能给我解决。然后又将cp、ls的man文档一一翻译了一遍,均没结果。 随即想起“一支烟,解千愁,去万恨。”果然,回来的时候翻着ppt看,发现绝对路径和相对路径。仔细一看果然虽然我ls -A /etc/skel 显示的是skel目录下的文件,但是我cp的时候不在skel目录下,系统就会在当前路径下寻找这几个文件,恰巧当前目录没有这几个,所以就出错了。各位看官,到此处应该看出来,我的错误了。接下来验证我的想法是否正确。

[root@localhost tmp]# ll -A
total 0
[root@localhost tmp]# cd /etc/skel/
[root@localhost skel]# ls -A
.bash_logout  .bash_profile  .bashrc  .mozilla
[root@localhost skel]# cp -r $(ls -A /etc/skel/) /tmp 
[root@localhost skel]# cd /tmp/
[root@localhost tmp]# ll -A
total 12
-rw-r--r--. 1 root root  18 Aug  2 08:44 .bash_logout
-rw-r--r--. 1 root root 193 Aug  2 08:44 .bash_profile
-rw-r--r--. 1 root root 231 Aug  2 08:44 .bashrc
drwxr-xr-x. 4 root root  37 Aug  2 08:44 .mozilla
[root@localhost tmp]# cd /home/mao2
[root@localhost mao2]# ll -A
total 0
[root@localhost mao2]# cd -
/tmp
[root@localhost tmp]# cp -r $(ls -A /etc/skel/) /home/mao2/ 
[root@localhost tmp]# cd -
/home/mao2
[root@localhost mao2]# ls -A
.bash_logout  .bash_profile  .bashrc  .mozilla
[root@localhost mao2]#

上面可以看出只要在有这几个文件的目录下能成功,那我们再去没有这几个文件的目录下面执行以下这条命令。

[root@localhost mao2]# cd /usr/tmp/
[root@localhost tmp]# ls -A
[root@localhost tmp]# cp -r $(ls -A /etc/skel/)  /home/mao2/ 
cp: cannot stat ‘.bash_logout’: No such file or directory
cp: cannot stat ‘.bash_profile’: No such file or directory
cp: cannot stat ‘.bashrc’: No such file or directory
cp: cannot stat ‘.mozilla’: No such file or directory

诚不欺我,果然不能成功。作为一个小白继续折腾,没有路径,我给你添加。

[root@localhost tmp]# cp -r /etc/skel/$(ls -A /etc/skel/)  /home/mao2/ 
cp: overwrite ‘/home/mao2/.bash_logout’? y
cp: cannot stat ‘.bash_profile’: No such file or directory
cp: cannot stat ‘.bashrc’: No such file or directory
cp: cannot stat ‘.mozilla’: No such file or directory

oh,shit。又只能拷贝一个,一看原来中间有空格,系统把每个空格后面有当成相对路径。不怕,我们继续,反正作为一个trouble maker。我就是不断创造麻烦的。那就继续,既然这样我尝试用xargs将结果输出,然后再用sed在每个文件前面加个路径。

尝试1:
[root@localhost tmp]# cp -r /etc/skel/{$(ls -A /etc/skel/ |xargs|sed -r "s#[[:space:]]#\,#g")} /tmp
cp: cannot stat ‘/etc/skel/{.bash_logout,.bash_profile,.bashrc,.mozilla}’: No such file or directory
[root@localhost tmp]# cp /etc/skel/{.bash_logout,.bash_profile,.bashrc,.mozilla}  /usr/tmp/
cp: omitting directory ‘/etc/skel/.mozilla’
[root@localhost tmp]# cp -r /etc/skel/{.bash_logout,.bash_profile,.bashrc,.mozilla}  /usr/tmp/
cp: overwrite ‘/usr/tmp/.bash_logout’? y
cp: overwrite ‘/usr/tmp/.bash_profile’? y
cp: overwrite ‘/usr/tmp/.bashrc’? y

这个问题的原因在于命令引用后的bash的{}特性不再被识别,/etc/skel/{.bashlogout,.bashprofile,.bashrc,.mozilla}被当做一个整体的文件了。因此路不通。

尝试2:
[root@localhost ~]# ls -A /etc/skel/ |xargs|sed -r "s#(\..*[[:alpha:]]\b)#/etc/skel/\1#g" 
/etc/skel/.bash_logout .bash_profile .bashrc .mozilla

这个尝试最为纠结,尝试了各种不同办法,我在下面简单写一下部分尝试。

[root@localhost tmp]# ls -A /etc/skel/ >1.txt
[root@localhost tmp]# cat 1.txt 
.bash_logout
.bash_profile
.bashrc
.mozilla
[root@localhost tmp]# sed -r "s#(\..*[[:alpha:]]\>)#/etc/skel/\1#g" 1.txt 
/etc/skel/.bash_logout
/etc/skel/.bash_profile
/etc/skel/.bashrc
/etc/skel/.mozilla

由此证明正则表达式没错。go on;

oh,no, 又出错了。

[root@localhost tmp]# ls -A /etc/skel/ |xargs > 1.txt 
[root@localhost tmp]# sed -r "s#(\..*[[:alpha:]]\>)#/etc/skel/\1#g" 1.txt 
/etc/skel/.bash_logout .bash_profile .bashrc .mozilla

于是我怀疑是xargs命令的问题,我用加法运算做了一个尝试

[root@localhost tmp]# seq 1 10 |xargs|sed -r "s# #+#g" |bc
55
[root@localhost tmp]# seq 1 10|xargs 
1 2 3 4 5 6 7 8 9 10
[root@localhost tmp]# seq 1 10 |xargs|sed -r "s# #+#g" 
1+2+3+4+5+6+7+8+9+10
[root@localhost tmp]# seq 1 10 |xargs|sed -r "s# #+#g" |bc
55

xargs也没有问题,同样的命令,同样的正则表达式居然就在上面行不通了。若各位看官能解决此问题,望通知小弟。

尝试3:
[root@localhost home]# for i in `ls -A /etc/skel/` ;do cp -r /etc/skel/$i  /tmp ;done
cp: overwrite ‘/tmp/.bash_logout’? y
cp: overwrite ‘/tmp/.bash_profile’? y     
cp: overwrite ‘/tmp/.bashrc’? y

用for循环来实现这个功能,这个思路来自于teacher Rex。

尝试4:
cp -r /etc/skel/.  /home/mao

隔壁老王的方法,这个方法是最完美,最简便的方法。teacher wang 不亏是老司机。

总结:一,理解为什么排错最痛苦。这次不完全排错我用了一天功夫,原因有1.作为一个小白很多东西没有接触到,这个是个吃经验的活。2.自己把简单问题复杂化了。3.对于相对路径和绝对路径的理解不够深透。

二,来源teacher Rex,既然不知道怎么出错,就把每一个命令一一追加之新的文件,在对这个被追加的文件来执行命令。

三,踏踏实实做作业,不要猎奇。好奇心害死猫。

四,有问题找隔壁老王,万能的teacher wang。

原创文章,作者:fighter,如若转载,请注明出处:http://www.178linux.com/27583

(0)
上一篇 2016-08-05 16:20
下一篇 2016-08-05 16:20

相关推荐

  • 权限管理

    权限 权限就是用户或者组对文件或者目录所拥有的能力,所能执行的操作。 权限的分配: 通过ls -l file这个命令可以查看文件或者目录的详细信息:     [root@localhost home]# ls -l /root/   &nb…

    Linux干货 2016-08-05
  • iptables练习

    iptables实战 1.开启防火墙 systemctl start firewalld 2.清空所有的默认规则,定义自己的规则 iptables -F 查看此时的iptables iptables -nL Chain INPUT (policy ACCEPT) target prot opt source destination Chain FORWARD…

    2017-06-24
  • Linux用户和组的相关管理命令(一、用户的相关命令)

    Linux是一个可以实现多用户登录的操作系统,通过su – 用户名 可以进行用户之间的切换,从而完成不同登录用户下对私有文件的操作,同时,每个用户有且只有一个主组,但是可以有零个或多个附加组,每个组可以是一个用户的主组,同时还可以是多个用户的附加组。因此,熟练掌握用户和组的相关命令十分重要。 首先,要了解用户和组的配置文件各有两个: 与用户相关的…

    2017-07-22
  • Nginx4大模块——proxy、headers、upstream、stream模块

    Nginx 应用程序发布: 灰度模型:          (1) 如果存在用户会话;              从服务器上拆除会话;   &…

    Linux干货 2016-10-29
  • LVM逻辑卷管理

    一、lvm介绍     1.lvm概念                        &n…

    Linux干货 2016-09-02
  • 马哥教育网络20期+第四周练习博客

    1、  复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 [root@localhost ~]# cd /home/ [root@localhost home]# mkdir tuser1 [root@localhost home]# cp -a /etc/skel&…

    Linux干货 2016-08-02