Hopefully, you enjoyed learning about the command line terminal in the first post, or are otherwise somewhat familiar with the command line. In the previous post, I showed the most basic operations (ls, cd, help) one can do in Window’s cmd.exe terminal, and in Linux’s bash terminal (pwd, ls, cd, man). I am attempting to reach both a Windows and Linux user audience, so please pay close attention to whether the section is marked for Windows or Linux. Next, I will show how to do basic things like:
- Create and delete files and folders [Linux]
- Search your computer for specific files [Windows]
- Print to the terminal the contents of a file, as well as search files for strings [Linux]
Short for “make directory”, that will make a new folder, or directory in your current working directory.
That will make a new directory outside of your current working directory, given that subfolders/that/already/exist have already been created. Did you actually do it? Here is example input/output from my own command line:
The first command lists the contents of a folder (the www folder, the name is arbitrary) that exists within the ‘var’ folder, which exists in my current working directory. I escalate privileges with ‘sudo’ so that I have the permission required to issue the command and then create the directory. Then I list the contents and you can see my ‘exampleFolder’ was added (second line with blue text). I repeat the steps I just took again, for thoroughness – ‘secondExampleFolder’.
Okay maybe you want to delete the example we just made:
rm -r exampleFolder
rm -r subfolders/that/already/exist/exampleFolder
Short for remove, “rm” delete files and folders. “-r” is short for recursive, and tells your computer to delete the folder and all of its contents. You can’t just delete a folder/directory without deleting the contents as well (go ahead and try the command without the “-r” argument). Where would they go? How would they be referred to? If you’re still feeling this isn’t making much sense, that’s natural. It is kind of a tricky subject, but this Wikipedia page can further explain distinctions between home directory, root directory, parent directory, and so on.
Now that we are able to tell where we are in the command line and navigate through directories, we should be more comfortable and thus ready to take the next step! Let’s tackle the common problem of “where did I save that darn file?” It happens to everyone. We find ourselves mindlessly clicking through folders, checking and double checking locations. From the command terminal we can let out a triumphant laugh as we accomplish the task by belting out:
dir examplesearchentry /s /p
Where “examplesearchentry” could be:
- *.pdf (asterisk is a wildcard, so it will return all .pdf files)
- *ceip*.pdf (another valid use of a wildcard, will return “receipt.pdf” as well as other pdf files that contain “ceip”
This will return a list of all files that meet the search criteria, their size, the date last modified, and their location. Very useful.
Note: this command will recursively search all sub folders, so be careful where you start from and how loose your search criteria are. I just ran a search on “*.txt” in my Python directory and returned nearly 300 .txt files. Like this:
dir *.txt /s /p
More commands with practical applications:
Now that you know how to refer to specific folders and files on your computer, you can now start to rapidly access files and their contents. But how do you see what’s inside of a file from the command line? There’s no windows or GUIs to display the contents of files. True, but there are command line tools that are designed to work with files in this way, but from the command line. For instance, ‘cat’ prints the contents of the file to the terminal. And ‘grep’ searches a file for a given string (with other more advanced parameters as well).
- cat filename.txt
This will print the text from the file “filename.txt”
- grep stringYouWantToSearch filename.txt (or path/to/filename.txt)
And that will search the file for all occurrences of the string you entered. Here is an example I just did, the commands are underlined.
The Next Level:
This was a concise how-to that should make even the most naive of users comfortable with using the command line. If you just read through this to understand how the command line works, I will point out that opening up the terminal yourself and typing in the commands will help everything sink in. If you already know how to do these things, congratulations, now you can move on to the fun stuff. Sometimes we need a goal to encourage us to discover and learn things. If you don’t have your own explicit goals to lend you motivation to continue learning, I can help you in that department. The next few posts (in the coming week or two) will resemble more of a “quiz”, or puzzle rather, which I intend to be fun for beginners, intermediates and advanced users alike.