olly - Fotolia
CLI commands every IT pro should know
This excerpt from 'CompTIA A+ Certification All-in-One Exam Guide, Tenth Edition,' published by McGraw-Hill Education, provides an introduction into common command-line prompts for Windows, Linux and macOS.
For techs who master the interface, the command line provides a powerful, quick, and flexible tool for working on a computer. Learning that interface and understanding how to make it work is not only useful, but also necessary for all techs who want to go beyond baby-tech status. You simply cannot work on modern computers without knowing the command line.
The CompTIA A+ 220-1002 certification exam tests you on a variety of command-line commands, both in Windows and Linux, for doing everything from renaming a file to rebuilding a system file. This excerpt from CompTIA A+ Certification All-in-One Exam Guide, Tenth Edition, written by Mike Myers and published by McGraw-Hill Education, outlines some of the most common and useful commands.
Structure: Syntax and switches
All commands in every command-line interface use a similar structure and execute in the same way. You type the name of the command, followed by the target of that command and any modifications of that command that you want to apply. You can call up a modification by using an extra letter or number, called a switch or option, which may follow either the command or the target, depending on the command. The proper way to write a command is called its syntax. The key with commands is that you can't spell anything incorrectly or use a \ when the syntax calls for a /. The command line is almost completely inflexible, so you have to learn the correct syntax for each command.
[command] [target (if any)] [switches]
or
[command] [switches] [target (if any)]
How do you know what switches are allowed? How do you know whether the switches come before or after the target? If you want to find out the syntax and switches used by a particular command, in Windows type the command followed by /? to get help:
[command name] /?
In macOS or Linux, type the command man (manual) followed by the command
you're interested in:
man [command name]
When you are done reading the manual, press the q key to quit back to the prompt.
Working with the command-line interface
Read the full chapter for more on CLI commands and how to use them.
Download NowViewing directory contents: dir and ls
The Windows dir command and the macOS and Linux ls command show you the contents of the working directory. If you're like most techs, you'll use dir or ls more often than any other command at the command prompt. When you open a command-line window in Windows, it opens focused on your user folder.
dir command
When you type a simple dir command, you will see that some of the entries look like this:
09/04/2021 05:51 PM 63,664 photo.jpg
All of these entries are files. The dir command lists the creation date, creation time, file size in bytes, filename, and extension.
Any entries that look like this are folders:
12/31/2021 10:18 AM <DIR> Windows
ls command
The ls command, like most UNIX commands, is very powerful and contains over 50 different switches. For now let's just cover one of the more important ones: –l. Using ls with the –l switch, which stands for long listing, gives detailed information about all the files:
$ ls -l
-rw-rw-r-- 0 mike 2313443 Jun 13 15:27 photo.jpg
Windows help screens sometimes seem a little cryptic and macOS and Linux help screens are often impossibly hard to read. Still, they're useful when you're not too familiar with a command or you can't figure out how to get a command to do what you need. If you're really lost, type help at the command prompt for a list of commands you may type.
Changing directory focus: The cd command
The cd command works in every operating system, although there are differences between Windows and macOS and Linux. You can use the cd command to change the focus of the command prompt to a different directory. To use the cd command, type cd followed by the name of the directory on which you want the prompt to focus. For example, in Windows, to go to the Obiwan directory in the root directory, you type cd \obiwan and then press enter. If the system has an Obiwan directory there, the prompt changes focus to that directory and appears as C:\Obiwan>.
Changing directory focus in macOS and Linux is similar to doing so in Windows, but you use a / instead of a \. Using the same example just shown for Windows, from the root directory you type cd /obiwan.
Moving between drives
Windows and macOS and Linux have very different techniques for moving between drives, given that Windows uses drive letters while macOS and Linux do not.
Moving between drives in Windows: The cd command is not used to move between Windows' drive letters. To get the prompt to point to another drive ("point" is command-line geekspeak for "switch its focus"), just type the drive letter and a colon. If the prompt points at the C:\Users\mike directory and you want to see what is on the USB thumb drive (E:), just type e: and the prompt will point to the USB drive.
Moving between Drives in macOS and Linux: So if macOS and Linux don't use drive letters, how do you access your other drive partitions, optical media, thumb drives, and so on? Well, all media is mounted as a folder, but the location of those folders is going to vary by the OS. In macOS, you need to look in the /Volumes folder. In Ubuntu Linux, you need to look in the /mnt folder for drives and the /media/<user name> folder for removable media.
Making directories: The md/mkdir command
Now that you have learned how to navigate in a command-prompt world, it's time to start making stuff, beginning with a new directory. To make a directory, use the md command in Windows. Alternatively, you can use the mkdir command, which works in all operating systems and is identical to md. In Windows, to create a directory called practice under your user's folder, for example, open a new Command Prompt window or cd to your users folder at \Users\<your username>.
You should see the prompt
C:Users\mike>_
Now that the prompt points to the C:\Users\mike directory, type md practice to create the directory:
C:\Users\mike>md practice
Creating folders in macOS and Linux is again identical, but you must use the mkdir command.
Removing directories: The rd/rmdir command
Removing subdirectories works exactly like making them. First, get to the directory that contains the subdirectory you want to delete, and then execute either the rmdir or rd command. Both of these commands are functionally identical, but where they work is important. The rmdir command works equally well in Windows, macOS, and Linux, but the rd command only works in Windows.
Running a program in Windows
To run a program from the Windows command line, simply change the prompt focus to the folder where the program is located, type the name of the program, and then press enter. Try this safe example. Go to the C:\Windows\System32 folder—the exact name of this folder is pretty standard on all Windows systems, but your mileage may vary. Type dir /p to see the files one page at a time.
Running a program in macOS and Linux
As much as I like to tell folks how similar the macOS, Linux, and Windows command lines are, they are very different in some areas, one of which is how you run executable programs from the command line. For starters, macOS and Linux executable programs don't rely on any kind of extension such as .exe in Windows. Instead, any file, whether it's compiled code or a text file, can be given the property of executable.
So it's really up to the person using the program to verify they are even using a program. MacOS and Linux have two very different types of executable file types: built-in and executables. Built-in programs are like the ones you just worked with: ls, mkdir, and rm. To run a built-in program you just type it in.
Executable programs are programs that are, well, not built in. If you download a program from somewhere (and Linux people do this a lot), you first unzip the program and then run the program. But there's one problem. If you try to run it, Linux can't find it, even though it is in the exact folder you are running it from! To make Linux run the executable, you need to add a period and a slash (./, commonly called "dot-slash") in front of the executable to make it run.
This excerpt covers information on CompTIA's A+ certification exam. For more preparation, see the CompTIA A+ practice quiz.