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

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

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

[[email protected] skel]# ls -A
.bash_logout  .bash_profile  .bashrc
[[email protected] skel]# cp $(ls -A) /home/mao1/
[[email protected] skel]# cd /home/mao1
[[email protected] mao1]# ls -A
.bash_logout  .bash_profile  .bashrc

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

[[email protected] skel]# mkdir .test.dir
[[email protected] skel]# mkdir test1.dir
[[email protected] skel]# ls -A
.bash_logout  .bash_profile  .bashrc  test1.dir  .test.dir
[[email protected] 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
[[email protected] skel]# ls -a /home/mao1/
.  ..  .bash_logout  .bash_profile  .bashrc  test1.dir  .test.dir

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

[[email protected] ~]# cd /home/mao2/
[[email protected] mao2]# ls -A
[[email protected] 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目录下,系统就会在当前路径下寻找这几个文件,恰巧当前目录没有这几个,所以就出错了。各位看官,到此处应该看出来,我的错误了。接下来验证我的想法是否正确。

[[email protected] tmp]# ll -A
total 0
[[email protected] tmp]# cd /etc/skel/
[[email protected] skel]# ls -A
.bash_logout  .bash_profile  .bashrc  .mozilla
[[email protected] skel]# cp -r $(ls -A /etc/skel/) /tmp 
[[email protected] skel]# cd /tmp/
[[email protected] 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
[[email protected] tmp]# cd /home/mao2
[[email protected] mao2]# ll -A
total 0
[[email protected] mao2]# cd -
/tmp
[[email protected] tmp]# cp -r $(ls -A /etc/skel/) /home/mao2/ 
[[email protected] tmp]# cd -
/home/mao2
[[email protected] mao2]# ls -A
.bash_logout  .bash_profile  .bashrc  .mozilla
[[email protected] mao2]#

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

[[email protected] mao2]# cd /usr/tmp/
[[email protected] tmp]# ls -A
[[email protected] 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

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

[[email protected] 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:
[[email protected] 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
[[email protected] tmp]# cp /etc/skel/{.bash_logout,.bash_profile,.bashrc,.mozilla}  /usr/tmp/
cp: omitting directory ‘/etc/skel/.mozilla’
[[email protected] 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:
[[email protected] ~]# ls -A /etc/skel/ |xargs|sed -r "s#(\..*[[:alpha:]]\b)#/etc/skel/\1#g" 
/etc/skel/.bash_logout .bash_profile .bashrc .mozilla

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

[[email protected] tmp]# ls -A /etc/skel/ >1.txt
[[email protected] tmp]# cat 1.txt 
.bash_logout
.bash_profile
.bashrc
.mozilla
[[email protected] 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, 又出错了。

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

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

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

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

尝试3:
[[email protected] 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。

时间: 2024-07-29 13:28:51

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

[golang 易犯错误] golang 局部变量初始化:=的陷阱

我们知道,golang中局部变量初始化方法(使用“:=”创建并赋值),让我们在使用变量时很方便.但是,这也是易犯错误的地方之一.特别是这个初始化符还支持多个变量同时初始化,更特别的是它还支持原有变量赋值和新变量创建并赋值同时进行!也就是说如果有部分变量不存在的而另外一些是已声明好的,用:=来初始化部分变量同样有效.这其实也没什么,更方便了嘛.但是,go的好多语句还支持局部前置语句,比如在if,for,switch等语句的初始化条件语句中.在这些地方,当你以为使用了原有变量的时候,实际上go已经为

样式易犯错误

1,可使字和图标对其 .logo_right img{ margin-right:10px; vertical-align: middle; 如: 2,a标签样式 .nav a:link ,.nav a:visited{ color:rgb(255,255,255); font-size:20px; text-decoration:none; } .nav a:hover ,.nav a:active{ color:#C00; text-decoration:none; font-size:20

git提交代码出现错误fatal: Unable to create '项目路径/.git/index.lock': File exists.

git提交代码出现错误fatal: Unable to create '项目路径/.git/index.lock': File exists. 具体出错代码如下: 具体原因不详,在stackoverflow上查找了很久,也不清楚原因,但是stackoverflow给出了解决的方法. 解决方法: 在当前项目下打开git bash,运行如下命令: rm -f ./.Git/index.lock 只为成功找方法.大胆的尝试,下一个fun就是你的fun! git提交代码出现错误fatal: Unable

初始化赋值时a = b = 常数 的易犯错误

1 #include <stdio.h> 2 #include <stdlib.h> 3 int main() 4 { 5 int a,b; a=b=2; //成功 6 //int a = b = 2;失败 7 printf("%d %d",a,b); 8 system("pause"); 9 return 0; 10 } 新手注意直接初始化变量的 int a = b = 2这样的写法会导致编译失败. 如何能让这个语句正确呢,那就在前面声明好

Python 基础教程 和 易犯错误(文本处理)

一:基础教程 (1)文件I/O  非常类似于 c语言:IO在计算机中指Input/Output,也就是输入和输出.由于程序和运行时数据是在内存中驻留,由CPU这个超快的计算核心来执行,涉及到数据交换的地方,通常是磁盘.网络等,就需要IO接口. spath="D:/download/baa.txt" f=open(spath,"w") # Opens file for writing.Creates this file doesn't exist. f.write(&

编程中易犯错误汇总:一个综合案例.md

# 11编程中易犯错误汇总:一个综合案例 在上一篇文章中,我们学习了如何区分好的代码与坏的代码,如何写好代码.所谓光说不练假把式,在这篇文章中,我们就做一件事——一起来写代码.首先,我会先列出问题,然后要求读者自己写一份答案:然后,我会给出我写的代码:最后,我们还会以这个问题为例,讨论编程中常见的错误. ## 1 问题描述 在[这个](http://wiki.openhatch.org/index.php?title=Scrabble_challenge)页面中,有一道Python相关的练习题,

绝对路径和相对路径

HTML初学者会经常遇到这样一个问题,如何正确引用一个文件. 比如,怎样在一个HTML网页中引用另外一个HTML网页作为超链接(hyperlink)?怎样在一个网页中插入一张图片? 如果你在引用文件时(如加入超链接,或者插入图片等),使用了错误的文件路径,就会导致引用失效(无法浏览链接文件,或无法显示插入的图片等). 为了避免这些错误,正确地引用文件,我们需要学习一下HTML路径. 在我们平时使用计算机时要找到需要的文件就必须知道文件的位置,而表示文件的位置的方式就是路径 1.绝对路径:是从盘符

Python中的绝对路径和相对路径

大牛们应该对路径都很了解了,这篇文章主要给像我这样的入门小白普及常识用的,啊哈 下面的路径介绍针对windows,其他平台的暂时不是很了解. 在编写的py文件中打开文件的时候经常见到下面其中路径的表达方式: [python] view plain copy open('aaa.txt') open('/data/bbb.txt') open('D:\\user\\ccc.txt') 这三种表达式里面,前两个都是相对路径,第三个则是绝对路径.绝对路径比较好理解,就是最完整的路径,相对路径的相对则是

关于绝对路径和相对路径

一.基本概念 1.相对路径-顾名思义,相对路径就是相对于当前文件的路径.网页中一般表示路径使用这个方法. 2.绝对路径-绝对路径就是你的主页上的文件或目录在硬盘上真正的路径.绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,比如,你的Perl 程序是存放在 c:/apache/cgi-bin 下的,那么 c:/apache/cgi-bin就是cgi-bin目录的绝对路径 在网络中,以http开头的链接都是绝对路径,绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,绝对路径一般在CGI程序