Ubuntu - Command Line
Ubuntu - Command Line Interface
Section titled “Ubuntu - Command Line Interface”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.
Accessing the Terminal
Section titled “Accessing the Terminal”To open the Terminal:
- Press
Ctrl+Alt+Ton 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.
Directory Listing (ls)
Section titled “Directory Listing (ls)”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.
Syntax
Section titled “Syntax”ls [options] [directoryname]Parameters
Section titled “Parameters”- 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.
Output
Section titled “Output”The command outputs a list of files and directories.
Examples
Section titled “Examples”To list contents of the current directory:
lsTo list contents with detailed information, including hidden files, in human-readable format:
ls -lahThis will display something like:
total 20Kdrwxr-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 .bashrcdrwxr-xr-x 2 user user 4.0K Jan 12 16:00 Documents-rw-rw-r-- 1 user user 675 Jan 11 14:20 script.shModern alternatives like exa or lsd offer more features and color-coded output, but ls is universally available.
Clearing the Screen (clear)
Section titled “Clearing the Screen (clear)”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.
Syntax
Section titled “Syntax”clearParameters
Section titled “Parameters”None.
Output
Section titled “Output”The terminal screen is cleared.
Command Help (man and tldr)
Section titled “Command Help (man and tldr)”To get detailed information about a command, use the man (manual) command. It displays the official manual page for the specified command.
Syntax (man)
Section titled “Syntax (man)”man commandnameParameters
Section titled “Parameters”Commandname: The name of the command you need help with.
Output
Section titled “Output”The manual page for the command, usually including a synopsis, description, options, and examples. Press q to exit the man page viewer.
Example (man)
Section titled “Example (man)”To view the manual for ls:
man lsFor 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 commandnameFinding Files (find and fd)
Section titled “Finding Files (find and fd)”The find command is a powerful tool for searching for files and directories based on various criteria like name, type, size, and modification time.
Syntax (find)
Section titled “Syntax (find)”find [path...] [expression]Parameters
Section titled “Parameters”- Path: The directory/directories to start searching from (e.g.,
.,/home/user). - Expression: Criteria for the search (e.g.,
-name "pattern",-type f).
Output
Section titled “Output”A list of files/directories matching the criteria.
Example (find)
Section titled “Example (find)”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 -1A modern, user-friendly alternative is fd (often installed as fdfind). It has simpler syntax and is often faster. Example: fd report.txt.
Display Current User (whoami)
Section titled “Display Current User (whoami)”The whoami command displays the username of the currently logged-in user.
Syntax
Section titled “Syntax”whoamiParameters
Section titled “Parameters”None.
Output
Section titled “Output”The username of the current user, for example:
myuserPresent Working Directory (pwd)
Section titled “Present Working Directory (pwd)”The pwd command displays the full pathname of the current working directory.
Syntax
Section titled “Syntax”pwdParameters
Section titled “Parameters”None.
Output
Section titled “Output”The absolute path of the current directory, for example:
/home/myuser/Documents