So you’ve been using Linux for a while but have never gone to the command line. Hmm … guess what? That’s not uncommon. While you don’t need to go to the command line to effectively use Linux, knowing even a few basic commands can be useful. That knowledge can open a few doors, expand your computing experience, and in some cases save your bacon.
But if you’re thinking about taking your first steps at the Linux command line, where do you start? There are literally hundreds of commands and it can be difficult to know which ones are useful to you.
Let’s take a look at five basic Linux commands that every user should know. Not only are these commands immediately useful, they’re a good foundation on which to build your knowledge of the command line.
1. cd
When you pop open a terminal window, you’re probably not in the directory in which you want to be. So, how do you get there? By using the cd command. Short for change directory, cd lets you quickly move around the command line.
To change to a directory, just type cd [directory_name] — for example, cd documents. Obviously, you can use this command to navigate to subdirectories, too.
What happens when you’re several subdirectories deep and want to move up a directory or two? To move back up one directory, type cd ... To move two directories up, type cd ../... You can see a pattern emerging here …
2. ls
You’ve moved into a directory and now you want to see what’s in there. To do that, type ls.
That’s all fine and dandy, but what if you want to find out more about one or more files? Like how big it is or when it was created or last modified? Type ls -l.
The -l option lists the permissions on the file, who owns it, who last edited it, its size in kilobytes, and the date and time when it was last modified.
While many people use ls with an entire directory, you can also use it with a single file or groups of files. How? For a single file, type ls [file_name]. For multiple files, use a wildcard — for example, ls *.html.
3. cp
Next up, the command to copy files: cp. There are a number of ways to use cp. The most common way to use this command is to copy a file to another directory.
To copy a file to another directory, type cp [file_name] [directory_name]. Say you want to copy a file named blue_background.png to the directory web_graphics/backgrounds. The directory is two levels above the directory you’re currently in. Type cp blue_background.png ../../web_graphics/backgrounds.
Remember that unless you’re at the top of your home directory (for example, /home/scott) or are copying a file to a directory that’s below the one you’re in, you need to do one of the following:
- Enter the full path to the directory into which you’re copying a file
- Specify how many levels up the target directory is (using ../)
Of course, you can copy multiple files with cp by using a wildcard — for example, cp *.odt myDocuments.
4. mv
Now that you know how to copy files, it’s time to learn how to move them around. That’s where the mv command comes in. You can use that command to either move files to another directory or rename them in place.
When you want to move a file to another directory, mv uses the same command syntax as cp: mv [file_name] [directory_name]. Let’s say you want to move a file named blue_background.png to the directory web_graphics/backgrounds. The directory is two levels above the directory you’re currently in. Type mv blue_background.png ../../web_graphics/backgrounds.
And, as with the cp command you can move multiple files by using a wildcard — for example, cp *.pdf Reports.
To rename a file, just type mv [file_name] [new_file_name] — for example, mv 14217_6794.jpg fast_typing.jpg.
5. rm
Finally, one of the more dangerous commands you’ll run into: rm. Use this command to delete files, but remember to use it with caution. You aren’t warned that you’re about to delete one or more files. Once you delete something, it’s gone.
To delete a file, type rm [file_name] — for example, rm presentation_template_2009.odp. To delete multiple files, use a wildcard — for example, rm *.odp.
Delving Deeper
Maybe this article has whetted your appetite for the command line and you want to learn more. Not just about other commands, but also the other options for the five commands that were just introduced. There are a number of excellent resources available to do just that.
I usually recommend two. First, Linux Phrasebook by Scott Granneman. It’s an indispensable dead-trees book. If your tastes are more aligned with the online world, then check out out Introduction to the Linux Command Line by the folks at FLOSS Manuals. It’s easily one of the best guides to the command line out there.
Photo credit: mulligand
Leave a Reply
You must be logged in to post a comment.