While bash is fast and powerful, it’s also difficult for beginners to pick up. If you’re just getting started with bash or Terminal on Linux, make sure you know these essential commands to avoid getting lost and breaking things.
- 6 of the Most Useful Linux Commands for New Users
- 17 Fun Linux Commands to Run in the Terminal
- 5 Deadly Linux Commands You Should Never Run
- Thirteen Useful Tools for Working with Text on the Command Line
You’ll use this command by first typing
cd and then the path to a directory. A path is the route to a file folder, showing all the folders you need to go through. Folders are separated by forward slashes (/). For paths with spaces, you can either wrap the entire path in quotes or add a backslash () before the space.
All paths are interpreted from your present working directory. To specify a path that is not in your present working directory, you need to either navigate to the parent folder of that path or use a fully qualified path. A fully qualified path is the “complete address” of a file on your system, starting at the root drive (symbolized by /) and ending with the target directory or file.
cd uses a lot of shorthand for navigating directories quickly. For example, a single forward slash (/) indicates the “root” directory, which is your system’s boot drive. As mentioned before, the tilde (~) indicates the current user’s home directory. A single period (.) indicates the current folder, and two periods (..) indicate the parent folder of the current working directory. The parent folder is the folder that contains the working directory.
That backslash () is particularly special. It’s called an “escape character” and indicates that whatever comes after it should be handled specially. In this case, it indicates that the space is part of the file path and not a space between two different commands or arguments.
To view the contents of a directory, use
ls, which stands for “list.” The command lists all the files and directories in your current directory. Its most useful features are accessed via “flags,” which are short additional commands you attach to a primary command to change options or extend functionality. They’re preceded by one or two dashes (-) and are often one letter.
There’s a bunch of other really useful flags for
ls. Favorites include the following:
-ashows hidden files and dotfiles (files preceded by .)
-hdisplays file size with human-readable units
-Ssorts by file size, largest first (mind the capital S)
-tsorts by modification time, newest first
If you want to use multiple flags, just string them together. For example, if you want to view the hidden files in a folder, in long format, with human-readable file sizes, you could run
This command takes no argument, so press Enter immediately afterwards to run it. The
pwd command shows the fully-qualified path of the present working directory, hence the initialism.
Any commands you execute will automatically execute within the present working directory. For example, the
mkdir command will create a directory within your present working directory. To do otherwise, you need to use a fully qualified path, which starts with / to indicate the system root directory.
Opens the manual page, or man page, for the specified command. For example,
man chmod will display the man page for the “chmod” command within your terminal window. These entries show usage information, flags, and examples. If you want to know more about what a command does or what options are available, always start by reading the command’s man page.
Universal reader for any file type. Run it on a text file to display the contents of the file in the console.
cat will display the contents of nearly any file, but that output may not always be human-readable or meaningful to you. As such, it is used less by Unix pros, but beginners will find it helpful as they stumble around their file system.
Working with Files and Directories
Creates a copy of the file “filename.doc” in “path/to/directory/newfilename.doc.” You can think of this as a copy and paste action. If the filename already exists, the copy operation will not be completed.
Moves “foo.txt” to “bar.txt.” You can think of this command as a cut and paste action. We also use
mv for renaming files, since we’re essentially “moving” them to a new name. Note that the
mv command deletes the original version of the file after the write operation is complete.
Removes the directory contents and all the files contained within. The
-r flag makes
rm, or remove, operate recursively, while the
-f flag shuts off any confirmation dialogs. The more powerful version of this command,
sudo rm -rf, should be used with extreme caution. It can erase your boot drive without warning or complaint, so handle with care.
If a file with the specified name already exists, the modification time will be changed to the current time. If the file does not exist, an empty file with the same name will be created instantly.
Creates a directory with the specified name inside the working directory. To create parent directories as necessary, use the
-p flag. This will create all necessary parent directories to fulfill your command, provided no other errors exist, allowing you to create a multi-level directory hierarchy in one line.
Removes the directory at the specified path.
Modifiers adjust existing commands to make them do more things or do things differently than they might normally.
sudo stands for “super user do,” and it doesn’t do anything on its own. Instead, it gives the next command superpowers. When you type
sudo before a command, you temporarily elevate yourself to have the permission of the root user, giving you the power to do things you wouldn’t normally do.
But with superuser power comes superuser responsibility. It also gives you the power to break things very badly, so be cautious while you’re learning. Make sure you fully understand each part of the command you’re executing, especially if you found the command online.
After you use
sudo, you’ll need to enter your administrator password. When you do, the input cursor won’t move, but the keystrokes will be captured. Just press Enter when done to execute the command.
Run the last command again, but this time with administrator privileges. The so-called “bang bang” command (!!) is the shortcut to repeat the previous command.
Called a “redirect,” the caret sends the text-based output of your command to a file. For example,
ls > filelist.txt will send the output of
ls to “filelist.txt.” If the targeted file doesn’t exist, it will be created.
Called the “pipe,” this symbol is like redirect but only for commands. It sends the output of one command to the input of another.
Permissions affect which users can view, edit and execute specific files. Files and folders have an owner, which is normally the user that created it, and modes, which control the users that can access the file as well as what they can do with it.
These commands are the first commands that we’ll use
sudo with. Remember that
sudo elevates us to a superuser, giving us temporary administrator power. This is almost always necessary when dealing with file permissions, since not every file will be owned by your current user.
chmod stands for “change mode,” and adjusts permissions for files and folders. Like
chown, it can be run on a single file or run recursively on directory contents with the
File permissions can be represented a few ways, but the “numeric mode” used above (775) is the most common.
Change the owner of every file in the given directory to user “sarah.” The
-R flag makes the command recursive, but it can be run without the flag on a single file as well. Run this in your present working directory by using the period (.) in place of foo/bar.
To learn these commands quickly, use bash and Terminal even when you don’t have to. Instead of using drag-and-drop to move a directory, try using the
mv command. The more you actually use the commands, the more quickly your skills will improve.
Image credit: Autopilot