Key Takeaways
- The Linux file system starts with a single root directory, which is straightforward to navigate and manage.
- You can use basic terminal commands to perform basic file operations, such as locating the current path with pwd, listing files and directories with ls, and copying/moving with cp and mv.
- Additionally, you can view and edit file content using tools like nano, Vim and others.
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 can 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 the drive-letter format on 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 the Linux file system hierarchy. From this topmost level, you can 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.
Here’s a concise overview of some common directories you’ll encounter:
- /: Represents the root directory, where everything begins.
- /home: Stores user-specific files. Each user has their own folder here.
- /etc: Holds configuration files for your system.
- /var: Keeps variable data, like logs and databases.
- /usr: Where user programs and data are stored.
- /bin: Stores essential binary files.
- /tmp: Temporary files.
Once you understand 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:
pwd
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 accidentally deleting files in 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.
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 the ls command, which displays a list of files and subdirectories within the current directory.
Simply type:
ls
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:
ls -l
I often use the -lh combination with the ls command. This combination is particularly useful when I’m trying to clean 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:
ls -lh
Furthermore, if you’re curious about hidden files (those that start with a dot), you can display them in the terminal by using the -a option:
ls -a
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 the cp and mv commands to organize my project files, such as copying my work files to my monthly reports folder.
Use the cp command followed by the original file and the directory where you want to copy it:
cp output.txt /home/yourusername/Documents/
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 -i flag with the cp command while copying a file. This flag prevents accidental overwriting of existing files by prompting whether to overwrite them.
Let’s execute the same previous command with -i flag:
cp -i output.txt /home/yourusername/Documents/
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:
mv output.txt /home/yourusername/Documents/
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 to change directories or create new ones.
To change into another directory, use cd followed by the directory name or absolute path:
cd Documents/python
Now you’re inside the Documents folder. If you want to go back up one level in the directory tree, simply type:
cd ..
To go directly to your home directory, use:
cd
Or just return to the previous directory with:
cd -
What if you need a new folder to organize files—say, for a new project? Try creating one with mkdir. With mkdir, you can create a simple or nested directory at any path.
Use mkdir with 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:
mkdir directory_name or path-to-directory
To build nested directories, use the -p option:
mkdir -p parent/child/grandchild
Remove Files and Folders
Need the fastest way to remove files or folders directly from the terminal? Try using the rm and 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 using rm followed by the filename:
rm document.txt
If you are looking to delete an entire directory (and all its contents), use the -r option:
rm -r Directory_name/
The -r flag stands for recursive, meaning it will clean up everything inside that directory—always double-check before hitting Enter.
If you have a directory that is empty and does not contain any content, you can delete it with:
rmdir directory_name
One option I often use when removing any important file is the -i flag, which asks for confirmation before deleting:
rm -i important_file.txt
View and Edit File Content in the Terminal
Sometimes, you may need to view or edit a file without closing the terminal. With tools like cat, nano, less, and others, you can view the contents of small to large files and also edit them with ease.
For a quick view of the contents, use cat with filename:
cat samplefile.txt
What if the file is too long and scrolls off-screen too quickly? In that case, try using less instead of the cat:
less samplefile.txt
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 the head command with the -n option:
head -n 10 samplefile.txt
Or show the last ten lines with the tail command:
tail -n 10 file.txt
For quick edits, I usually go with nano, a user-friendly text editor accessible from the terminal. To open a file in nano, run:
nano samplefile.txt
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 use Vim instead of nano if you want a more 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 can perform complex tasks on files in seconds directly from the terminal—tasks that would otherwise take much longer time to do manually.