Copying and moving (renaming) files and folders.


One of the most often done operations on a file or folder is the ‘copy-paste’ one. And also a brother to him is the ‘cut-paste’. Here we are going to do both with the terminal.

As usual, we need a test folder. We shall call it “tetsujin”.

Suppose your home folder (hoping you know when you are in the home folder while using the terminal) has three folders namely tetsujn, Music and Documents. You wish to copy-paste tetsujin into the Documents folder. What would you do? Oops… I guess that’s what I’m trying to tell you…

Well, we’ll do the following command

cp -r tetsujin/ Documents/

Now of you do a ‘ls’ from the home folder, you will see ‘tetsujin’ there. Use ‘cd’ to go to the ‘Documents’ folder. Do an ‘ls’ over there and viola! There is ‘tetsujin’ there too! So you have successfully done a copy paste.

Note: If tetsujin was a file and not a folder, you can omit that ‘-r’ when copying. Also the trailing ‘/’ after tetsujin won’t be there in this case.

Let us now ‘cut-paste’ ‘tetsujin’ back into the home folder. To do this you run the command

mv tetsujin ~/

Don’t ask! ‘~/’ denotes your home folder. It is equivalent to writing “/home/your_username”. Suppose the name of your home folder, that is, your username is ‘mickey’, we can write the command above in any of the two following ways:

mv tetsujin ~/

mv tetsujin /home/mickey/

You might be wondering why we didn’t use ‘~’ in the command where we did ‘cp’. It is quite simple. Both ‘tetsujin’ and ‘Documents’ were in the same folder. You can only do it that way when the destination folder is accessible from the folder you are at the moment. If we wanted to copy ‘tetsujin’ from the ‘Documents’ folder into the home folder you would have had to run

cp -r tetsujin/ ~/

We have to specify the path of the destination folder from the root (‘/’) itself if that destination folder is not accessible from the folder we are in. That’s why we used the Tilda (‘~’) sign. It is tedious to keep on writing ‘/home/mickey’.

One interesting thing is that ‘mv’ can also be used for renaming files and folders.  Suppose we want to rename ‘tetsujin’ into ‘mazinger’. We run the command

mv tetsujin mazinger

If you do an ‘ls’ now, you’ll find that tetsujin has disappeared and mazinger is there in its stead.

Take care though. If the second argument was a folder’s name in the current directory, ‘tetsujin’ would have been moved into that folder instead of being renamed. Also if the second argument existed as a file already, then it will be over written bu tetsujin.

That is, assuming mazinger was already present in the current directory (Current directory means the folder in which your terminal is in right now). And you ran the command “mv tetsujin mazinger”. Now if you do an ‘ls’, tetsujin won’t be there (obviously), but mazinger will be there. However, it won’t be the old mazinger. It will have become tetsujin!

Check it out!

Advertisements

Hey! What do you think? Share your thoughts. :)

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s