Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills
30.10.2024

Ubuntu Command Line: Bash Commands

The command line in Ubuntu, also known as the terminal, provides powerful tools for interacting with the system. It allows users to run commands directly to manage files, install software, configure system settings, and much more. At the heart of the Ubuntu terminal is Bash, a command-line shell that interprets and executes commands.

In this article, we will explore some of the most common and useful Bash commands for Ubuntu, giving you a solid foundation for working efficiently in the terminal.


What is Bash?

Bash (short for Bourne Again Shell) is the default command-line shell in Ubuntu and most Linux distributions. It is a text-based interface that allows users to issue commands directly to the operating system. Bash is extremely powerful and supports features such as scripting, command history, aliases, and more.

The terminal can be opened in Ubuntu by pressing Ctrl + Alt + T or by searching for “Terminal” in the application menu.


Basic Bash Commands

Below are some basic commands that are essential for navigating and performing operations in the Ubuntu terminal.

1. pwd – Print Working Directory

The pwd command shows the current directory you are working in.

pwd

2. ls – List Directory Contents

The ls command lists the files and directories in the current directory.

ls

You can add options to display more information, such as file permissions, file sizes, and more:

ls -l
  • -l: Long format, showing file details (permissions, owner, size, and modification date).
  • -a: Show hidden files (files starting with .).

3. cd – Change Directory

The cd command is used to change the current directory.

cd /path/to/directory
  • To navigate to your home directory, simply type:
    cd
  • To move up one directory:
    cd ..

4. mkdir – Make Directory

The mkdir command creates a new directory.

mkdir new_directory

5. rmdir – Remove Directory

The rmdir command removes an empty directory.

rmdir directory_name

To remove a directory and its contents, use the rm command with the -r option:

rm -r directory_name

File Management Commands

Managing files is a core part of using the command line. Here are some key commands for creating, viewing, and deleting files.

1. touch – Create a New File

The touch command creates an empty file or updates the timestamp of an existing file.

touch file_name.txt

2. cp – Copy Files and Directories

The cp command is used to copy files or directories.

cp source_file destination

To copy a directory and its contents, use the -r option:

cp -r source_directory destination_directory

3. mv – Move or Rename Files

The mv command moves or renames files and directories.

  • To move a file:
    mv file_name /new/directory
  • To rename a file:
    mv old_name new_name

4. rm – Remove Files

The rm command deletes files. Use it with caution, as deleted files are not moved to the Trash.

rm file_name.txt

To delete directories and their contents, use the -r option:

rm -r directory_name

Viewing and Editing Files

The terminal offers several ways to view and edit text files directly.

1. cat – View File Contents

The cat command displays the contents of a file.

cat file_name.txt

2. less – View File Contents Page by Page

The less command is useful for viewing large files, as it allows you to scroll through the contents one page at a time.

less file_name.txt

3. nano – Edit Files

The nano command opens the Nano text editor in the terminal, allowing you to edit files directly.

nano file_name.txt

4. head and tail – View Beginning and End of Files

  • head shows the first 10 lines of a file:
    head file_name.txt
  • tail shows the last 10 lines of a file:
    tail file_name.txt

System Information and Management

These commands provide useful information about your system and allow you to manage running processes.

1. top – View Running Processes

The top command provides a dynamic, real-time view of running processes on your system.

top

2. ps – Display Current Processes

The ps command displays information about the processes running in your session.

ps

3. df – Check Disk Usage

The df command shows the amount of disk space available on the file system.

df -h

The -h option provides the output in a human-readable format (MB, GB).

4. free – Check Memory Usage

The free command displays information about the system’s memory (RAM) usage.

free -h

The -h option shows the memory usage in a human-readable format (MB, GB).

5. kill – Terminate a Process

The kill command allows you to terminate a running process using its PID (Process ID).

  1. Find the PID of the process using ps or top.
  2. Use the kill command followed by the PID:
    kill 1234

Package Management with APT

In Ubuntu, software packages are managed using APT (Advanced Package Tool). Here are some basic commands for installing, updating, and removing software.

1. apt update – Update Package List

The apt update command refreshes the list of available packages and their versions.

sudo apt update

2. apt upgrade – Upgrade Installed Packages

The apt upgrade command installs the latest versions of all installed packages.

sudo apt upgrade

3. apt install – Install New Packages

To install a new package using APT, use the apt install command:

sudo apt install package_name

4. apt remove – Remove Packages

To remove a package from your system:

sudo apt remove package_name

File Permissions

Understanding file permissions is important for managing access control in Ubuntu.

1. chmod – Change File Permissions

The chmod command changes the read, write, and execute permissions for a file or directory.

  • To grant execute permissions to a script:
    chmod +x script.sh

2. chown – Change File Owner

The chown command changes the ownership of a file or directory.

sudo chown new_owner file_name.txt

Conclusion

The Bash command line in Ubuntu is a powerful tool that allows users to perform a wide range of tasks, from file management to system monitoring. By learning and using the basic Bash commands mentioned in this article, you can navigate the file system, manage files, install software, and configure your system more efficiently. As you grow more comfortable with Bash, you can also create scripts to automate repetitive tasks and further enhance your productivity.

Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills