Skip to content

Ubuntu - Command Line

Ubuntu, like other Linux-based operating systems, features a powerful command-line interface (CLI), also known as the Terminal. Many users, especially developers and system administrators, prefer the CLI for its efficiency and control. This chapter introduces some fundamental commands to get you started.

To open the Terminal:

  • Press Ctrl+Alt+T on your keyboard. This is the quickest shortcut.
  • Alternatively, click on the ‘Show Applications’ icon (usually a grid of dots at the bottom of the dock) and search for ‘Terminal’. Click the Terminal icon to launch it.

You will see a window with a prompt, typically showing your username and the current directory, like username@hostname:~$. This is where you type commands.

The ls command is used to list files and directories within a specified directory. If no directory is specified, it lists the contents of the current directory.

ls [options] [directoryname]
  • Options: Flags that modify the behavior of ls. Common useful options include:
  • -l: Long listing format (shows permissions, owner, size, modification date).
  • -a: Show all files, including hidden files (those starting with a dot .).
  • -h: With -l, print sizes in human-readable format (e.g., 1K, 234M, 2G).
  • -t: Sort by modification time, newest first.
  • Directoryname (optional): The directory whose contents you want to list. Defaults to the current directory.

The command outputs a list of files and directories.

To list contents of the current directory:

ls

To list contents with detailed information, including hidden files, in human-readable format:

ls -lah

This will display something like:

total 20K
drwxr-xr-x 5 user user 4.0K Jan 15 10:00 .
drwxr-xr-x 23 user user 4.0K Jan 14 09:00 ..
-rw-r--r-- 1 user user 123 Jan 15 09:30 .bash_history
-rw-r--r-- 1 user user 3.5K Jan 10 11:00 .bashrc
drwxr-xr-x 2 user user 4.0K Jan 12 16:00 Documents
-rw-rw-r-- 1 user user 675 Jan 11 14:20 script.sh

Modern alternatives like exa or lsd offer more features and color-coded output, but ls is universally available.

The clear command removes all previous commands and output from the terminal window, providing a clean slate. You can also use the shortcut Ctrl+L.

clear

None.

The terminal screen is cleared.

To get detailed information about a command, use the man (manual) command. It displays the official manual page for the specified command.

man commandname

Commandname: The name of the command you need help with.

The manual page for the command, usually including a synopsis, description, options, and examples. Press q to exit the man page viewer.

To view the manual for ls:

man ls

For a quicker, more example-focused help, consider installing and using tldr (Too Long; Didn’t Read). After installation (sudo apt install tldr), you can type:

tldr commandname

The find command is a powerful tool for searching for files and directories based on various criteria like name, type, size, and modification time.

find [path...] [expression]
  • Path: The directory/directories to start searching from (e.g., ., /home/user).
  • Expression: Criteria for the search (e.g., -name "pattern", -type f).

A list of files/directories matching the criteria.

To find all files named report.txt in the current directory and its subdirectories:

find . -name "report.txt"

To find all files ending with .log modified in the last 24 hours in your home directory:

find ~ -name "*.log" -mtime -1

A modern, user-friendly alternative is fd (often installed as fdfind). It has simpler syntax and is often faster. Example: fd report.txt.

The whoami command displays the username of the currently logged-in user.

whoami

None.

The username of the current user, for example:

myuser

The pwd command displays the full pathname of the current working directory.

pwd

None.

The absolute path of the current directory, for example:

/home/myuser/Documents