IntroToLinux

8. Essential File Commands


πŸ“‚ File and Directory Operations

πŸ” Viewing and Navigation

Command Purpose Example
ls πŸ“‹ List directory contents ls
ls -l πŸ“Š Long format with details ls -l
ls -la πŸ‘οΈ Include hidden files ls -la
ls -lh πŸ“ Human readable sizes ls -lh
pwd πŸ“ Print working directory pwd
cd 🚢 Change directory cd /etc
cd .. ⬆️ Go up one level cd ..
cd - ↩️ Go to previous directory cd -

✨ Creating Files and Directories

# πŸ“„ Create files
touch filename.txt
touch file1.txt file2.txt file3.txt

# πŸ“ Create directories
mkdir dirname
mkdir project

# πŸ—‚οΈ Create nested directories
mkdir -p path/to/nested/directories
mkdir -p project/src/main/java

πŸ“‹ Copying and Moving

# πŸ“„βž‘οΈπŸ“„ Copy files
cp file1.txt file2.txt
cp report.txt backup.txt

# πŸ“βž‘οΈπŸ“ Copy directories (recursive)
cp -r directory1 directory2
cp -r project project-backup

# πŸ”„ Move/rename files
mv oldname.txt newname.txt
mv report.txt final-report.txt

# πŸ“¦ Move to different location
mv file.txt /path/to/destination/
mv document.txt ~/Documents/

πŸ—‘οΈ Removing Files and Directories

# ❌ Remove files
rm filename.txt

# ❓ Interactive removal (asks before deleting)
rm -i filename.txt

# πŸ“βŒ Remove empty directories
rmdir empty_directory

# πŸ—‚οΈβŒ Remove directories with contents
rm -r directory_with_contents

# ⚠️ Force remove (be careful!)
rm -rf directory

⚠️ Warning: rm -rf is permanent! There’s no recycle bin in Linux!


πŸ“– Viewing File Contents

Command Purpose Best For
cat filename.txt πŸ“„ Display entire file Small files
less filename.txt πŸ“– View with paging Large files
more filename.txt πŸ“‘ View with paging (older) Legacy systems
head filename.txt ⬆️ First 10 lines Quick preview
tail filename.txt ⬇️ Last 10 lines Log files
head -20 file.txt ⬆️ First 20 lines Custom preview
tail -20 file.txt ⬇️ Last 20 lines Recent logs

πŸ’‘ Pro Tip: Navigation in less

Key          Action
───────────────────────────────
Space        ⬇️ Next page
b            ⬆️ Previous page
/pattern     πŸ” Search forward
?pattern     πŸ”Ž Search backward
q            πŸšͺ Quit

✏️ Editing Files

# πŸ“ Simple text editor (beginner-friendly)
nano filename.txt

# πŸ’Ύ Save in nano: Ctrl+O, then Enter
# πŸšͺ Exit nano: Ctrl+X

🌐 Downloading Files

# ⬇️ Download files from internet
wget https://example.com/file.txt

# πŸ“₯ Download with custom name
wget -O newname.txt https://example.com/file.txt

# πŸ“Š Download with progress bar
wget --progress=bar https://example.com/largefile.zip

Next: β†’ Getting Help With Commands
Previous: ← Essential Navigation Commands
Lesson Home: ↑ Lesson 2: The Shell
Course Home: βŒ‚ Introduction to Linux