The Ultimate Guide to Linux File and Directory Mastery
From filtering directory listings to advanced brace expansion and safe file deletion—master the Linux command line with these essential tips and tricks.
Whether you are a sysadmin or a hobbyist, the Linux terminal offers incredible power if you know the right shortcuts. Here is a curated list of tips to help you manage files and directories like a pro.
1. View Only Directory Listings
Exclude files and see only subdirectories by filtering for lines that start with the directory flag (d):
1
ls -l | egrep ^d
2. Powerful Brace Expansion
Need to create multiple files or folders at once? Use curly braces to save typing:
1
2
3
4
5
6
7
8
9
10
11
#create a new directory
mkdir my_project
#Create nested parent directories in one go
mkdir -p apps/web/logs
# Creates backup, bin, and logs folders instantly
mkdir {backup,bin,logs}
# Creates 10 text files numbered 1 to 10
touch file{1..10}.txt
3. Quick File Backups
Want to make a quick copy of a config file before editing it? Use this shorthand:
1
2
cp important.conf{,.bak}
This expands to cp important.conf important.conf.bak.
4. Find and Execute Actions
Find all .tmp files and delete them in one go:
1
find . -name "*.tmp" -type f -exec rm -v {} +
5. Create Nested Directories Instantly
Build an entire directory tree in a single command using the -p (parents) flag:
1
mkdir -p app/src/main/resources
6. Search for Text Inside Files (Recursive)
Forget where you wrote that specific note? Search for “database_secret” inside all files in the current folder and subfolders:
1
grep -rnw "." -e "database_secret"
7. Find Large Files Quickly
Locate the space-hogs on your system (files larger than 500MB):
1
2
3
4
find / -type f -size +500M 2>/dev/null
#List files recursively
ls -R /home/user
8. Switch back to the previous working directory.
Don’t type the full path to go back to where you just were. Use the dash:
1
cd -
9. Navigate to the home directory.
Don’t type the full path to go back to your home directory. Use the tild sign:
1
cd ~
10. Move to a specific absolute path
Use the cd with directory path to go to a specific directory
1
cd /usr/share
11. Empty a File Without Deleting It
If a log file is getting too huge and you want to clear the content without deleting the file itself:
1
> access.log
12. Watch Logs in Real-Time
Monitor a file as it grows—ideal for watching web server logs or application output:
1
tail -f /var/log/nginx/access.log
13. Understanding ls -l Output
When you run ls -l, the output provides a wealth of metadata. Here is a breakdown of what those characters represent:
| Component | Example | Description |
|---|---|---|
| Type | d or - | d for directory, - for regular file. |
| Permissions | rwx r-x r-x | User, Group, and Others (Read, Write, Execute). |
| Metadata | Size, Date | File size and last modification timestamp. |
Permissions Breakdown:
drwxrwxr-x: A directory where the owner and group have full access, others can only read/execute.-rw-rw-r--: A regular file where the owner and group can read/write, others can only read.
14. Advanced File Operations
Safe file management is critical when working on production systems.
Secure Backups
cp --backup=numbered /etc/hosts ~: Creates a numbered backup (e.g.,hosts.~1~) of a file. This is an excellent safety net before making manual configuration changes.
Core Concepts
cpvsmv:cp(Copy): Creates a duplicate of the data, leaving the original intact.mv(Move/Rename): Relocates the file or changes its name. On the same filesystem, this only updates the file’s metadata (inode reference), making it nearly instantaneous.
