Skip to main content
    Courses/Command Line/File Management Commands

    Lesson 3 • Beginner

    File Management Commands 📁

    By the end of this lesson you'll create files and folders, copy and move them, rename and delete them safely, read their contents, and act on dozens of files at once with wildcards — the everyday muscle-memory of working in a terminal.

    What You'll Learn

    • Create files and folders with touch and mkdir -p
    • Copy files and whole directories with cp -r
    • Move and rename in one step with mv
    • Delete safely with rm -i and rmdir — and avoid the rm -rf trap
    • Read file contents with cat and less
    • Match many files at once with wildcards (*, ?, [])

    1️⃣ Creating Files and Directories

    Every project starts as empty files and folders. touch makes an empty file (or, if it already exists, just updates its "last modified" time). mkdir makes a directory. The flag you'll use constantly is -p: it creates every missing parent folder in a path, so one command can build a deep structure. Read this worked example, run it, then check the output matches.

    Worked example: touch and mkdir -p
    # touch — create an EMPTY file (or update its timestamp if it exists).
    touch index.html                 # one file
    touch style.css app.js           # several at once, space-separated
    
    # mkdir — make a directory (folder).
    mkdir projects                   # one directory
    
    # mkdir -p — make a whole nested path in one go.
    # Without -p, "mkdir src/components/ui" fails if src/ doesn't exist yet.
    mkdir -p src/components/ui       # creates src, src/components AND src/components/ui
    
    # See what you just made (-F adds a / after directory names).
    ls -F
    Output
    app.js  index.html  projects/  src/  style.css
    These commands create files on disk — run them in your own terminal (macOS, Linux, or WSL on Windows). The output is what ls -F prints afterwards.

    2️⃣ Reading File Contents

    Before you copy or delete something, you usually want to look inside it. cat dumps a file's entire contents to the screen — perfect for short files. For long files (like logs) that would scroll off the top, use less: it opens a scrollable viewer you move through with the arrow keys, and you quit by pressing q.

    Worked example: cat and less
    # First, put some text in a file so there is something to read.
    # >  writes (overwrites!) the file.   >>  appends to the end.
    echo "Roses are red"     > poem.txt
    echo "Violets are blue"  >> poem.txt
    echo "Bash is handy"     >> poem.txt
    
    # cat — print the WHOLE file to the screen at once.
    cat poem.txt
    
    # less — open a SCROLLABLE viewer for long files.
    # (Arrow keys / Space to scroll, press q to quit.) Best for big logs.
    less poem.txt
    Output
    Roses are red
    Violets are blue
    Bash is handy
    Run these in your own terminal. The output shown is what cat poem.txt prints; less opens an interactive viewer you quit with q.

    🎯 Your Turn: scaffold a project folder

    Fill in the three blanks marked ___ using the hints in the comments, then run it and check your output against the expected lines.

    Fill in the blanks, then run it
    # 🎯 YOUR TURN — build a small project folder.
    # Replace each ___ then run it in your terminal.
    
    # 1) Make the nested folder src/pages in ONE command
    mkdir ___ src/pages         # 👉 the flag that creates parent folders: -p
    
    # 2) Create an empty file called index.html
    ___ index.html              # 👉 the command that makes an empty file: touch
    
    # 3) Create two files at once: style.css and app.js
    touch ___ ___               # 👉 just list both names, separated by a space
    
    # Check your work
    ls -F
    
    # ✅ Expected output:
    #    app.js  index.html  src/  style.css
    Type your answers in place of each ___, run it in your terminal, and confirm ls -F shows the expected files.

    3️⃣ Copy, Move, Rename, and Delete

    These four jobs are the core of file management. cp duplicates (original stays); add -r to copy a directory. mv does double duty — give it a new name in the same folder and it's a rename; give it a different folder and it's a move. rm deletes, and there is no undo. rmdir removes empty folders only — a built-in safety check.

    Worked example: cp, mv, rm, rmdir
    # cp — COPY (original stays put).
    cp file.txt backup.txt           # copy a file to a new name
    cp -r src/ src-backup/           # copy a DIRECTORY — -r ("recursive") is required!
    
    # mv — MOVE or RENAME (the original is gone from its old spot).
    mv old-name.txt new-name.txt     # same folder + new name = a rename
    mv report.pdf archive/           # different folder = a move
    mv draft.txt archive/final.txt   # move AND rename in one step
    
    # rm — REMOVE. PERMANENT: there is NO recycle bin / trash.
    rm file.txt                      # delete one file
    rm -r old-folder/                # delete a directory AND everything inside it
    rm -i notes.txt                  # -i = interactive: asks y/n before deleting
    
    # rmdir — remove an EMPTY directory only (safer than rm -r).
    rmdir empty-folder
    
    # Confirm what remains
    ls -F
    Output
    rm: remove 'notes.txt'? y
    archive/  backup.txt  new-name.txt  src/  src-backup/
    These commands move and delete files on disk — run them in your own terminal. rm -i pauses to ask y/n; the final line is what ls -F shows.

    DANGER: rm is permanent — treat rm -rf with extreme care

    Unlike dragging to the trash, rm erases files with no recovery. The combination rm -rf is the most dangerous command in this lesson: -r means "recurse into directories" and -f means "force, never ask and never complain". Together they will silently wipe out an entire folder tree. Never run rm -rf /, rm -rf ~, or rm -rf * casually, and beware a stray space — rm -rf / tmp/junk tries to delete your whole system instead of /tmp/junk. The habit that keeps you safe: run the command as ls first to see exactly what matches, and reach for rm -i (interactive) when deleting anything you care about.

    4️⃣ Wildcards: Acting on Many Files at Once

    A wildcard (or "glob") is a pattern the shell expands into a list of matching filenames before the command even runs. * matches any run of characters, ? matches exactly one character, and [abc] matches one character from the set. This is how you copy or delete dozens of files in a single line — and exactly why previewing with ls first is so important.

    Worked example: *, ?, and [...]
    # Wildcards (globs) let ONE command act on MANY files.
    #   *      matches any run of characters   (zero or more)
    #   ?      matches exactly ONE character
    #   [...]  matches one character from the set
    
    # Set up a few files to match against.
    touch report.txt notes.txt photo.jpg img1.png img2.png img9.png
    
    # ALWAYS preview a glob with ls BEFORE using it to delete or move.
    ls *.txt                 # everything ending in .txt
    ls img?.png              # img + ONE char + .png
    ls img[12].png           # img1.png or img2.png — NOT img9.png
    
    # Now use the same patterns with real commands.
    cp *.png backup/         # copy every PNG into backup/
    rm img[12].png           # delete only img1.png and img2.png
    Output
    notes.txt  report.txt
    img1.png  img2.png  img9.png
    img1.png  img2.png
    Run these in your own terminal. The output shows the three ls previews; the last two lines (cp and rm) act on the files those patterns matched.

    Pro Tip

    Preview every glob with ls before you destroy anything. The shell expands *.tmp the same way for ls and rm, so ls *.tmp shows you the exact list rm *.tmp would delete. Get into the habit of running the ls version, reading the list, and only then swapping in rm or mv. This one reflex prevents the most common "oh no" moment for beginners.

    🎯 Your Turn: clean up with wildcards

    Fill in each ___ with the right glob, then run it and compare your result to the expected output.

    Fill in the blanks, then run it
    # 🎯 YOUR TURN — clean up a messy folder with wildcards.
    # The folder contains: a.log  b.log  notes.txt  cat.jpg  cat.png
    
    # 1) List every .log file (preview before you delete anything!)
    ls ___                  # 👉 the glob for "anything ending in .log": *.log
    
    # 2) Copy BOTH cat pictures (cat.jpg and cat.png) into pics/
    cp cat.___ pics/        # 👉 one wildcard char matches the single letter: ?
    
    # 3) Delete every .log file now that you've previewed them
    rm ___                  # 👉 reuse the same glob as step 1: *.log
    
    ls
    
    # ✅ Expected output:
    #    a.log  b.log
    #    cat.jpg  notes.txt  pics
    Replace each ___ with a wildcard pattern, run it in your terminal, and check the files that remain match the expected output.

    Common Errors (and the fix)

    • cp: -r not specified; omitting directory 'src/' — you tried to copy a folder without -r. Add it: cp -r src/ src-backup/.
    • You deleted the wrong thing with rm -rf — and there's no undo. The only real fix is prevention: preview with ls first, avoid -f unless you must, and use rm -i for anything important.
    • A file silently disappeared after mv or cp. The destination already existed and got overwritten without warning. Use mv -i / cp -i (prompt) or -n (never overwrite) next time.
    • rmdir: failed to remove 'logs': Directory not emptyrmdir only deletes empty folders. Empty it first, or use rm -r logs if you really mean to delete its contents too.
    • mkdir: cannot create directory 'src/pages': No such file or directory — the parent src/ doesn't exist yet. Add -p so it's created for you: mkdir -p src/pages.

    📋 Quick Reference

    CommandWhat it does
    touch fileCreate an empty file (or update its timestamp)
    mkdir -p a/b/cCreate nested directories, parents included
    cp -r src dstCopy a file or (with -r) a whole directory
    mv old newMove, or rename when the folder stays the same
    rm -i fileDelete a file, asking y/n first (no trash!)
    rm -r dirDelete a directory and everything in it
    rmdir dirDelete an empty directory only
    cat filePrint a file's full contents
    less fileScrollable viewer for long files (q to quit)
    ls *.txtMatch files with a wildcard (preview before rm!)

    Frequently Asked Questions

    Q: Does rm move files to the trash or recycle bin?

    No. rm deletes files immediately and permanently — there is no trash to restore from. Always run an ls with the same name or glob first to confirm exactly what will be removed, and use rm -i to be asked y/n before each delete.

    Q: What is the difference between rm -r and rmdir?

    rmdir only removes a directory that is already empty, so it fails loudly if you target the wrong thing — that safety is the point. rm -r removes a directory and everything inside it. Use rmdir for empty folders and rm -r (carefully) when you genuinely want to delete contents too.

    Q: Why does cp say it omitted a directory?

    cp copies a single file by default. To copy a directory and its contents you must add -r (recursive): cp -r src/ src-backup/. Without -r, cp refuses and prints something like "cp: -r not specified; omitting directory 'src/'".

    Q: Will mv or cp warn me before overwriting an existing file?

    By default, no — they silently replace the destination if it already exists, and the old contents are gone. Add the -i flag (cp -i, mv -i) to be prompted before any overwrite, or -n (no-clobber) to refuse overwriting entirely.

    Q: What is the safest way to use wildcards like * with rm?

    Run the exact same pattern with ls first (for example ls *.tmp) and read the list it prints. Only once you are happy that the list is correct should you swap ls for rm. The glob expands the same way for both commands, so the ls preview shows precisely what rm would remove.

    Mini-Challenge: Build & Tidy a Website Folder

    No blanks this time — just a brief and an outline. Work through the steps in your terminal, then check your folder against the expected ls output in the comments. This is exactly the kind of setup-and-cleanup you'll do at the start of real projects.

    🎯 Mini-Challenge: your commands here
    # 🎯 MINI-CHALLENGE: scaffold and tidy a website folder.
    #
    # 1. Create this structure in as few commands as you can:
    #       site/
    #         css/
    #         js/
    #    (hint: mkdir -p site/css  and  mkdir -p site/js)
    # 2. Create empty files: site/index.html, site/css/main.css, site/js/app.js
    # 3. Copy site/index.html to site/about.html
    # 4. Rename site/js/app.js to site/js/main.js  (use mv)
    # 5. Preview, then delete every .html file in site/  with a single glob
    #
    # ✅ After step 4, "ls site" should show:
    #       about.html  css  index.html  js
    # ✅ After step 5, "ls site" should show:
    #       css  js
    
    # your commands here
    Run each step in your own terminal and verify with ls site at the checkpoints noted in the comments.

    🎉 Lesson Complete!

    • touch creates empty files; mkdir -p builds nested folders in one go
    • cp copies (use -r for directories); mv moves and renames
    • rm is permanent — there's no trash; rm -i asks first, and rm -rf is to be feared
    • cat prints whole files; less scrolls long ones (q to quit)
    • ✅ Wildcards *, ?, and [...] act on many files — always ls them first
    • Next lesson: Text Processing — search and transform file contents with grep, sed, and awk

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy PolicyTerms of Service