Summary

I’ll admit that the Terminal was a bit intimidating at first. But over time, as you get the basics down, you’ll find that there are many things you’re able to do more easily and quickly through the terminal than by using a graphical program. Perhaps the best place to start is in browsing files.

The Linux File System

Unlike thedrive-letter formaton Windows, where files are organized under labels like C:\ or D:, Linux uses a single-rooted structure that branches out from a single root directory. This structure may seem unfamiliar at first, but it’s surprisingly logical once you get the hang of it.

The root directory, marked by a forward slash (/), is the starting point of theLinux file systemhierarchy. From this topmost level, you’re able to navigate through various subdirectories such as /home, /bin, and /usr. Each of these directories has a specific role within the system and can contain additional subdirectories.

Showing current working directory in Linux terminal.

Here’s a concise overview of some common directories you’ll encounter:

Once youunderstand this structure, navigating your Linux system becomes straightforward. You can easily locate files, manage resources, and troubleshoot issues more efficiently.

Display Your Working Directory in the Terminal

The first thing you can do in the terminal is to check your current location within the file system. You can display the absolute path of your current directory by using the pwd command (print working directory).

To use it, just type:

If you are in your home directory, you’ll see something like/home/your_username, which indicates the sequence of directories leading from the root directory to your present location or path.

Knowing your current position in the file system helps you prevent mistakes, like accidentallydeleting filesin the wrong directory. I use this command countless times throughout the day, especially when I’m deep within several folders and need a quick check on my working path.

Listing files and directories in the terminal using ‘ls’ command.

List Files or Directories

Once you know where you are, you’ll probably want to see what’s inside that directory. You can do this with thels command, which displays a list of files and subdirectories within the current directory.

Simply type:

Then hit Enter, and you’ll see a list of items in your current directory.

Want a more detailed listing, including file sizes, modification dates, and permissions? Use the-l(lower-case L) option:

Listing files and directories in terminal in long format.

I often use the-lhcombination with the ls command. This combination is particularly useful when I’m trying toclean up disk space, as it shows file sizes in a format that actually makes sense (like 1.5 GB instead of 1500000000 bytes).

For a detailed listing with human-readable file sizes (like KB, MB, etc.), use:

Listing files and directories with human readable format.

Furthermore, if you’re curious abouthidden files(those that start with a dot), you can display them in the terminal by using the-aoption:

Copy and Move Files to a New Location

Sometimes, you might want to copy a file to share it with someone else or move it to a different folder for better organization. I often use thecpandmvcommands to organize my project files, such as copying my work files to my monthly reports folder.

Use thecpcommand followed by the original file and the directory where you want to copy it:

Showing hidden files in the terminal.

Here, I am copying the output.txt file from my current working directory to the Documents folder, leaving the original file untouched.

Furthermore, I love using the-iflag with the cp command while copying a file. This flag prevents accidental overwriting of existing files by prompting whether to overwrite them.

Copying files from one location to another in terminal.

Let’s execute the same previous command with-iflag:

The mv command, which is popular for renaming and moving, works similarly to cp. However, unlike cp, it removes the file from its original location.

To move a file, replace cp with mv in the previous cp command:

Copying files with precaution using the -i option of cp command.

Change or Make Directory

Navigating through directories is one of the most frequent tasks in the terminal. As you move through your file system, you’ll often need tochange directoriesor create new ones.

To change into another directory, usecdfollowed by the directory name or absolute path:

Changing current working directory to Document directory.

Now you’re inside the Documents folder. If you want to go back up one level in the directory tree, simply type:

Or just return to the previous directory with:

What if you need a new folder to organize files—say, for a new project? Trycreating one with mkdir. With mkdir, you can create a simple or nested directory at any path.

Usemkdirwith the directory name (to make a directory in the current working directory) or provide the complete path to create a new directory at a specified location:

Going one level back up in Linux file system.

Remove Files and Folders

Need the fastest way toremove files or foldersdirectly from the terminal? Try using thermand rmdir commands. With these commands, you can remove an empty directory, a directory with contents, or any single file.

Let’s remove a file by usingrmfollowed by the filename:

If you are looking to delete an entire directory (and all its contents), use the-roption:

The -r flag stands for recursive, meaning it will clean up everything inside that directory—always double-check before hitting Enter.

Going to home directory directly.

If you have a directory that is empty and does not contain any content, you’re able to delete it with:

One option I often use when removing any important file is the -i flag, which asks for confirmation before deleting:

View and Edit File Content in the Terminal

Sometimes, you may need to view or edit a file without closing the terminal. With tools likecat,nano,less, and others, you may view the contents of small to large files and also edit them with ease.

For a quick view of the contents, usecatwith filename:

What if the file is too long and scrolls off-screen too quickly? In that case, try using less instead of the cat:

With less, you can scroll through the file one page at a time—press the spacebar to move down or q to quit.

To display just the first few lines of a file, use theheadcommandwith the-noption:

Or show the last ten lines with thetail command:

For quick edits, I usually go with nano, a user-friendly text editor accessible from the terminal. To open a file in nano, run:

In nano, you can scroll using the arrow keys, make changes, and save them by pressing Ctrl+O. If you want a lightweight, no-fuss editor, nano is ideal for basic edits.

Alternatively, you can useViminstead of nano if you want amore powerful editor. I started with nano and gradually moved to Vim as I became more comfortable.

By knowing these simple Linux commands and tools, you may perform complex tasks on files in seconds directly from the terminal—tasks that would otherwise take much longer time to do manually.