Nandishwar Singh

Home / Learn Linux / Lesson 3

Terminal speed: Tab, shortcuts, history and sudo

Type less with Tab and shortcuts, reuse old commands, and run things as root safely.

Lesson
3 of 30
Time
About 40 minutes

On this page

  1. Tab completion
  2. Keyboard shortcuts
  3. Command history
  4. Keeping commands out of history
  5. Timestamps in history
  6. root and sudo
  7. Common mistakes
  8. Practice
  9. Cheat sheet

Tab completion

Tab is the most useful key in the terminal. Type the start of a command, a file name or a folder, press Tab, and Bash finishes it for you.

Tab saves typing, but the real win is fewer mistakes. If Tab won't complete a path, the path is wrong, and you find out before you press Enter.

Try it

  1. Type cat /etc/os-r, press Tab, then Enter.
  2. Type ls /usr/share/doc/a and press Tab twice. How many folders start with a?
  3. Type whoa and press Tab.
Answer
  1. It completes to cat /etc/os-release and prints the file from lesson 1.
  2. A few dozen, depending on what's installed. If the list is very long, Bash asks before showing it all: press y to see it or n to skip.
  3. It completes to whoami.

Keyboard shortcuts

These work in Bash and in most other places where you type a line of text in a terminal. Learn the first six this week and you'll never hold down the arrow key again.

KeysDoes
Ctrl+AJump to the start of the line
Ctrl+EJump to the end of the line
Ctrl+UDelete everything before the cursor
Ctrl+KDelete everything after the cursor
Ctrl+WDelete the word before the cursor
Ctrl+LClear the screen (same as clear)
Ctrl+YPaste back what you just deleted with Ctrl+U, K or W
Alt+B / Alt+FMove back / forward one word
Ctrl+CCancel the line you're typing, or stop the running program
Ctrl+DOn an empty line, exit the shell (like exit)
Up / Down arrowsStep through earlier commands

On a Mac, Alt is the Option key, and you may need to turn on "Use Option as Meta key" in Terminal's settings (Profiles, then Keyboard) for Alt+B and Alt+F to work.

Careful with Ctrl+S: in many terminals it freezes the screen, and it looks like everything hung. Ctrl+Q unfreezes it.

Try it

  1. Type echo one two three four, but don't press Enter. Jump to the start, then to the end, then delete just four with one key.
  2. Now delete the whole line with one key, then paste it back.
Answer
  1. Ctrl+A, Ctrl+E, then Ctrl+W.
  2. Ctrl+E then Ctrl+U (or Ctrl+A then Ctrl+K) deletes it. Ctrl+Y pastes it back.

Command history

Bash remembers what you type. The up arrow walks back through it, and history prints it with numbers. Here's a short session followed by history:

$ echo one
one
$ cd /etc
$ ls -d a*
adduser.conf  alternatives  apparmor  apparmor.d  apport  apt
$ cd
$ history
    1  echo one
    2  cd /etc
    3  ls -d a*
    4  cd
    5  history

Every command has a number. Use it, or use one of these shortcuts, to run something again without typing it:

TypeRuns
!!The last command again
!3Command number 3
!lsThe last command that started with ls
!$(inside a command) The last argument of the previous command
sudo !!The last command again, with sudo in front

Bash prints the expanded command first, so you can see what actually ran:

ubuntu@linux:~$ cd /etc
ubuntu@linux:/etc$ ls -d a*
adduser.conf  alternatives  apparmor  apparmor.d  apport  apt
ubuntu@linux:/etc$ !!
ls -d a*
adduser.conf  alternatives  apparmor  apparmor.d  apport  apt
ubuntu@linux:/etc$ echo !$
echo a*
adduser.conf alternatives apparmor apparmor.d apport apt
ubuntu@linux:/etc$ cd

sudo !! is the one you'll use most. You type a command, it fails because it needed root, and you fix it in five keystrokes:

$ apt update
Error: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
Error: Unable to lock directory /var/lib/apt/lists/
Warning: Problem unlinking the file /var/cache/apt/pkgcache.bin - RemoveCaches (13: Permission denied)
Warning: Problem unlinking the file /var/cache/apt/srcpkgcache.bin - RemoveCaches (13: Permission denied)
$ sudo !!
sudo apt update
Hit:1 http://archive.ubuntu.com/ubuntu resolute InRelease
Hit:2 http://security.ubuntu.com/ubuntu resolute-security InRelease
Hit:3 http://archive.ubuntu.com/ubuntu resolute-updates InRelease
Hit:4 http://archive.ubuntu.com/ubuntu resolute-backports InRelease
9 packages can be upgraded. Run 'apt list --upgradable' to see them.

Search with Ctrl+R

Press Ctrl+R and start typing any part of an old command. Bash shows the most recent match:

(reverse-i-search)`upd': sudo apt update

Press Ctrl+R again to go further back, Enter to run it, the right arrow to edit it first, or Ctrl+G to give up. Once you're used to it, you'll find yourself searching for commands from last week instead of retyping them.

Where history lives

History is kept in memory while the shell runs and saved to ~/.bash_history when you exit. On Ubuntu it keeps 1,000 commands in memory and 2,000 in the file:

$ echo $HISTSIZE $HISTFILESIZE
1000 2000

Try it

  1. Run date, then whoami, then run date again using only history.
  2. Run ls /etc/apt, then list the same folder with ls -l without typing the path.
  3. Find your apt update from lesson 1 with Ctrl+R.
Answer
  1. !date, or press the up arrow twice.
  2. ls -l !$
  3. Press Ctrl+R and type upd. Press Enter to run it.

Keeping commands out of history

Sometimes you type something that shouldn't be saved, like a password or an API key on the command line. Ubuntu's .bashrc sets this:

$ echo $HISTCONTROL
ignoreboth

ignoreboth means two things. Repeating the same command several times in a row is only saved once, and any command that starts with a space isn't saved at all:

$ echo visible
visible
$  echo "token=abc123"
token=abc123
$ history
    1  echo visible
    2  history

The command with the leading space ran, but it's not in the list. Ubuntu sets this by default, but other distros may not, so check echo $HISTCONTROL before trusting it.

If something slipped in anyway, delete it by its number. history -d removes one entry, and history -c clears everything in memory. Then history -w writes the cleaned list over ~/.bash_history:

$ echo one
one
$ echo "password=hunter2"
password=hunter2
$ echo three
three
$ history
    1  echo one
    2  echo "password=hunter2"
    3  echo three
    4  history
$ history -d 2
$ history -w
$ history
    1  echo one
    2  echo three
    3  history
    4  history -d 2
    5  history -w
    6  history

Try it

  1. Run echo test with a space in front, then check that history doesn't show it.
  2. Run echo oops, then remove it from history.
Answer
  1. echo test (note the space), then history | tail -3. It isn't there.
  2. Run history, find the number next to echo oops, then history -d with that number, and history -w to save the change.

Timestamps in history

By default, history doesn't tell you when you ran something. Set HISTTIMEFORMAT and it will. %F %T means date and time:

$ export HISTTIMEFORMAT="%F %T  "
$ echo hello
hello
$ history
    1  2026-10-21 11:06:20  export HISTTIMEFORMAT="%F %T  "
    2  2026-10-21 11:06:20  echo hello
    3  2026-10-21 11:06:20  history

Bash quietly records the time of every command you type, so even commands from before you set it show the right time. Commands loaded from an older ~/.bash_history are different: no times were saved for them, so they all show the same one. To keep it on for good, add it to ~/.bashrc, the file Bash runs every time it starts:

$ echo 'export HISTTIMEFORMAT="%F %T  "' >> ~/.bashrc
$ source ~/.bashrc

>> adds a line to the end of a file (lesson 8 covers it), and source reloads the file in the shell you're in, so you don't have to log out and back in.

Try it

  1. Turn on timestamps for good, then open a new shell (exit, then multipass shell linux) and check that history shows times.
Answer

After the two commands above, every new shell shows dates and times in history. To check that the line is in the file:

$ tail -1 ~/.bashrc
export HISTTIMEFORMAT="%F %T  "

root and sudo

Every Linux system has one all-powerful user: root. Root can read any file, change any setting, delete anything and stop any program, with no questions asked. That's exactly why you shouldn't work as root all the time. One typo as root can wipe the system.

Normal users like ubuntu can't change the system:

$ cat /etc/shadow
cat: /etc/shadow: Permission denied

/etc/shadow holds the password hashes, so only root may read it. To run a single command as root, put sudo in front:

$ sudo head -3 /etc/shadow
root:*:20654:0:99999:7:::
daemon:*:20654:0:99999:7:::
bin:*:20654:0:99999:7:::
$ sudo whoami
root

How sudo decides

sudo doesn't ask for root's password. It asks for your password, then checks whether you're allowed. On Ubuntu, members of the sudo group may run anything:

$ groups
ubuntu adm cdrom sudo dip lxd
$ sudo -l
User ubuntu may run the following commands on linux:
    (ALL : ALL) ALL
    (ALL) NOPASSWD: ALL

The first line comes from the sudo group. The second, NOPASSWD, is a rule Multipass adds so the ubuntu user never needs a password. On WSL or a normal server, sudo asks for your password the first time, then remembers it for 15 minutes in that terminal. On Ubuntu 26.04 you'll see * for each key you press. Older versions show nothing while you type, which is normal and doesn't mean it froze.

Who may use sudo is set in /etc/sudoers and the files in /etc/sudoers.d/. Never edit them with a normal editor. Use sudo visudo, which checks the file before saving, because a typo in sudoers can lock you out of sudo completely.

A root shell: sudo -i

When you have many admin commands to run, sudo -i opens a shell as root. Watch the prompt change from $ to #:

ubuntu@linux:~$ sudo -i
root@linux:~# whoami
root
root@linux:~# pwd
/root
root@linux:~# exit
logout

Root's home folder is /root. Do what you need and exit straight away. A # prompt left open in a terminal is how accidents happen.

su, and why it doesn't work here

su (switch user) is the older way to become root. It asks for root's password, but Ubuntu locks root's password, so it can't work:

$ su -
Password:
su: Authentication failure

Root's account is locked, and it's meant to stay that way:

$ sudo passwd -S root
root L 2026-07-20 0 99999 7 -1

The L means locked. Don't "fix" it by giving root a password. Use sudo for single commands and sudo -i for a root shell. You'll also see sudo su - in old guides. It works, but sudo -i does the same thing more directly.

Try it

  1. Show the first line of /etc/shadow, which ubuntu isn't allowed to read.
  2. Open a root shell, check who you are, and leave it.
  3. Is ubuntu in the sudo group?
Answer
$ sudo head -1 /etc/shadow
root:*:20654:0:99999:7:::

Then sudo -i, whoami (prints root), exit. And yes, groups lists sudo.

Common mistakes

$ sudo cd /root
sudo: 'cd': command not found

To work inside root-only folders, use sudo -i instead.

Practice

  1. Run ls /etc/apt. Then, using history, run the same command with -l added, without retyping the path.
Answer
$ ls /etc/apt
apt.conf.d   keyrings       sources.list    trusted.gpg.d
auth.conf.d  preferences.d  sources.list.d
$ ls -l !$
ls -l /etc/apt
total 28
drwxr-xr-x 2 root root 4096 Oct 21 11:03 apt.conf.d
drwxr-xr-x 2 root root 4096 Apr  7  2026 auth.conf.d
drwxr-xr-x 2 root root 4096 Apr  7  2026 keyrings
drwxr-xr-x 2 root root 4096 Jul 20 16:38 preferences.d
-rw-r--r-- 1 root root  270 Jul 20 16:38 sources.list
drwxr-xr-x 2 root root 4096 Jul 20 16:39 sources.list.d
drwxr-xr-x 2 root root 4096 Jul 20 16:34 trusted.gpg.d
  1. Run apt list --installed without sudo and count the lines with wc -l (lesson 8 explains the |). Then run history and re-run that command by its number.
Answer
$ apt list --installed | wc -l

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

678
$ history
    1  apt list --installed | wc -l
    2  history
$ !1
apt list --installed | wc -l

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

678

The WARNING line is apt reminding you that its output format may change between versions, so scripts shouldn't depend on it. It appears whenever apt's output goes into another command. For counting by hand it doesn't matter.

  1. Type cat /etc/hosts, press Ctrl+A, and turn it into sudo cat /etc/hosts before pressing Enter.
Answer

Ctrl+A jumps to the start, so you type sudo there and press Enter.

  1. Run echo "api_key=12345" so that it never appears in history, and prove it.
Answer
$  echo "api_key=12345"
api_key=12345
$ history
    1  history

history shows only itself.

  1. Using one sudo command, list the files in /root, then check what sudo -l says you're allowed to run.
Answer
$ sudo ls -la /root
total 24
drwx------  3 root root 4096 Oct 21 11:06 .
drwxr-xr-x 19 root root 4096 Oct 21 11:06 ..
-rw-------  1 root root   16 Oct 21 11:06 .bash_history
-rw-r--r--  1 root root 3106 Apr 20  2026 .bashrc
-rw-r--r--  1 root root  132 Apr 20  2026 .profile
drwx------  2 root root 4096 Oct 21 11:01 .ssh
$ sudo -l
User ubuntu may run the following commands on linux:
    (ALL : ALL) ALL
    (ALL) NOPASSWD: ALL
  1. Turn on history timestamps permanently, run three commands, and show the last four lines of history with their times.
Answer
$ echo 'export HISTTIMEFORMAT="%F %T  "' >> ~/.bashrc
$ source ~/.bashrc
$ date
$ whoami
$ uptime
$ history | tail -4

Every line shows the date and time it ran.

Cheat sheet

Keys or commandWhat it does
Tab, Tab TabComplete, or list the choices
Ctrl+A / Ctrl+EStart / end of the line
Ctrl+U / Ctrl+K / Ctrl+WDelete before / after the cursor / one word
Ctrl+YPaste back what you deleted
Ctrl+LClear the screen
Ctrl+C / Ctrl+DCancel / exit the shell
Ctrl+RSearch history
historyNumbered list of past commands
!!, !n, !text, !$Last command, number n, last starting with text, last argument
sudo !!Last command with sudo
command (leading space)Not saved in history (Ubuntu default)
history -d n, history -c, history -wDelete one, clear all, save to file
export HISTTIMEFORMAT="%F %T "Show times in history
sudo commandRun one command as root
sudo -lWhat you may run with sudo
sudo -iRoot shell (exit to leave)
sudo visudoThe only safe way to edit sudoers
sudo passwd -S rootRoot's password state (L = locked)