The Command Line and You - Part Two

The Power to Create and Destroy

So, in Part One we learned how to get around the file system using the command line using cd to move from one folder to another, and pwd to find out where we were.

This in itself is very useful, but the real power comes from creating new files and folders, then copying, moving and deleting them.

Let's start by creating a new folder to play around in. First, change directory to your Desktop folder with the following command: cd ~/Desktop  note: the screenschots are using Git Bash on a Windows PC, but the same commands will work on a Mac Terminal.


Next, we use the mkdir command to create a new folder on our desktop. (Are you wondering why change directory is cd and make directory is mkdir? Me too. I'm guessing there was an epic naming battle between the guys who wrote the first Unix shells that used command lines between economy  and clarity.) Let's crate a folder called "experiment," then cd into it and use pwd to verify we're where we expect to be.


Let me point out something really cool and time saving about the command line at this point. When you are typing a command such as cd, you can type the first few letters of the folder you want to change to, then press the [Tab] key and the rest will be filled out for you. Try it. Do a quick cd .., then type cd exp then [Tab]. The command line will see that experiment is the only folder that fits what you're looking for and fills in the rest.

Note though that I said it is the "only" folder that fits what you're looking for. If you press [Tab] and nothing happens, try it again. In Git Bash, it'll display a list of folders and files that match what you've started typing. In the example below I typed cd D in the home folder, and it told me I have Desktop, Documents and Downloads to choose from. If I add the o, it shows me just Documents and Downloads. When I add c, then it fills in Documents for me. Note: some command line environments may have a different behavior. For example, using Windows Powershell, pressing [Tab] will cycle through the available options rather than listing them. You can even keep going down the folder tree with this. If there was a subfolder in your documents folder,  you could type the first few letters of its name and press [Tab] and it will autofill that as well.


Okay, let's get back to our experiment folder. Now we're going to learn how to create a file. Can you guess what the command for that is? mf? mkfl? Good guesses, but not even close. The command is touch.  To use it, type touch followed by the name of the file you want to create. Let's try it. Create a couple files, maybe an HTML and a TXT file to start.


Okay, that works, but how do we know we actually created any files? Remember the ls command we learned about in Part One? It will show you files as well as folders. ls has a lot of options you can use with it. if you type ls -l it will show you the files in a long format, including file permissions, size, date and time it was created. (The - followed by one or more option letters is how you specify options for this command. Some other options are -a Show all files including hidden files (ones that start with a dot), and -F Show folders with a trailing /, and -R Show the contents of not only this folder, but recursively all the folders inside this folder.


Okay, we have files, now let's deleted them. df? del? Nope, to delete a file you use the rm command (remove).


Ah, the power to create and destroy. We are now command line gods! What about deleting folders. Can we do that? Sure. And it's the same command we use for files... almost. I'm going to make a new directory then try and delete it. Whoops, cannot remove it because it's a folder. But, if I add the -r option, success! (-r for recursive -- I know, it was a capital R for the ls command, but lowercase r for the rm command. Can't they just make up their minds?) 


A few more related things I want to show you before we end this post. First, copying a file. The command for this is cp (all right, that one mostly makes sense!) and it takes two arguments, the file you want to copy -- the source -- and where you want to copy it to -- the destination. You can copy a file to a new file, or you can copy it to a folder. I created a new folder called copiedstuff, then used the cp command two ways. Next you'll see the ls -R command to show off what we did.


When I specify a filename as the destination, it has to be different from the source. When I specify a folder as the destination, it copies it with the same name. Make sense?

By the way, you can specify a folder as your source, but you need to make sure of two things: that your destination is also a folder and not a file, and that you add the -r option like we did for rm.

What about moving a file? Taking it from one folder and putting it into another. That's mv, the move command (okay, looks like the naming guys have settled into a rhythm now.) Let's move my info2.txt file into the copiedstuff folder.


That'll come in handy. Almost done. One last thing. Renaming a file. I know you're playing the guessing game right now, but don't bother. We're actually going to use the mv command again, but instead of specifying a folder as the destination, we're going to specify a new filename.


Now you know how to work with your file system from the command line using internal commands. I showed you some of the options avaialble, but if youre curious (and you should be!) you'll be looking up what else each of these command can do.

Next time, we'll explore some other neat features related to launching programs and creating batch files!

Additional Links



Comments

Popular posts from this blog

Curiosity Digest #11

The Command Line and You - Part One

Curiosity Digest #12