DevOps Peach

Linux · Linux Basics – Files, Directories, and Permissions · Lesson 3 of 6

View, create, and remove files and directories

Perform basic file operations without deleting the wrong data.

Practice interactively →

List and inspect

ls -la shows hidden entries in long format. Useful variations: ls -lh prints human-readable sizes, ls -lt sorts by modification time (newest first), and ls -R lists subdirectories too. stat file.txt shows the full metadata: size, owner, permissions, and access/modify/change times. wc -l file.txt counts lines, which is handy for logs and CSV files.

flowchart LR
  L["ls -la<br/>see what is there"] --> R["cat, less<br/>read contents"]
  R --> M["mkdir -p, touch<br/>create"]
  M --> G["Wildcards *.log<br/>expand first"]
  G --> CHK["ls *.log<br/>preview the match"]
  CHK --> RM["rm -i<br/>remove deliberately"]
  %% step L: List with ls -la to see hidden files, sizes and permissions.
  %% step R: Read small files with cat and long ones with less.
  %% step M: Create directories with mkdir -p and empty files with touch.
  %% step G: The shell expands wildcards before the command runs.
  %% step CHK: Run ls with the same pattern first to see exactly what will match.
  %% step RM: Only then remove, interactively if unsure. There is no recycle bin.

Read file contents

Tool When to use it
cat Short files, or joining several files
less Long files; /text searches, q quits
head -n 20 The first lines
tail -n 50 The last lines
tail -f Follow a log as new lines arrive

head and tail show 10 lines by default.

Create files and directories

touch empty.txt creates an empty file or updates timestamps. mkdir -p a/b/c creates missing parents. Redirection also creates files: echo "hello" > note.txt overwrites the file, while >> appends to it. Mixing them up is a common way to wipe a config file.

An editor can create a file when it saves. nano shows its shortcuts on screen and is easiest for beginners. In vi, press i to insert text, Esc to return to command mode, :wq to write and quit, and :q! to quit without saving.

Wildcards

The shell expands patterns before the command runs: * matches any characters, ? exactly one, and [0-9] one character from a set. ls *.log therefore receives a list of file names, not the pattern. Run echo rm *.tmp first to preview what a pattern would delete.

Remove deliberately

rmdir removes only empty directories. rm -r project traverses a directory tree; -f suppresses prompts and some errors, so do not add it by habit. rm -i asks before each file and is a safer habit while learning. A normal shell has no trash can for rm.

Before a recursive operation, use pwd and ls to verify the target. Prefer a specific path inside the exercise directory over a broad wildcard.

Practice interactively →