By The How-To Geek A Command Line Primer for BeginnersThe command line isn't just for wise Linux beards. It's actually an awesome tool with almost limitless functionality. Here's a primer on how it works, and how you can do almost anything with it. Note: This article is meant for people who are either new to the command line or only have a couple of command-line tricks up their sleeve. If you're already conversant with most basic commands, you can send this article to others that still aren't up to your skill level and spread the good word about how great the command line really is. What Is the Command Line?The command-line interface, sometimes referred to as the CLI, is a tool into which you can type text commands to perform specific tasks—in contrast to the mouse's pointing and clicking on menus and buttons. Since you can directly control the computer by typing, many tasks can be performed more quickly, and some tasks can be automated with special commands that loop through and perform the same action on many files—saving you, potentially, loads of time in the process. The application or user interface that accepts your typed responses and displays the data on the screen is called a shell, and there are many different varieties that you can choose from, but the most common these days is the Bash shell, which is the default on Linux and Mac systems in the Terminal application. By default, Windows systems only include the anemic Command Prompt application, which has nowhere near the power of Bash, so for the purposes of this article we're going to suggest you use the open source Cygwin tool as your Windows command line, since it's quite a bit more powerful. You'll also at some point want to read parts one, two, and three of our series on using the Bash shell under Cygwin—the tips apply to Linux and OS X as well. Basic Command Line UsageTo get started with the command line, you'll need to open up a terminal window and get ready to start typing commands. Here's a list of basic commands you can use, organized by the type of activity that you might want to perform. When you run your terminal application (Cygwin on Windows, Terminal on Mac and Linux), your command prompt will start up pointing to a specific folder on your hard drive. You can navigate between folders, act on files inside those folders, or perform other actions. List Files
If you use the –a option, you can see hidden files, and you'll also notice something else in the listing: there are two entries for "." and ".." at the beginning of the list. These represent the current folder—the "." folder—and the parent folder—the ".." folder. Change Directories
You can navigate to either full or relative paths. For example, the command above navigates to a relative path—one above the current folder. If you're in
You can also navigate to absolute paths. Again, if I were in
To swap directories to the previous working directory, the '-' (hyphen) shortcut is handy to have on hand. For example, if you were in the
Creating or Removing Folders Creating and Removing Files
You can quickly remove all files in a directory by using the '*' (asterisk) wildcard—another simple tool that will come in very handy during your time in the command line. For example, if you're in a folder and want to delete every file inside that folder, just type:
If you want to delete a list of files and folders, including all files from subdirectories, without prompting you for every single entry, you can use the -r option for recursive, and the -f option for force. This command will wipe out every instance of a matching filename pattern (note the slightly different use of the wildcard) from the current directory and below:
Edit Plain Text Files
Otherwise, the vim editor is available on just about any system and can be invoked with the vi <filename> syntax. Displaying Files
This will display the contents of a file on the screen, and prompt you to scroll through the file a screen at a time. Command Redirection For instance, if you want to use ls –l to display a list of files but it keeps scrolling off the screen, you can pipe the output from the ls –l command into the input of the
If you wanted to save the output of that list directly into a file instead of displaying on the console, you could use the
You could then use the
Running a Script in the Current Folder
Using History Looping Over a Set of Files
Find Files
Find a Text String in Files
Batch Rename Files
Using Bash Shortcut Keys
Customizing Your Command ShellThere's no need to do your work in a boring terminal when you can do all sorts of tricks to customize it, like changing the colors, fonts, and adding aliases to complicated commands to save yourself time. You'll want to start off by reading our guide to customizing the command prompt, which will show you how to change the colors and add them to your profile so they show up when you launch a new shell. Using Aliases
This alias would make it so you could simply type
There are any number of useful aliases that you can use aliases to personalize your setup, but if you're having trouble coming up with good ideas, check out the list of the ten most useful aliases. Control Your System from the Shell The terminal has a rich set of tools for manipulating processes and checking on system stats. You can use the
You can then use the These examples not enough for you? Check out our top 10 command-line tools, our list of useful commands for Mac users, our guide to turbocharging your terminal, or the list of ten handy bash aliases. What are some of your favorite command-line tricks? Share your favorite tips, tricks, and advice in the comments.
| September 9th, 2010 Top Stories
|
Terms of use
0 comments to "A Command Line Primer for Beginners"