clothes iron, hammer, axe, flashlight and pitcher on brown wooden table

10 must-know Bash commands for Linux users

The Linux terminal is one of the most powerful tools on your computer, offering direct access…
Home » Blog » 10 must-know Bash commands for Linux users

Most Linux users know that the terminal is one of the most powerful tools on a computer. Don’t let the fact that it looks like an underwhelming text editor fool you. The Linux terminal, which runs a shell such as Bash or Zsh, offers direct access to the operating system in ways that nothing else can. Here are the top 10 terminal commands every Linux and POSIX user needs to know.

1. ls

The ls command lists files and folders (also called directories). When you first open a terminal, typing ls can often help orient you. Once you see a list of the familiar directories in your home folder, you have a good idea of where you are on your computer, and can navigate to the location where you want to go.

2. cd

The cd command stands for “change directory” or “current directory.” It’s the terminal equivalent of double-clicking on a folder icon out on the desktop. The cd command takes you to a new location, or in other words, it changes your current working directory to the location you specify.

For example, to make the Downloads folder your current directory:

$ cd ~/Downloads

The tilde (~) in that command is shorthand for “my home,” so the command specifically goes to the Downloads folder located within your home directory. That can be important when there’s more than one directory with the same name, in different locations.

Once you get to your destination, it’s common (although not required) to run ls, which was the first command in this list, to see what files and folders are in your current location.

Should you ever get hopelessly lost in your filesystem and just need to get back to someplace familiar, you can just type cd and you’re returned to the comfort of your home directory.

3. file

In a text-based interface, files and folders don’t look all that different from one another. Tell them apart with the file command:

$ file Readme
Readme: ASCII text
$ file Downloads
Downloads: directory

4. mv

When you need to move or rename a file, you use the mv command. The mv command requires two arguments. Logically, you must provide the location of the thing you want to move and where you want to move it to. For example, suppose you want to move a file called IMG_20230910.JPG into your Pictures folder:

$ mv IMG_20230910.JPG Pictures

To move that file into Pictures with a better name:

$ mv IMG_20230910.JPG Pictures/vacation_20230910.jpg

You don’t have to move a file far for it to get a new name. It’s acceptable to just rename a file in place:

$ mv IMG_20230910.JPG vacation_20230910.jpg
$ ls
vacation_20230910.jpg
[...]

When you’re all done with a file and have no further use for it, you can use the mv command to move it into your system’s trash folder. Your trash is a hidden directory in your home directory:

$ mv file.txt ~/.local/share/Trash/files

5. ln

You can link files or folders for quick access. This is sometimes called creating a “shortcut” or an “alias” on other operating systems. When you create a symbolic link on Linux, you create a pointer that looks like a file or a folder, but really only points to some location on your drive.

For example, the path to your system trash is pretty long: ~/.local/share/Trash/files so maybe you’d find it convenient to create a link to it instead. The ln command requires two arguments: the location that exists and the name of the link you want to create. In this case, it also requires the --symbolic option, which ensures that your pointer is just a pointer and not a clone of the data you’re linking.

$ ln --symbolic ~/.local/share/Trash/files ~/.Trash

Now you can use the mv command to move files to your trash folder with just this command:

$ mv file.txt ~/.Trash

You can create links to common locations on your drive for quick access, or you can create links to files you need access to in various locations but that you don’t want to make repeated copies of.

6. cp

Copying is like moving, except the original file remains. Suppose want to copy a file into your Documents folder, but you still want a copy of it in Downloads:

$ cp README.md ~/Documents
$ ls
README.md
[...]
$ ls ~/Documents
README.md
[...]

6. find

You can use cd and ls to crawl through your filesystem looking for files, or you can use the find command and let it do the work for you. The find command, as its name suggests, is designed to find files using several different filters and qualifiers. The find command is also a great interface for launching batch jobs, and it’s one of those “power” commands that seems simple but that’s secretly teeming with potential.

For now though, just the basics. The first argument for find must be the location you want to search. This can be ~ for your whole home directory, or it can be a specific location. There’s a balance to strike, here. If you constrain where you want find to search, the search will be faster because find has fewer items to look through. On the other hand, if you really don’t know where a file is, then maybe it’s better to just let find search everywhere.

You can limit what find looks for using the type option. There are lots of abbreviations available, but two of the most common are f for files and d for directories.

Finally, you must provide what you want find to search for. This is defined by the name or iname option, the first being the filename and the second being the filename ignoring the case of the letters. You can also use the wildcards * to stand in for any and all characters.

For example, to search your Downloads folder for a file with a name that includes the string linux in it:

$ find ~/Downloads -type f -iname "*linux*"

Here’s another example. This command searches your home directory for a directory with a filename starting with the string “My”:

$ find ~ -type d -name "My*"

7. sudo

By design, you don’t have permission to change files outside your own home directory. This restriction has saved many a user from accidentally moving or altering important system files. However, sometimes you have a legitimate reason for affecting your system environment. As long as you’re a user with administrative privileges, you can use the sudo command to be prompted for your password.

8. nano

Especially when you’re performing administrative tasks to a machine, it’s sometimes quicker and easier to make changes to an important configuration file right there in the terminal. On a desktop or laptop, there’s nothing stopping you from opening a file in a text editor of your choice as usual, but if you’re logged into a “headless” server or Raspberry Pi, then you may not have that option.

The nano text editor runs in your terminal, so you can open files, edit them, and save your changes without ever changing windows. The keyboard shortcuts are different from what you’re used to (instead of Ctrl+S to save, it’s Ctrl+O to “write out”) but they’re all listed at the bottom of the nano interface. Learning to edit text from within a terminal is vital for working on remote systems, and nano is a great introduction to the process.

9. curl

The curl command is a non-interactive web browser for your terminal. It’s non-interactive because, unlike a graphical web browser, you don’t open it and idly browse the Internet. Instead, you identify a specific page or resource, and curl fetches it.

For example, suppose you need to download the index page of example.com:

$ curl example.com   

By default, curl writes all data to your terminal screen. You can use the --output option to send data to a file:

$ curl example.com/sample.zip --output sample.zip

10. info or man

The info and man commands are designed to help you learn the options available for almost any command. Type info followed by the command you want info about to see that command’s documentation. If your system doesn’t have an info command, you can use man (short for “manual”) instead, but I find that info has the advantage of sometimes revealing a lot more information about a command than the often terse man pages do.

Bonus: &

The ampersand (&) isn’t actually a command, but it’s an important metacharacter that’s often forgotten about. Once you start working in a terminal, you’re likely to find yourself launching applications from the terminal for the sake of convenience and expediency. But when you launch a graphical application from a terminal, your terminal prompt becomes occupied by the application for as long as it’s running.

When you terminate a command with the & symbol, the process is sent to the “background” and your terminal prompt is restored to you.

$ libreoffice &
$

More commands to learn

There are plenty of other commands on Linux and POSIX systems to learn. In fact, a typical POSIX system likely has at least a hundred, but most have thousands. Depending on what you do on your computer, you’ll find your own collection of go-to commands, so start exploring what’s available and try some new commands out. And once you’ve found a few of your favorites, consider writing an article to share here on OpenSource.net!

Photo by Todd Quackenbush on Unsplash

Author

If you enjoyed this post, you might also enjoy these