The Linux terminal is a powerful tool that allows users to execute commands directly on the operating system. In this article, we will cover essential terminal commands for managing files, directories, processes, and system settings in Ubuntu or other Linux distributions.
Navigating the File System:
cd
: Change the current working directory. For example,cd /path/to/new/directory
.pwd
: Prints the path of the current working directory. For example,pwd
will output something like/home/user/Documents
.ls
: Lists the files and directories in the current working directory. For example,ls -l
will display a detailed list of files and directories.mkdir
: Creates a new directory. For example,mkdir new_directory
.rmdir
: Removes an empty directory. For example,rmdir old_directory
.rm
: Removes a file. For example,rm file.txt
. Use with caution as it permanently deletes files.mv
: Moves or renames a file. For example,mv old_file.txt new_file.txt
ormv old_directory new_directory2
.cp
: Copies a file or directory. For example,cp source_file destination
orcp -r source_directory destination
.
Managing Directories:
touch
: Thetouch
command is used to create a new empty file or update the modification time of an existing file. For example, to create a new file named “newfile.txt”, use the commandtouch newfile.txt
. If the file already exists, it will be updated with the current date and time.nano/vi/vim
: These are text editors used for editing files in Unix-based systems.nano
is a beginner-friendly editor, whilevi
andvim
are more advanced text editors. For example, to edit a file named “file.txt” using nano, use the commandnano file.txt
.chmod
: Thechmod
command is used to change the permissions of files and directories. It sets the read, write, and execute permissions for the owner, group, and others. For example, to give read, write, and execute permissions to the owner of a file named “file.txt”, use the commandchmod u+rwx file.txt
.chown
: Thechown
command is used to change the ownership of files and directories. It allows you to change the user or group that owns a particular file or directory. For example, to change the owner of a file named “file.txt” to the user “newuser”, use the commandchown newuser file.txt
.
Process Management:
ps
: List running processes – Theps
command is used to display information about currently running processes, including their process ID (PID), memory usage, and other details. For example, you can use the commandps aux | grep <process_name>
to list all processes with a specific name.top
: Display system resources usage and running processes – Thetop
command shows real-time information about system resources such as CPU usage, memory usage, and running processes. You can use the arrow keys to navigate through the list of processes and view detailed information about each one. For example, you can press ‘q’ to quit thetop
command.kill
: Terminate a process – Thekill
command is used to send signals to a running process, which can be used to terminate it or send other signals such as SIGINT (interrupt) or SIGTERM (terminate). For example, you can use the commandkill <PID>
to terminate a process with the given PID. If the process doesn’t terminate, you may need to usekill -9 <PID>
to forcefully kill it.pgrep
: Search for a process by name – Thepgrep
command is used to search for processes based on their names or other attributes such as PIDs. For example, you can use the commandpgrep <process_name>
to find the PID of a running process with that name. If you want to kill the process, you can pipe the output ofpgrep
tokill
, like this:pgrep <process_name> | xargs kill
.
System Settings:
sudo
: Thesudo
(Short for “Superuser DO”) command allows users to execute commands with administrative privileges. For example, if you want to install software using the package managerapt-get
, but require root access, you can usesudo apt-get install <package-name>
.apt-get
: The Advanced Package Tool (APT) is a popular package manager for Debian and Ubuntu based systems. It’s used to install, update, and remove software packages. For example, you can install thehtop
terminal application usingsudo apt-get install htop
.ifconfig
: Theifconfig
command is used to display network interface configurations, including IP addresses, netmasks, and other details. For example, to check the IP address of your eth0 interface, you can useifconfig eth0
.iptables
: IPTables is a firewall configuration tool for Linux systems. It’s used to manage network traffic by defining rules that allow or block specific types of connections based on various criteria such as source and destination IP addresses, ports, and protocols. For example, you can create a rule to allow incoming SSH connections by runningsudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
.