The main reason why CLI, or any interface for that matter, even exists is so that it gives a user an easy way to work with files. Files can be just about any object on the computer; from dll files, to program files, to text documents. If you don’t know how to work with files in CLI, CLI will be pointless to you.
Thankfully, since you have been using Window’s GUI, you should have somewhat of an idea on what you can do to files using CLI. In fact, a lot of GUI commands in Windows (at least in Windows 9x) works by doing a CLI command in the background. These commands include copy, cut, create and delete.
WARNING: SOME OF THE COMMANDS THAT WE ARE GOING TO DO CAN DELETE OR DAMAGE DATA. ALWAYS MAKE SURE YOU BACK UP YOUR DATA, AND DO EXACTLY WHAT IS WRITTEN IN THIS POST. I AM NOT RESPONSIBLE FOR ANY LOST OR DAMAGE DATA. DO THESE EXERCISES AT YOUR OWN RISK.
Go to start->run, type cmd and press enter. This will bring up a command window. I am assuming that you have read the previous post and that you have a folder on the desktop named ‘batch files.’ If not, you can right click your desktop and create that folder. I am also assuming that your working directory is currently your home folder in the command line terminal. Change the directory to the ‘batch files’ directory by typing cd desktop/”batch files” and press enter.
NOTE: When working with CLI and batch files, it is best to work in a directory to kind of isolate your commands in that directory by using relative paths. This is a big reason why I had you create the ‘batch files’ directory in the first place. Also, I keep on posting warnings on the damages you can do with CLI to your computer. But, you can do just as much damage of the GUI interface as well if you don’t know what you are doing
.
Now that we are in the ‘batch files’ directory, type in dir to make sure the file ‘log.txt’ does not exist as well as the directories ‘source1′ and ‘source2′. These are the test names we are going to be using in this post.
Now, create the directories I had you check by typing mkdir source1 source2. Type in dir to make sure they were created. Notice how mkdir knew that source1 and source2 are two different directories by the space? If you wanted to make one directory named ‘source1 source2′, you would put it in quotes by typing mkdir “source1 source2″. In GUI, if you were to try to give a folder a name with a quote in it, you would get an error message saying you cannot use that. The reason why is because quotes have special meanings.
We are now going to create a text file named ‘log.txt’. You could try typing in help to find the command to use, but it will not help you that much (if it did, these posts would be useless
). We are going to use the command echo. The echo command displays a message. By default, the echo command, or any command, displays its messages on the screen. Type echo hello world!. The very next line says ‘hello world!’. It might not sound like much, but echo has very powerful uses. You can redirect that message in a file. The greater-than symbol can be used to redirect echo messages into a file. Redirect is used to redirect the output of one command and store it in a file.
Type in echo hello world! > log.txt and press enter. Now type in dir to make sure log.txt was created. Good, it exists! But, how do we know what is inside the text file? You could click on the ‘batch files’ folder on your desktop, and open it with notepad, but that is useless if our ultimate goal is to create batch files. What we could do instead is use the more command. The more command prints out a text file one screen at a time. This means that you would see the text file print on the screen, and pause so that you can actually read its content, and hit any key to see what is on the next screen. Type in more log.txt and press enter. You should see ‘hello world!’ on the next line.
NOTE: The more command was something that was unique in the Linux/Unix world. When it was implemented onto other platforms (like in Windows,) the Linux community wanted their CLI to be more powerful, so they created a command called ‘less’, which gave more features (like going back to the previous page) than the more command. Ironic, huh? Less is really more.
Now, type echo goodbye world! > log.txt and press enter. Huh. It accepted it. Type in more log.txt. It says ‘goodbye world!’ on the next line. Where is ‘hello world!’? We wrote over it! This is why I had you make sure that log.txt did not exist in the first place before we started (am I nice, or what?). Even using a simple, harmless echo command in CLI can write over data in a file without any warning W-H-A-T S-O E-V-E-R. That is why I post warnings about doing these exercises.
But, what if you want to add to a file? Just use two greater than signs to append, of course! Append is used to append text to a file that currently has text. If the file is not present, append would create the file and then put the text into it, just like a redirect would.
So, if you wanted to wipe the data in a file and start fresh, you would do a redirect. Else, do an append to add to the file.
Type in echo I’m back, world! >> log.txt and press enter. Instead of using more, let’s run the file and see what happens. Type in log.txt and press enter. Since Notepad is the default program to handle text files, Notepad opens up with the text we had entered.
Close notepad. Let us now work on moving the file around to different folders.
We want to make a copy of it into the source1 directory. The copy command copies a source directory or file into a destination folder or file. To do a simple copy, type in copy log.txt source1 and press enter. You should get a message saying one file was copy. Type in dir source1 and press enter to display the contents of that directory. You should see log.txt in it.
OK, that was simple. But, what if you wanted to change the name to message.txt when copying it? Type in copy log.txt source1\message.txt and press enter. And, just for good measure (or the fact we will need this in a minute), type in copy log.txt source1\message2.txt and press enter.
Now, have you ever heard of CLI being an efficient interface? It is not so efficient if we are copying one thing at a time. Heck, in GUI, you can highlight multiple files in order to be copied. Well, an advantage CLI has over GUI is the use of wild cards. Wild cards are used to specify more than one name based on conditions. To keep this post shorter than 100 pages, we will just stick to one character for now, the multiply sign (*). This is used to specify everything. For example, if you would want to copy all the files from source1 to source2, you can type ‘copy source1\* source2′. Notice you put the wild card in front of the directory. That wild card means ‘any file who’s name begins with it being in the source1 directory’ (remember, the directory name is part of the file name.) We can also just copy over the files named ‘message’ as well. Type in copy source1\m* source2 and press enter. Check the source2 directory by typing dir source2 to make sure the copy happened. What we did is we copied any file beginning with the letter ‘m’ in the source1 directory to the source2 directory.
The copy command is great, but what if you don’t want the source file to exist after the command has been executed? The move command will do this for you. The move command copies a source file or folder to a destination file or folder, then deletes the source. type in move log.txt source2 and press enter. Check both the source2 and current directories to make sure the move took place.
A move command can be used to rename a directory or file. Typing move log.txt message.txt would rename the file ‘message.txt’. However, the rename command can be used for the same thing. The rename command can be used to rename files and folders. However, the difference between a rename and a move is that rename will not let you move a file or directory into another directory.
I would show you the erase command, but we already did that in the previous post.
This concludes the working with files post. Feel free to play around with the commands we just used. Like I have said, be very, very careful as to what directory you are in. You would not want to type ‘erase *’ if you are in your home directory, unless if you wanted to delete all of your files and information in documents and on the desktop!!!!
Join the forum discussion on this post
