Skip to content

Phase 3 · Free

3.5 Redirection, Pipes, Permissions, and sudo

Every command produces output. You can send that output to a file, or feed it into another command. Chaining small commands is how Linux does big jobs. This lesson also covers the first look at permissions, because sooner or later a command will say "Permission denied."

Phase 3. Lesson 5 of 7. About 30 minutes.

Plain entry

Every command produces output. You can send that output to a file, or feed it into another command. Chaining small commands is how Linux does big jobs. This lesson also covers the first look at permissions, because sooner or later a command will say “Permission denied.”

Terms

  • stdout / stderr: a command’s normal output and its error output. Two separate streams.
  • Redirection: sending a stream to a file. > overwrite, >> append, 2> errors.
  • PipeThe | operator that sends one command's output directly into the next command's input, letting small tools be chained into a single job. Read more → (|): sends one command’s output into the next command.
  • Permissions: who can read (r), write (w), or execute (x) a file: the owner, the group, everyone else.
  • sudoA Linux command that runs a single command with administrator (root) privileges, after checking that your account is allowed to do so. Read more →: run one command as root. Logged in /var/log/auth.log. Ubuntu 26.04 uses sudo-rs, a Rust rewrite, as the default sudo; the commands and the sudoers file work the same.

Depth

echo "line one" > notes.txt
echo "line two" >> notes.txt
cat notes.txt
ls /root 2> errors.txt
cat errors.txt

Expected output:

line one
line two
ls: cannot open directory '/root': Permission denied

Pipes:

ls /etc | wc -l                         # how many items in /etc
ps aux | grep -i ssh                    # find a process
cat /etc/passwd | cut -d: -f1 | sort    # every username, sorted
df -h | grep -v tmpfs                   # disk usage without the noise

Permissions, first look:

ls -l notes.txt
chmod 600 notes.txt
ls -l notes.txt
-rw-rw-r-- 1 you you 18 Nov  1 09:30 notes.txt
-rw------- 1 you you 18 Nov  1 09:30 notes.txt

600 means owner read and write, nobody else anything. Numbers: r=4, w=2, x=1, added up per group. Phase 6 goes deep.

chown changes the owner and needs sudo: sudo chown root:root notes.txt.

sudo, honestly

sudo is not a magic fix for “Permission denied.” The error is often telling you that you are in the wrong place or touching something you should not. Ask why before adding sudo. sudo !! reruns the last command with sudo when you did mean it.

Going further

2>&1 merges errors into normal output: command > all.txt 2>&1. tee writes to a file and the screen at once: df -h | tee report.txt. To write a root-owned file from a pipe, use echo x | sudo tee /etc/somefile; sudo echo x > /etc/somefile fails because the redirect runs as you.

Why it works this way

Every program has two outputs: normal output and errors. They are kept separate so a pipe can carry your data to the next program while errors still reach your screen. That is why 2> exists, and why 2>/dev/null hides only the noise. It is also why sudo echo x > /etc/somefile fails: the shellThe program that reads the commands you type, runs them, and shows the results; Bash is the default on most Linux systems. Read more → sets up the > before sudo runs, so the redirect is done by you, not root.

Self-check

  1. Which symbol appends, which overwrites?
  2. Count the lines in /etc/passwd using a pipe.
  3. What does chmod 644 give the owner, group, and others?

Answers: (1) >> appends, > overwrites. (2) cat /etc/passwd | wc -l. (3) Owner read/write, group read, others read.

Sources