File and Directory Management Commands
ls: List directory contents
ls - List all files and directories in current location
List all, including hidden files (those starting with a .):
Long listing format with details (permissions, owner, size, date):
Show human-readable file sizes (e.g., KB, MB):
Show all files, including hidden, in long format:
List contents of the ‘data’ directory:
pwd: Present working directory
Print the current working directory: Command that displays the full path of your current directory
cd: Change directory
Command that displays the full path of your current directory
Move to the parent directory:
Go to the root directory: Switches to the filesystem’s root.
Change to a specific directory by absolute path: Return to the previous directory:
Examples
mkdir: Make new directory
Create a single directory:
Create multiple directories at once:
Create a directory inside another existing directory:
Create nested parent-child directories with -p
:Automatically creates all missing parent directories (2025
, July
, etc.).
Create a directory with spaces in its name:
Create directories named by date:
rmdir: Remove directory
The rmdir
command is used to remove empty directories in Unix/Linux systems. It cannot delete directories that contain files or subdirectories.
Remove a single empty directory: Deletes empty_dir
if it is empty
Remove multiple empty directories at once: Removes dir1
, dir2
, and dir3
if all are empty
Use the verbose flag to display messages on deletion: Outputs a confirmation message for each directory removed
rm: Remove files or directories
If you need to delete a directory that contains files or other directories, use the rm -r
command instead:
Remove all files in the current directory (non-hidden):
Remove all files in a specific subdirectory:
Remove files of a specific type or pattern:
Remove all files and folders (minus hidden ones):
Force delete all files without confirmation (dangerous):
To delete hidden files (e.g., .gitignore
, .env
), you need to target them with:
cp: Copy files and directories
Copy a file to another location
Copy and rename file:
Copy multiple files to a directory
Copy an entire directory recursively
Ask before overwriting (interactive):
Force overwrite without asking:
Copy files while preserving timestamps, permissions, etc.:
Archive mode (preserves everything, handles symlinks, and hidden files):
Create a backup of an existing file (adds suffix):
Copy all .txt
files to another directory:
Copy hidden files (those starting with .):
Back up all scripts:
mv: Move or rename files and directories
The mv
command (move) is essential in Unix/Linux for moving or renaming files and directories
Move a file to another directory:
Move and rename a file:
Move multiple files to a directory:
Move an entire directory to another location:
Move a subdirectory into a parent directory:
Prompt before overwriting files:
Force overwrite existing files (no prompt):
Move all .log
files except hidden ones:
Move hidden files (those starting with .):
More Examples:
Moves all shell scripts into the new bash_tools/
directory under scripts/
.
Moves all results from June 2025 into an archive.
Renames the temp
directory to intermediate
.
## tree: View files and directories as a tree
Show hidden files too:
Limit depth of subdirectories:
List directories only (no files):
Show full file paths in output:
Save the directory structure for documentation:
touch: Create an empty file
The touch
command in Unix/Linux is used to create empty files and update the timestamps of existing files without modifying their contents
Create a new empty file: bash
bash touch notes.txt
Create multiple empty files at once
```bash
touch file1.txt file2.txt file3.txt
Create files with brace expansion:
Update only the access time of a file:
Update only the modification time:
Set a specific time and date: Assigns a custom timestamp (format: YYYYMMDDhhmm.ss)
cat: View a file’s content
The cat
command (short for “concatenate”) is widely used in Unix/Linux for viewing, combining, and manipulating text files
Display the contents of a single file: bash
bash cat data/info.txt
Display multiple files in sequence:
```bash
cat scripts/tool.sh logs/latest.log
Add line numbers to output
Create a new file and add content (end with Ctrl+D):
Append text to an existing file:
Append the contents of one file to another:
Display content page by page (for large files):
|
Check configuration files side by side:
Prepare a new data file from scratch: Enter data, then Ctrl+D
to save.
less, more: View large files page by page
Both less
and more
are pager programs used in Unix/Linux to view the contents of text files one screen at a time, especially useful for large files that don’t fit in one terminal window.
View a file one page at a time:
View multiple files:
Navigate between multiple log files with :n
(next) or :p
(previous). Search forward in the file:Type /pattern
and press Enter to find “pattern”. Search backward:Type ?pattern
and press Enter. Go to a specific line number:Inside less, type 123
then press Enter to jump to line 123. Quit less:Press q
.
head / tail: Display first/last lines of a file
The head
and tail
commands in Linux are used to view the beginning and end parts of text files, respectively. Both are especially useful for inspecting logs, configuration files, or any large files efficiently.
Display the first 10 lines of a file (default):
Show the first N lines (e.g., 20 lines):
Display first N lines from multiple files:
Display the last 10 lines of a file (default):
Show the last N lines (e.g., 15 lines):
Monitor multiple files simultaneously:
Extract a specific line (e.g., line 5) using head
and tail
:
|
Display lines 10 to 20:
|