File and directory management is a fundamental Linux skill involving listing, viewing, creating, copying, moving, and removing files and directories.
Purpose: Lists directory contents
Common Options:
-a = Show all files (including hidden files starting with .)
-a
.
-l = Long listing format (shows permissions, owner, size, date)
-l
-al = Combined (often aliased to ll)
-al
ll
Purpose: Shows the full path of current directory
Usage: pwd
pwd
Output: /home/username
/home/username
Purpose: Navigate between directories
Special Characters:
. (dot) = Current directory
.. (dot dot) = Parent directory
..
- (dash) = Previous working directory
-
~ (tilde) = User's home directory
~
Examples:
cd /home/user # Absolute path
cd ~ # Home directory
cd .. # Parent directory
cd - # Previous directory
Purpose: Display file contents in standard output
Usage: cat filename
cat filename
Note: Good for viewing small text files
Purpose: Text editor for viewing and editing files
Also creates files if they don't exist
Basic Usage:
i = Insert mode (to type)
i
Esc = Exit insert mode
Esc
:w = Save (write)
:w
:q = Quit
:q
:wq = Save and quit
:wq
Absolute Path:
A complete path starting from the root directory
Always begins from the root directory (/ in Linux/Mac, C:\ in Windows)
Works regardless of your current location in the file system
Like giving a complete street address with city, state, and country
Examples of absolute paths:
Windows: C:\Users\John\Documents\file.txt
Linux/Mac: /home/user/documents/file.txt
Relative Path:
A path relative to your current working directory
Starts from where you currently are in the file system
Changes meaning depending on your current location
Like giving directions from your current position
Examples of relative paths:
./file.txt (file in current directory) ../file.txt
(file in parent directory)
folder/file.txt (file in subfolder of current directory)
Special Symbols in Relative Paths:
. refers to current directory
.. refers to parent directory
~ refers to home directory (in Linux/Mac)
Real-world Analogy:
Absolute path is like: "123 Main Street, New York, NY, 10001, USA"
Relative path is like: "Two blocks down, turn right"
The relative path only makes sense if you know your starting point, while the absolute path works from anywhere.
Primary Purpose: Update timestamp of existing file
Secondary Use: Creates new empty file if it doesn't exist
Usage: touch filename or touch file1 file2 file3
touch filename
touch file1 file2 file3
Purpose: Create directories
Usage: mkdir directory_name
mkdir directory_name
Important Option:
-p = Create parent directories as needed
-p
Example: mkdir -p dir1/dir2/dir3 (creates all three)
mkdir -p dir1/dir2/dir3
Purpose: Copy files and directories
Usage: cp source destination
cp source destination
Critical Option:
-r or -R = Required for copying directories recursively
-r
-R
Example: cp -r directory1 directory2
cp -r directory1 directory2
Purpose: Move OR rename files/directories
Usage:
Move: mv file.txt /other/directory/
mv file.txt /other/directory/
Rename: mv oldname.txt newname.txt
mv oldname.txt newname.txt
Note: No special options needed for directories
Purpose: Delete files and directories
Usage: rm filename
rm filename
Important Options:
-r or -R = Remove directories recursively
-f = Force (no prompts/confirmations)
-f
Warning: Be very careful! Deletion is permanent!
rm file.txt # Remove file
rm -r directory/ # Remove directory
rm -rf directory/ # Force remove without prompts
rm -f storage/* # Remove all files in directory
Purpose: Remove EMPTY directories only
Usage: rmdir directory_name
rmdir directory_name
Limitation: Directory must be completely empty
Start with a dot (.)
Examples: .bashrc, .profile
.bashrc
.profile
Only visible with ls -a
ls -a
Often contain user configuration
* (asterisk) = Matches any characters
*
Example: rm test* removes test01, test02, test03, etc.
rm test*
Example: rm *.txt removes all .txt files
rm *.txt
Press Tab to autocomplete file/directory names
Saves typing and prevents errors
Works in most modern Linux distributions
/ at end = Directory (from tab completion)
/
d in ls -l output = Directory
d
ls -l
- in ls -l output = Regular file
Absolute: Full path from root (/home/user/file)
/home/user/file
Relative: Path from current location (../file)
../file
Always double-check before using rm -rf
rm -rf
Be careful with wildcards (*)
Use -i option for interactive prompts if unsure
-i
Verify your location with pwd before deleting
Test commands with ls before using rm
ls
rm
Command
Purpose
Key Options
List files
-a, -l
Show current directory
None
cd
Change directory
., .., ~, -
cat
View file contents
vi
Edit/create files
i, :wq
touch
Create empty file
mkdir
Create directory
cp
Copy
-r (for dirs)
mv
Move/rename
Remove
-r, -f
rmdir
Remove empty dir
What command lists the contents of a directory?
ls (use -a for all files including hidden, -l for long format)
What does pwd stand for and what does it do?
Print Working Directory - displays the full path of the current directory you're in.
What are the four special characters used with cd command?
. = Current directory
.. = Parent directory
- = Previous directory
~ = Home directory
How do you navigate to your home directory?
cd ~ or just cd
cd ~
How do you go up one directory level (to parent directory)?
cd ..
What command displays the contents of a file in the terminal?
What is vi and what can it do?
A text editor that can view, edit, and create files. If the file doesn't exist, vi will create it.
What are the basic vi commands to insert text, save, and quit?
i = Insert mode
:w = Save
What command creates an empty file?
touch filename (primarily updates timestamp, but creates file if it doesn't exist)
How do you create multiple files at once with touch?
touch file1 file2 file3 (separate filenames with spaces)
What command creates a directory?
What does the -p option do with mkdir?
Creates parent directories as needed. Example: mkdir -p dir1/dir2/dir3 creates all three levels.
What command copies files and directories?
cp source destination (use -r option for directories)
Why do you need the -r option when copying directories?
The -r (recursive) option is required to copy directories and all their contents. Without it, cp will fail on directories.
What command moves or renames files?
mv source destination (move) or mv oldname newname (rename)
mv source destination
mv oldname newname
What's the difference between moving and renaming with mv?
Rename: mv oldname.txt newname.txt (same directory)
What command removes files?
What options are needed to remove a directory with rm?
-r (recursive) to remove directory and contents. Often combined with -f (force): rm -rf directory/
rm -rf directory/
What does the -f option do with rm?
Force removal without prompting for confirmation. Use with caution!
What is the difference between rm and rmdir?
rm = Removes files and directories (with -r)
rmdir = Only removes EMPTY directories
What happens if you try to use rmdir on a non-empty directory?
It will fail with an error message stating the directory is not empty.
What does ls -a show that ls alone doesn't?
Hidden files (files starting with a dot .)
What does ls -l display?
Long listing format showing permissions, owner, group, size, timestamp, and filename.
What is the common alias for ls -al?
ls -al
ll (on many distributions)
How do you identify a hidden file in Linux?
Hidden files start with a dot (.) such as .bashrc or .profile
What does the asterisk (*) wildcard do?
Matches any characters. Example: rm test* removes all files starting with "test"
What does rm -f storage/* do?
rm -f storage/*
Removes all files in the storage directory without prompting (but not subdirectories)
What key do you press for tab completion?
Tab key - autocompletes file/directory names to save typing and prevent errors
In ls -l output, how do you distinguish a directory from a file?
d at the beginning = Directory
- at the beginning = Regular file
What is an absolute path?
The full path from the root directory. Example: /home/user/documents/file.txt
/home/user/documents/file.txt
What is a relative path?
A path relative to the current directory. Example: ../documents/file.txt
../documents/file.txt
Can you create a file with vi even if it doesn't exist?
Yes. If you run vi newfile.txt and the file doesn't exist, vi will create it when you save.
vi newfile.txt
What two methods can create a new file?
Text editor like vi filename or nano filename
vi filename
nano filename
What is the safest way to remove directories?
First remove all files inside: rm directory/*
rm directory/*
Then use rmdir directory to remove the empty directory
rmdir directory
Why should you be careful with rm -rf?
It forcefully removes directories and all contents without prompting. Deletion is permanent and cannot be undone!
How do you view your current location in the filesystem?
pwd (print working directory)
What happens if you use cp without -r on a directory?
It will fail with an error saying -r not specified and will omit the directory.
-r not specified
How do you return to the previous directory you were in?
cd -
What command shows all files including configuration files in long format?
ls -al or ll
What does the / at the end of a directory name indicate?
It indicates the item is a directory (often added by tab completion)
Navigation commands:
ls -la
File operations:
touch file.txt
cat file.txt
cp file.txt file_copy.txt
mv file.txt newname.txt
rm file.txt
Directory operations:
mkdir mydir
cp -r mydir mydir_backup
rm -r mydir
rmdir empty_directory
Forgetting -r with cp or rm for directories
Using rm -rf without double-checking path
Confusing / (root) with /root (root's home)
/root
Not checking pwd before deleting files
Forgetting hidden files exist
Always verify location with pwd
Use ls to check before rm
Double-check wildcards (*)
Be extremely careful with rm -rf
Test commands on unimportant files first
Task
Where am I?
Go home
Go up
List all
Create file
touch file
Create dir
mkdir dir
cp source dest
mv old new
Delete file
rm file
Delete dir
rm -r dir
Zuletzt geändertvor einem Monat