Need to sort through files or folders in Linux? The terminal provides powerful one-liners to manipulate directories without GUI tools. These commands work in any Linux terminal (Ubuntu, Fedora, Arch, etc.), and let you create, move, delete, and organize files with speed and precision—no mouse required.
What You'll Need
- A Linux terminal (run via Ctrl+Alt+T in most desktop environments)
- Basic familiarity with command-line syntax (optional but helpful)
- Root or sufficient permissions for advanced operations (e.g., `sudo` for system-wide actions)
- An example directory structure (e.g., ~/Downloads or /home/user/project)
Step-by-Step
-
Navigate to a directory
Use `cd` to change directories. Replace `
` with an actual folder: cd ~/DocumentsTo return to the home directory, use:cd ~orcdExample: Navigate to a project folder:cd ~/my-project -
List directory contents
Use `ls` to see files/folders. Add `-l` for long listings (permissions, size):
ls -lAdd `-a` to show hidden files (e.g., `.bashrc`):ls -la -
Create a new directory
Use `mkdir` to make folders. For nested folders, separate paths with slashes:
mkdir new-foldermkdir parent/child-folderFor root permissions, use `sudo`:sudo mkdir /srv/webWarning: This requires root access and can overwrite existing files. -
Move or rename files/folders
Use `mv` to rename or move. For cross-directory moves, omit the destination:
mv old-name.txt ~/archiveTo rename:mv old-name.txt new-name.txtIf that did not work: Check if the target path exists. If files are locked, use:chflags -R noupdate(Only works in macOS; replace with `sudo` + `ipcallow` for Linux if needed.)&& mv target -
Delete files or folders
Files: Use `rm` with caution. For small files:
rm file.txtFor force-deleting (no prompts):rm -f file.txtFolders: Use `rm -r` to delete recursively (⚠️ permanent!):rm -r folder-nameTo delete empty folders only:rmdir folder-nameIf that did not work: Check permissions with `ls -ld folder-name` and use `sudo` if needed. -
Copy files/folders
Use `cp`. To copy a file:
cp file.txt ~/backupTo copy recursively (folders too):cp -r folder-name ~/backupNotes: Avoid overwriting existing files; use `-i` for interactive confirmation:cp -i file.txt ~/backup -
Create and use symbolic links
Use `ln -s` to make shortcuts. Example:
ln -s ~/old-link ~/new-linkWhy use this? Link files across partitions or share a file via a single path. Delete the link (not the original) with:rm ~/new-link -
Change working directory
Use `pwd` to verify current location:
pwdCombine with `ls -l` to see relative paths:ls -l --relative -
View/move files by extension
Filter files with `grep` or `find`:
ls *.txt(lists all .txt files)find . -name "*.pdf"(finds PDFs recursively) Example: List all .jpg images in a folder:ls *.jpg -
Compress files/folders
Use `tar` to create archives (e.g., for backup):
tar -cvf archive.tar folder-nameFor ZIP-style archives (Linux 12+):zip archive.zip folder-nameWarning: Compressed files take up more space than originals. -
Check disk space
Use `df` to see mounted drives:
df -hdu -sh folder-name(estimates folder size)
Common Mistakes
-
Forgetting to use absolute paths
Example: `mv ~/old /new` fails if `/new` doesn’t exist. Always specify full paths:
mv ~/old ~/new - Deleting folders recursively without confirmation Use `rm -i` for interactive prompts or `rmdir` only for empty folders.
-
Overwriting files with `cp`
Add `-i` to confirm overwrite:
cp -i file.txt ~/backup - Using `rm -r` on non-empty folders Always verify with `ls` before running `rm -r`.
Tips
- Use tab completion Press Tab to autocomplete paths (e.g., `mkdir my-p` → `my-project`).
-
Pipes (`|`) for complex commands
Example: List files > filter > sort:
ls | grep .txt | sort -
Search for commands with `which`
Check if a command is installed:
which ls -
Aliases for common tasks
Create shortcuts in `~/.bashrc`:
alias ll='ls -l'(Reload with `source ~/.bashrc`.) -
Redirection for logging
Save output to a file:
ls -l > mylist.txt
Troubleshooting
-
Command not found
Check if the command is installed:
which lsUpdate your system:sudo apt update && sudo apt upgrade(Debian/Ubuntu)sudo dnf upgrade(Fedora/RHEL) -
Permission denied
Use `sudo` (for root access) or check folder permissions:
ls -ld folder-nameFix permissions with:chmod +x file.txt -
`rm` or `mv` hangs
Restart the terminal or check for locked files (e.g., in use by another process):
lsof | grep filename -
Symbolic links broken
Verify with:
ls -l ~/link-nameRepair with:ln -sf(Replace `~/link-name ` with the correct source.) -
Disk full (`df` shows 100%)
Free up space:
du -sh * | sort -n(lists largest folders) Delete unnecessary files:rm -r /tmp/*(⚠️ caution: deletes temporary files.)
Frequently Asked Questions
How do I create hidden folders in Linux?
Use `mkdir` with a dot prefix:
mkdir .hidden-folder
These folders won’t appear in `ls` by default but will be accessible in the terminal.
Can I move folders across partitions using terminal?
Yes! Use `mv` with absolute paths:
mv /mnt/external/folder ~/Documents
Warning: Ensure the target partition has enough space and is writable.
What’s the difference between `cp` and `cp -r`?
`cp` copies single files; `cp -r` copies entire directories (recursively). Example:
cp file.txt ~/backup vs.
cp -r folder-name ~/backup
How do I permanently delete a file?
Use `rm` without `-i` or `-f` flags:
rm file.txt
rm -rf folder-name (⚠️ only for empty folders or trusted locations.)
Why won’t `ln -s` create a link?
Check permissions and ensure the source file exists:
ls -l ~/source-file
If permissions are wrong, use `sudo` or fix with:
chmod +x ~/source-file
Can I undo `rm`?
Not directly, but use `trash-cli` (install with `sudo apt install trash-cli`) or `sudo apt install files` (for GUI trash recovery). For critical files, back them up first.