Modifying the Filesystem

Screen Link:

https://app.dataquest.io/m/385/modifying-the-filesystem/4/copying-files

My Code:

/home/dq$ ls                                                                    
augustus  brats  prize_winners  tv  veruca  violet                              
/home/dq$ mkdir basket                                                          
/home/dq$ ls                                                                    
augustus  basket  brats  prize_winners  tv  veruca  violet                      
/home/dq$ cd brats                                                              
/home/dq/brats$ ls                                                              
augustus                                                                        
/home/dq/brats$ cp augustus basket                                              
/home/dq/brats$ ls                                                              
augustus  basket                                                                
/home/dq/brats$ cd ../basket                                                    
/home/dq/basket$ ls                                                              

What I expected to happen / What actually happened:

  1. I created a new directory basket inside /home/dq

  2. I opened the existing directory brats

  3. After listing out the files in this directory I tried to copy the file augustus to the newly created directory basket

  4. Instead of copying file augustus into directory basket, it created a new file with the name basket inside directory brats — Any idea why this happened?

  5. When I checked the diretory basket, it is empty — Any idea why this happened

image

The (absolute) path to basket that you created is /home/dq/basket.

It isn’t the case, but suppose you had other directories in your filesystem called basket. How would the shell know what you mean when you run cp augustus basket in /home/dq/brats? What basket could you mean?

Also, let’s say you just want to do exactly what you did: create a copy of augustus called basket in /home/dq/brat. What would that command look like?

The paragraphs above aren’t trying to answer the question, but rather try to given some motivation behind the behavior you’re seeing.

The answer, in a technical sense, rests on absolute and relative paths that you learned in the previous mission.

The arguments you’re passing to cp are relative paths. Therefore, running cp augustus basket in /home/dq/brats is equivalent to running the command below anywhere in the filesystem.

cp /home/dq/brats/augustus /home/dq/brats/basket

Since the directory /home/dq/brats/basket doesn’t exist, cp interprets it as you wanting to copy augustus in the same location.

I understood your point. But then still I am left with a couple of questions.

  1. What is that new file created with the name basket (green color) inside directory brats (as shown in the above picture)?
  2. Why am I not able to copy the file located at /home/dq/brats/basket into /home/dq/ inspite providing absolute location (as shown in the below picture)?
  3. When I try to list the directory content for dq from /home/dq/brats/basket, I am getting an error. But I am able to list the directory content for directory brats from /home/dq/brats/basket (as shown in the below picture). Why is this difference?

It is the result of running cp augustus basket. The contents of the file are the same as those of augustus.

This is incorrect, you were able to copy it when you ran cp tv /home/dq.

That’s because you’re passing a relative path to ls, meaning you’re asking the shell to list the files in /home/dq/brats/basket/home/dq, this is an inexisting directory and that’s what the error message says.

Ok I understood the answer to the questions 1 and 3.
But for question 2, you can see the error message in the picture .
“cp: cannot create regular file ‘home/dq’: No such file or directory.”

Right after that line you did it correctly. You see, home/dq is a relative path. An absolute path is a path starts with /.

Ok. Now its clear. Thank you.