DEV Community

LaTerral Williams
LaTerral Williams

Posted on • Edited on

πŸ“Working with Files and Directories in Linux (Beginner Guide + Intermediate Bonus)

If you're just starting your Linux journey, one of the first superpowers you'll want to pick up is how to create and manage files and directories from the terminal. It might feel a bit old school at first (no GUI), but once you get the hang of it, it's faster, cleaner, and way more powerful than dragging things around with a mouse.

πŸ‘‹ I'm still learning too! As I go through my Linux course, I’ve realized how helpful it is to document the commands and concepts; along with practicing the commands. This article is a guide for others and a reference for me.

Let’s dive in!


πŸ“š Table of Contents


πŸ“Œ Intro: Why Learn File & Directory Commands?

In Linux, everything is a file, even devices! So understanding how to work with files and directories is like learning how to talk to your OS. Whether you're scripting, developing, or just trying to stay organized, these commands will be your best friends.


πŸ—ƒοΈ Creating Files in Linux

Let’s start by looking at different ways to create files from the command line.


πŸ“„ touch β€” Create Empty Files

touch myfile.txt
Enter fullscreen mode Exit fullscreen mode

What it does:
Creates an empty file named myfile.txt. If it already exists, it updates the modified time.

Use case:
Starting a new file, like a placeholder for code, logs, or notes.

Bonus tip:
You can create multiple files at once:

touch index.html style.css app.js
Enter fullscreen mode Exit fullscreen mode

🐱 cat β€” Create & View Files

cat > shopping_list.txt
Enter fullscreen mode Exit fullscreen mode

Start typing your content. Press Ctrl + D to save and exit.

cat shopping_list.txt
Enter fullscreen mode Exit fullscreen mode

What it does:
Creates a file and lets you input content, or displays file contents.

Use case:
Great for quick note-taking or reviewing the contents of a file.

πŸ’¬ echo β€” Quick File Content

echo "Hello, Linux!" > hello.txt
Enter fullscreen mode Exit fullscreen mode

To append rather than overwrite:

echo "Another line" >> hello.txt
Enter fullscreen mode Exit fullscreen mode

What it does:
Writes text to a file.

Use case:
Perfect for logging, adding config lines, or creating files within scripts.

✍️ nano / vim β€” Terminal Text Editors

nano mynote.txt
Enter fullscreen mode Exit fullscreen mode

What it does:
Opens a terminal-based text editor.

  • nano is user-friendly for beginners.

  • To save and exit: Ctrl + O (write), Enter, then Ctrl + X (exit).

Alternative:
Use vim if you’re ready for advanced editing features β€” just be ready for a learning curve!


πŸ“ Creating Directories

Once you've got your files, let’s organize them.

πŸ“‚ mkdir β€” Make Directories

mkdir projects
Enter fullscreen mode Exit fullscreen mode

What it does:
Creates a new folder (directory) named projects.

Use case:
Organizing files by category, such as code, notes, or data.

🌲 mkdir -p β€” Nested Directory Creation

mkdir -p dev/app/logs
Enter fullscreen mode Exit fullscreen mode

What it does:
Creates multiple nested directories in one go β€” it builds any missing parent folders.

Use case:
Great for setting up a full folder structure for a project.


πŸ› οΈ Other Helpful File Commands

πŸ“œ ls β€” List Files & Folders

ls
ls -l
ls -a
Enter fullscreen mode Exit fullscreen mode

What it does:

ls lists files and folders.

-l provides a detailed list (permissions, size, etc.).

-a includes hidden files (those starting with .).

Use case:
See what's in your current directory at a glance.

🌳 tree β€” Visual Folder Structure

sudo apt install tree   # On Debian/Ubuntu
tree
Enter fullscreen mode Exit fullscreen mode

What it does:
Shows a visual hierarchy of directories and subdirectories.

Use case:
Helpful for understanding or documenting the structure of a project.

Warning: Avoid using tree in the root directory.


🌍 Real-World Scenarios

Here’s where the commands come together:

πŸ’Ό 1. Setting Up a Project Structure

mkdir -p my_project/{src,tests,docs}
touch my_project/README.md
Enter fullscreen mode Exit fullscreen mode

πŸ““ 2. Writing a Quick Note

nano todo.txt
Enter fullscreen mode Exit fullscreen mode

πŸ—‚οΈ 3. Logging Events from a Script

echo "Backup completed on $(date)" >> logs/backup.log
Enter fullscreen mode Exit fullscreen mode

πŸ§ͺ 4. Creating Sample Data Files

touch data/sample1.csv data/sample2.csv
Enter fullscreen mode Exit fullscreen mode

πŸ”— Bonus: Combining Commands

You can chain commands together using && to automate tasks:

mkdir reports && cd reports && touch report1.txt report2.txt
Enter fullscreen mode Exit fullscreen mode
echo "System Check: $(date)" > system_status.txt && cat system_status.txt
Enter fullscreen mode Exit fullscreen mode

🏁 Conclusion

Working with files and directories from the command line might seem simple, but it's one of the most essential Linux skills. Once you’re comfortable, it opens the door to scripting, automation, and mastering the command line.

πŸ’‘ Pro tip: Create a directory like playground/ and try out everything you've learned!

Thanks for reading! If you're learning Linux too, feel free to share your favorite tricks or beginner tips in the comments. Let’s keep building confidence! πŸš€πŸ§

πŸ’¬ Let’s Connect

https://www.linkedin.com/in/ltwilliams-tech/

Top comments (0)