Linux Ubuntu Basic Commands: Master the Essentials

Share On

Welcome to our comprehensive guide on Linux Ubuntu basic commands. Whether you are a beginner or an experienced user, understanding and mastering these essential commands is crucial for navigating and managing your Ubuntu system effectively. In this article, we will cover a wide range of commands that will empower you to perform various tasks, from navigating directories to managing files, configuring network interfaces, and much more. By the end of this guide, you will have a solid foundation in using the command line interface in Ubuntu and be able to perform common tasks with ease.

Introduction

In this section, we will provide a brief introduction to Linux Ubuntu and the command line interface. We will explain the benefits of using the command line, its role in Ubuntu, and why it is important to learn and master basic commands.

1. ls – List directory contents

The ls command is used to list the contents of a directory. It displays the names of files and subdirectories within the specified directory. By default, it lists the contents of the current directory. However, you can specify a different directory as an argument to the command.

To list the contents of the current directory, simply type ls and press Enter. The command will display the names of all files and directories in the current directory.

If you want to list the contents of a specific directory, you can provide the directory path as an argument to the ls command. For example, to list the contents of the /home directory, you would type ls /home and press Enter.

2. cd – Change directory

The cd command is used to change the current working directory. It allows you to navigate through the directory structure of your Ubuntu system.

To change to a specific directory, you can use the cd command followed by the directory path. For example, to change to the /home directory, you would type cd /home and press Enter.

If you want to change to a directory that is located within the current directory, you can simply type cd followed by the directory name. For example, if you are currently in the /home directory and want to change to the /home/user directory, you would type cd user and press Enter.

3. pwd – Print working directory

The pwd command is used to print the current working directory. It displays the full path of the directory you are currently in.

To use the pwd command, simply type pwd and press Enter. The command will display the full path of the current directory.

This command is particularly useful when you are navigating through the directory structure and want to know your current location.

4. mkdir – Create a new directory

The mkdir command is used to create a new directory. It allows you to create directories within the current directory or specify a different directory path.

To create a new directory, use the mkdir command followed by the name of the directory you want to create. For example, to create a directory named documents within the current directory, you would type mkdir documents and press Enter.

If you want to create a directory in a specific location, you can provide the directory path as an argument to the mkdir command. For example, to create a directory named documents in the /home directory, you would type mkdir /home/documents and press Enter.

5. rmdir – Remove a directory

The rmdir command is used to remove an empty directory. It allows you to delete directories within the current directory or specify a different directory path.

To remove a directory, use the rmdir command followed by the name of the directory you want to remove. For example, to remove a directory named documents within the current directory, you would type rmdir documents and press Enter.

If you want to remove a directory in a specific location, you can provide the directory path as an argument to the rmdir command. For example, to remove a directory named documents in the /home directory, you would type rmdir /home/documents and press Enter.

6. touch – Create a new file

The touch command is used to create a new file. It allows you to create empty files within the current directory or specify a different directory path.

To create a new file, use the touch command followed by the name of the file you want to create. For example, to create a file named example.txt within the current directory, you would type touch example.txt and press Enter.

If you want to create a file in a specific location, you can provide the file path as an argument to the touch command. For example, to create a file named example.txt in the /home directory, you would type touch /home/example.txt and press Enter.

7. rm – Remove files or directories

The rm command is used to remove files or directories. It allows you to delete one or more files or directories within the current directory or specify a different directory path.

To remove a file, use the rm command followed by the name of the file you want to remove. For example, to remove a file named example.txt within the current directory, you would type rm example.txt and press Enter.

If you want to remove multiple files, you can provide the file names as arguments to the rm command. For example, to remove two files named file1.txt and file2.txt within the current directory, you would type rm file1.txt file2.txt and press Enter.

To remove a directory, you can use the rm command with the -r option, which stands for recursive. This option allows you to remove a directory and its contents. For example, to remove a directory named documents within the current directory, you would type rm -r documents and press Enter.

8. cp – Copy files and directories

The cp command is used to copy files and directories. It allows you to create copies of one or more files or directories within the current directory or specify a different directory path.

To copy a file, use the cp command followed by the name of the file you want to copy and the destination path. For example, to copy a file named example.txt to the /home directory, you would type cp example.txt /home and press Enter.

If you want to copy multiple files, you can provide the file names as arguments to the cp command, followed by the destination path. For example, to copy two files named file1.txt and file2.txt to the /home directory, you would type cp file1.txt file2.txt /home and press Enter.

To copy a directory, you can use the cp command with the -r option, which stands for recursive. This option allows you to copy a directory and its contents. For example, to copy a directory named documents to the /home directory, you would type cp -r documents /home and press Enter.

9. mv – Move or rename files and directories

The mv command is used to move or rename files and directories. It allows you to change the location or name of one or more files or directories within the current directory or specify a different directory path.

To move a file or directory, use the mv command followed by the name of the file or directory you want to move and the destination path. For example, to move a file named example.txt to the /home directory, you would type mv example.txt /home and press Enter.

If you want to move multiple files or directories, you can provide the names as arguments to the mv command, followed by the destination path. For example, to move two files named file1.txt and file2.txt to the /home directory, you would type mv file1.txt file2.txt /home and press Enter.

To rename a file or directory, you can use the mv command and specify the new name as the destination path. For example, to rename a file named example.txt to newname.txt, you would type mv example.txt newname.txt and press Enter.

10. cat – Concatenate and display file content

The cat command is used to concatenate and display the content of one or more files. It allows you to view the contents of text files directly in the terminal.

To display the content of a file, use the cat command followed by the name of the file. For example, to display the content of a file named example.txt, you would type cat example.txt and press Enter.

If you want to display the content of multiple files, you can provide the file names as arguments to the cat command. For example, to display the content of two files named file1.txt and file2.txt, you would type cat file1.txt file2.txt and press Enter.

The cat command can also be used to concatenate files. To concatenate multiple files into a single file, you can use the cat command followed by the names of the files you want to concatenate, and redirect the output to a new file. For example, to concatenate two files named file1.txt and file2.txt into a new file named combined.txt, you would type cat file1.txt file2.txt > combined.txt and press Enter.

11. less – View file content page by page

The less command is used to view the content of a file page by page. It allows you to navigate through the content of large files without overwhelming the terminal.

To view the content of a file using less, simply type less followed by the name of the file. For example, to view the content of a file named example.txt, you would type less example.txt and press Enter.

Once the file is open in less, you can use the arrow keys to scroll up and down through the content. Press the Space key to scroll down one page at a time, and press the B key to scroll up one page at a time. To exit less, press the Q key.

12. head – Display the beginning of a file

The head command is used to display the beginning of a file. It allows you to view the first few lines of a file without displaying the entire content.

To display the beginning of a file using head, simply type head followed by the name of the file. By default, head displays the first 10 lines of the file. For example, to display the first 10 lines of a file named example.txt, you would type head example.txt and press Enter.

If you want to display a different number of lines, you can use the -n option followed by the number of lines you want to display. For example, to display the first 5 lines of a file, you would type head -n 5 example.txt and press Enter.

13. tail – Display the end of a file

The tail command is used to display the end of a file. It allows you to view the last few lines of a file without displaying the entire content.

To display the end of a file using tail, simply type tail followed by the name of the file. By default, tail displays the last 10 lines of the file. For example, to display the last 10 lines of a file named example.txt, you would type tail example.txt and press Enter.

If you want to display a different number of lines, you can use the -n option followed by the number of lines you want to display. For example, to display the last 5 lines of a file, you would type tail -n 5 example.txt and press Enter.

14. grep – Search for a specific pattern in files

The grep command is used to search for a specific pattern in one or more files. It allows you to find lines that match a given pattern and display them in the terminal.

To search for a pattern using grep, use the command followed by the pattern you want to search for and the name of the file. For example, to search for the word “linux” in a file named example.txt, you would type grep linux example.txt and press Enter.

If you want to search for a pattern in multiple files, you can provide the file names as arguments to the grep command. For example, to search for the word “linux” in two files named file1.txt and file2.txt, you would type grep linux file1.txt file2.txt and press Enter.

The grep command also supports various options that allow you to customize the search. For example, you can use the -i option to perform a case-insensitive search, or the -r option to search for a pattern recursively in a directory and its subdirectories.

15. find – Search for files and directories

The find command is used to search for files and directories based on various criteria, such as name, size, or modification time. It allows you to locate files and directories within a specified directory or the entire file system.

To search for files and directories using find, use the command followed by the directory path and the search criteria. For example, to search for all files with the extension “.txt” in the current directory and its subdirectories, you would type find . -name “*.txt” and press Enter.

If you want to search for files and directories in a specific location, you can provide the directory path as an argument to the find command. For example, to search for all directories named “documents” in the /home directory, you would type find /home -type d -name “documents” and press Enter.

The find command supports a wide range of options that allow you to refine your search. You can specify the file type, size, modification time, and much more.

16. chmod – Change file permissions

The chmod command is used to change the permissions of a file or directory. It allows you to control who can read, write, or execute a file or directory.

To change the permissions of a file or directory using chmod, use the command followed by the desired permissions and the name of the file or directory. For example, to give read, write, and execute permissions to the owner of a file named example.txt, you would type chmod u+rwx example.txt and press Enter.

The permissions are represented by three sets of characters: user, group, and others. Each set consists of three characters: read (r), write (w), and execute (x). You can use the following symbols to specify the permissions:

  • r – read permission
  • w – write permission
  • x – execute permission
  • – no permission

For example, chmod u+rwx,g+rx,o+r example.txt gives read, write, and execute permissions to the owner, read and execute permissions to the group, and read permission to others.

17. chown – Change file ownership

The chown command is used to change the ownership of a file or directory. It allows you to transfer the ownership from one user to another.

To change the ownership of a file or directory using chown, use the command followed by the new owner and the name of the file or directory. For example, to change the ownership of a file named example.txt to a user named user, you would type chown user example.txt and press Enter.

You can also specify the group ownership by using the : symbol. For example, chown user:group example.txt changes the ownership to a user and a group.

18. sudo – Execute a command with superuser privileges

The sudo command is used to execute a command with superuser privileges. It allows you to perform administrative tasks that require root access.

To execute a command with sudo, simply type sudo followed by the command you want to run. For example, to install a software package using the apt-get command, you would type sudo apt-get install package and press Enter.

When you run a command with sudo, you will be prompted to enter your password. After entering the password, the command will be executed with superuser privileges.

19. apt-get – Package management command for installing, updating, and removing software packages

The apt-get command is a package management command used for installing, updating, and removing software packages in Ubuntu. It allows you to easily manage software on your system.

To install a software package using apt-get, use the command followed by the install keyword and the name of the package. For example, to install the firefox web browser, you would type sudo apt-get install firefox and press Enter.

To update all installed packages, use the upgrade keyword. For example, to update all installed packages on your system, you would type sudo apt-get upgrade and press Enter.

To remove a software package, use the remove keyword followed by the name of the package. For example, to remove the firefox web browser, you would type sudo apt-get remove firefox and press Enter.

20. dpkg – Package management command for installing, updating, and removing software packages

The dpkg command is another package management command used for installing, updating, and removing software packages in Ubuntu. It provides a lower-level interface compared to apt-get and allows you to directly interact with the package management system.

To install a software package using dpkg, use the command followed by the -i option and the name of the package file. For example, to install a package named package.deb, you would type sudo dpkg -i package.deb and press Enter.

To update a software package, use the -i option followed by the name of the updated package file. For example, to update a package named package.deb, you would type sudo dpkg -i package.deb and press Enter.

To remove a software package, use the -r option followed by the name of the package. For example, to remove a package named package, you would type sudo dpkg -r package and press Enter.

21. tar – Create or extract compressed archive files

The tar command is used to create or extract compressed archive files. It allows you to combine multiple files and directories into a single file, which can be compressed for efficient storage or transmission.

To create a compressed archive file using tar, use the command followed by the -czvf options, the name of the archive file, and the files or directories you want to include. For example, to create a compressed archive file named archive.tar.gz containing the files file1.txt and file2.txt, you would type tar -czvf archive.tar.gz file1.txt file2.txt and press Enter.

To extract the contents of a compressed archive file, use the command followed by the -xzvf options and the name of the archive file. For example, to extract the contents of a compressed archive file named archive.tar.gz, you would type tar -xzvf archive.tar.gz and press Enter.

22. gzip – Compress files

The gzip command is used to compress files. It allows you to reduce the size of files for efficient storage or transmission.

To compress a file using gzip, use the command followed by the name of the file. For example, to compress a file named example.txt, you would type gzip example.txt and press Enter.

After compression, the original file will be replaced with a compressed file with the extension .gz. For example, the file example.txt will be replaced with example.txt.gz.

To decompress a compressed file, use the gunzip command followed by the name of the compressed file. For example, to decompress a file named example.txt.gz, you would type gunzip example.txt.gz and press Enter.

23. gunzip – Decompress files

The gunzip command is used to decompress files that have been compressed using gzip. It allows you to restore the original file from a compressed file.

To decompress a file using gunzip, use the command followed by the name of the compressed file. For example, to decompress a file named example.txt.gz, you would type gunzip example.txt.gz and press Enter.

After decompression, the original file will be restored with its original name and extension. For example, the file example.txt.gz will be restored as example.txt.

24. ssh – Securely connect to a remote server

The ssh command is used to securely connect to a remote server. It allows you to establish a secure encrypted connection and access the command line interface of the remote server.

To connect to a remote server using ssh, use the command followed by the username and the IP address or hostname of the remote server. For example, to connect to a remote server with the username user and the IP address 192.168.0.1, you would type ssh user@192.168.0.1 and press Enter.

After entering the command, you will be prompted to enter the password for the remote server. Once authenticated, you will have access to the command line interface of the remote server.

25. scp – Securely copy files between local and remote systems

The scp command is used to securely copy files between a local system and a remote system. It allows you to transfer files over a secure encrypted connection.

To copy a file from a local system to a remote system using scp, use the command followed by the name of the file, the username, and the IP address or hostname of the remote system, and the destination path. For example, to copy a file named example.txt from the local system to a remote system with the username user and the IP address 192.168.0.1, you would type scp example.txt user@192.168.0.1:/path/to/destination and press Enter.

To copy a file from a remote system to a local system, simply reverse the source and destination paths. For example, to copy a file named example.txt from a remote system to the local system, you would type scp user@192.168.0.1:/path/to/file/example.txt /path/to/destination and press Enter.

26. wget – Download files from the internet

The wget command is used to download files from the internet. It allows you to retrieve files from web servers using various protocols, such as HTTP, HTTPS, and FTP.

To download a file using wget, use the command followed by the URL of the file. For example, to download a file named example.txt from a web server, you would type wget http://example.com/example.txt and press Enter.

By default, wget will save the downloaded file in the current directory. If you want to specify a different location, you can provide the directory path as an argument to the -P option. For example, to save the downloaded file in the /home directory, you would type wget -P /home http://example.com/example.txt and press Enter.

27. ping – Send ICMP echo requests to a network host

The ping command is used to send ICMP echo requests to a network host. It allows you to check the connectivity and response time of a remote host.

To send a ping request using ping, use the command followed by the IP address or hostname of the remote host. For example, to send a ping request to a host with the IP address 192.168.0.1, you would type ping 192.168.0.1 and press Enter.

The ping command will send a series of ICMP echo requests to the remote host and display the response time for each request. This allows you to determine if the remote host is reachable and measure the round-trip time.

28. ifconfig – Display or configure network interfaces

The ifconfig command is used to display or configure network interfaces. It allows you to view the IP address, netmask, and other network configuration information of your Ubuntu system.

To display the network configuration using ifconfig, simply type ifconfig and press Enter. The command will display the network interfaces and their corresponding IP addresses, netmasks, and other information.

If you want to configure a network interface, you can use the ifconfig command followed by the interface name and the desired configuration options. For example, to set the IP address of an interface named eth0 to 192.168.0.1, you would type sudo ifconfig eth0 192.168.0.1 and press Enter.

29. netstat – Display network connections and statistics

The netstat command is used to display network connections and statistics. It allows you to view the active network connections, listening ports, and network interface statistics of your Ubuntu system.

To display the network connections using netstat, simply type netstat and press Enter. The command will display a list of active network connections, including the local and remote IP addresses, port numbers, and connection states.

If you want to display listening ports, you can use the -l option. For example, to display listening ports, you would type netstat -l and press Enter.

The netstat command also supports various options that allow you to filter and customize the output. You can specify the protocol, address families, and much more.

30. top – Display system resource usage and running processes

The top command is used to display real-time system resource usage and running processes. It allows you to monitor the CPU, memory, and other system resources of your Ubuntu system.

To display the system resource usage using top, simply type top and press Enter. The command will display a dynamic view of the system resource usage, including the CPU usage, memory usage, and other statistics.

The top command also displays a list of running processes, sorted by their resource usage. You can use the arrow keys to navigate through the list and press the Q key to exit top.

31. ps – Display running processes

The ps command is used to display information about running processes. It allows you to view the process ID, CPU usage, memory usage, and other details of the processes running on your Ubuntu system.

To display the running processes using ps, simply type ps and press Enter. The command will display a list of running processes, including their process IDs, CPU usage, memory usage, and other information.

The ps command supports various options that allow you to customize the output. You can specify the format, sort the processes by different criteria, and much more.

32. kill – Terminate a process

The kill command is used to terminate a running process. It allows you to send a signal to a process, instructing it to exit gracefully or forcefully.

To terminate a process using kill, use the command followed by the process ID of the process you want to terminate. For example, to terminate a process with the process ID 1234, you would type kill 1234 and press Enter.

By default, the kill command sends the SIGTERM signal to the process, which instructs it to exit gracefully. If the process does not respond to the SIGTERM signal, you can use the -9 option to send the SIGKILL signal, which forcefully terminates the process. For example, to forcefully terminate a process with the process ID 1234, you would type kill -9 1234 and press Enter.

33. history – Display command history

The history command is used to display the command history. It allows you to view the list of commands that you have executed in the current session.

To display the command history using history, simply type history and press Enter. The command will display a numbered list of commands, starting from the most recent command.

You can use the arrow keys to navigate through the command history and press Enter to execute a command from the history.

34. clear – Clear the terminal screen

The clear command is used to clear the terminal screen. It allows you to remove all previous output from the terminal and start with a clean screen.

To clear the terminal screen using clear, simply type clear and press Enter. The command will remove all previous output and display a clean screen.

This command is particularly useful when the terminal screen becomes cluttered with previous output and you want to start with a clean slate.

35. man – Display the manual page for a command

The man command is used to display the manual page for a command. It allows you to access the documentation and usage instructions for various commands in Ubuntu.

To display the manual page for a command using man, use the command followed by the name of the command. For example, to display the manual page for the ls command, you would type man ls and press Enter.

The manual page provides detailed information about the command, including its usage, options, and examples. You can use the arrow keys to navigate through the manual page and press the Q key to exit man.

36. alias – Create a shortcut for a command

The alias command is used to create a shortcut for a command. It allows you to define custom command aliases that can be used to execute complex or frequently used commands with a single keyword.

To create an alias using alias, use the command followed by the desired alias name, the equals sign (=), and the command you want to alias. For example, to create an alias named ll for the ls -l command, you would type alias ll=’ls -l’ and press Enter.

After creating an alias, you can use the alias name as a shortcut to execute the aliased command. For example, to execute the ls -l command using the ll alias, you would simply type ll and press Enter.

Aliases are particularly useful for simplifying complex or frequently used commands, improving productivity, and reducing typing.

37. echo – Display text or variables

The echo command is used to display text or the values of variables. It allows you to output messages or information to the terminal.

To display text using echo, use the command followed by the text you want to display. For example, to display the message “Hello, world!”, you would type echo “Hello, world!” and press Enter.

The echo command can also display the values of variables. To display the value of a variable, use the command followed by the dollar sign ($) and the variable name. For example, if you have a variable named name with the value “John”, you would type echo $name and press Enter.

The echo command supports various options that allow you to customize the output. You can use the -e option to enable interpretation of backslash escapes, or the -n option to suppress the trailing newline character.

38. date – Display or set the system date and time

The date command is used to display or set the system date and time. It allows you to view the current date and time, as well as modify the system date and time.

To display the current date and time using date, simply type date and press Enter. The command will display the current date and time in the default format.

If you want to display the date and time in a specific format, you can use the +%format option. For example, to display the date and time in the format “YYYY-MM-DD HH:MM:SS”, you would type date +’%Y-%m-%d %H:%M:%S’ and press Enter.

To set the system date and time, you need superuser privileges. You can use the sudo command followed by date and the desired date and time in the format “MMDDhhmmYYYY.ss”. For example, to set the system date and time to January 1, 2022, 12:00 PM, you would type sudo date 010112002022.00 and press Enter.

39. uname – Display system information

The uname command is used to display system information. It allows you to view various details about your Ubuntu system, such as the kernel version, machine architecture, and operating system.

To display system information using uname, simply type uname and press Enter. The command will display the kernel name, network node hostname, kernel release, kernel version, machine architecture, and operating system.

The uname command supports various options that allow you to display specific information. For example, you can use the -r option to display only the kernel release, or the -m option to display only the machine architecture.

40. whoami – Display the current user’s username

The whoami command is used to display the username of the current user. It allows you to quickly identify the user account that is currently logged in.

To display the current user’s username using whoami, simply type whoami and press Enter. The command will display the username of the current user.

This command is particularly useful when you are working with multiple user accounts or need to verify the current user’s identity.

Now that you have learned the basics of Linux Ubuntu commands, you are well-equipped to navigate and manage your Ubuntu system with ease. Whether you are a beginner or an experienced user, mastering these essential commands will greatly enhance your productivity and efficiency. Remember to practice and explore further to become even more proficient in using the command line interface.

FAQs

1. Can I use these commands in other Linux distributions?

Yes, most of these commands are common across different Linux distributions. However, there might be slight differences in syntax or options. It’s always a good idea to consult the documentation or manual pages specific to your distribution.

2. Are there any risks associated with using the command line?

Using the command line can be powerful, but it also requires caution. Executing commands with superuser privileges (using sudo) can have serious consequences if used incorrectly. It’s important to double-check the commands and their arguments before executing them, especially when dealing with system files or critical operations.

3. How can I learn more about these commands?

The best way to learn more about these commands is to practice using them and explore their options and variations. You can refer to the manual pages (man command) for detailed information and examples. Additionally, there are numerous online resources, tutorials, and forums dedicated to Linux and Ubuntu that can provide further guidance and insights.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *