Linux Commands
Linux, renowned for its stability, flexibility, and open-source nature, is a powerful operating system that is widely used in servers, development environments, and desktops. While it can seem intimidating at first due to its command-line interface (CLI), mastering essential Linux commands is an empowering step for anyone wanting to become proficient in the system. Knowing the right commands can help you efficiently navigate the Linux environment, manage files, and perform administrative tasks.
This article will introduce you to the fundamental Linux commands that every beginner should know. Whether you’re just starting out with Linux commands or looking to solidify your command-line skills, these commands will give you a strong foundation to build upon.
Understanding the Linux File System
1. pwd (Print Working Directory)
The pwd command is one of the most basic commands in Linux. It tells you the full path of the current directory you are working in. When you open a terminal, it starts in your home directory by default. Using pwd will show you where you are in the directory structure.
$ pwd
/home/user2. ls (List Directory Contents)
The ls command lists all the files and directories in the current directory. It’s one of the most frequently used commands, as it helps you view the contents of directories.
$ ls
Documents Downloads Music Pictures VideosCommon options for ls:
ls -l: Shows detailed information like file permissions, owner, size, and modification date.ls -a: Displays all files, including hidden files (those starting with a dot.).
$ ls -la
total 28
drwxr-xr-x 6 user user 4096 Oct 15 10:32 .
drwxr-xr-x 18 user user 4096 Oct 15 09:22 ..
-rw-r--r-- 1 user user 45 Oct 15 10:10 .bashrc3. cd (Change Directory)
The cd command allows you to move between directories. It’s an essential Linux command for navigating the Linux file system.
- To move to another directory:
$ cd /home/user/Documents- To go back to the previous directory:
$ cd ..- To return to your home directory:
$ cd ~4. touch (Create a New File)
The touch command is used to create an empty file. This is particularly useful when you need to quickly generate a new file for testing or configuration.
$ touch newfile.txt5. mkdir (Make Directory)
The mkdir command is used to create a new directory. It’s a straightforward way to organize your files into separate folders.
$ mkdir newfolderTo create a directory along with its parent directories (if they don’t exist), you can use the -p option:
$ mkdir -p /path/to/newfolder6. rmdir and rm (Remove Directory or Files)
The rmdir command removes empty directories, while the rm command deletes files or directories.
- To remove an empty directory:
$ rmdir emptyfolder- To remove a file:
$ rm file.txt- To remove a directory and its contents recursively:
$ rm -r directoryname7. cp (Copy Files and Directories)
The cp command is used to copy files or directories. It creates a duplicate of the source file or directory at the specified destination.
$ cp file.txt /path/to/destination/To copy a directory and its contents recursively, use the -r option:
$ cp -r sourcedir /path/to/destination/8. mv (Move or Rename Files)
The mv command is used to move files or directories from one location to another. It can also be used to rename files or directories.
- To move a file:
$ mv file.txt /path/to/destination/- To rename a file:
$ mv oldname.txt newname.txt9. cat (Concatenate and Display File Contents)
The cat command is used to display the contents of a file in the terminal. It can also be used to concatenate multiple files and output their content.
$ cat file.txtIf you want to combine two files and save the result in a new file:
$ cat file1.txt file2.txt > combined.txt10. nano and vim (Text Editors)
Linux comes with several text editors that allow you to edit files directly from the terminal. The two most popular ones are nano and vim.
- nano: A beginner-friendly text editor that is easy to use. To open a file with nano:
$ nano file.txt- vim: A powerful text editor that requires more experience but offers advanced features. To open a file with vim:
$ vim file.txt11. man (Manual Pages)
The man command displays the manual pages for a particular command, providing detailed information about how to use the command, including its options and syntax.
$ man lsYou can exit the manual page by pressing q.
12. chmod (Change File Permissions)
Linux is a multi-user system, and file permissions are crucial for controlling access to files. The chmod command is used to change the permissions of a file or directory.
Each file has three types of permissions: read (r), write (w), and execute (x). These permissions are assigned to the owner, group, and others.
- To give the owner read, write, and execute permissions:
$ chmod u+rwx file.txt- To remove write permission for others:
$ chmod o-w file.txtYou can also use numeric values to set permissions:
r = 4w = 2x = 1
For example, to set read and write permissions for the owner, and read-only permissions for the group and others:
$ chmod 644 file.txt13. chown (Change Ownership)
The chown command changes the ownership of a file or directory. Ownership consists of a user and a group.
- To change the owner of a file:
$ chown user file.txt- To change the owner and group of a file:
$ chown user:group file.txt14. ps (Process Status)
The ps command displays information about the currently running processes. It is useful for monitoring system activity.
$ psCommon options for ps:
ps aux: Shows a detailed list of all processes running on the system.ps -ef: Displays full-format listing of all processes.
15. kill (Terminate a Process)
The kill command sends a signal to terminate a process by its process ID (PID). This is useful when a program becomes unresponsive or hangs.
- To find the PID of a process:
$ ps aux | grep processname- To kill the process:
$ kill PIDTo force kill a process, use the -9 option:
$ kill -9 PID16. df (Disk Free)
The df command shows the available disk space on your system’s file systems. It’s particularly useful for checking how much space is available on different drives or partitions.
$ df -hThe -h option shows the output in human-readable format, making it easier to understand (e.g., in MB or GB).
17. du (Disk Usage)
The du command displays the disk usage of files and directories. It helps you determine how much space is being used by specific directories or files.
- To see the disk usage of a specific directory:
$ du /path/to/directory- To see the total disk usage of a directory in human-readable format:
$ du -sh /path/to/directory18. top (Real-Time Process Monitoring)
The top command displays a real-time view of running processes, showing information like CPU usage, memory usage, and process ID. It is a great way to monitor system performance and resource usage.
$ topTo exit top, press q.
19. grep (Search Inside Files)
The grep command is a powerful text search tool that searches for patterns within files or outputs. It is particularly useful for filtering data or searching logs.
- To search for a word inside a file:
$ grep "keyword" file.txt- To search recursively through all files in a directory:
$ grep -r "keyword" /path/to/directory20. tar (Archive Files)
The tar command is used to archive multiple files into a single file, often referred to as a tarball. It is commonly used for backup and transfer purposes.
- To create a tar archive:
$ tar -cv
f archive.tar file1.txt file2.txt- To extract a tar archive:
$ tar -xvf archive.tarIf the archive is compressed with gzip (creating a .tar.gz file), use:
$ tar -xzvf archive.tar.gz21. sudo (Superuser Do)
The sudo command allows a permitted user to execute a command as the superuser (root) or another user. It’s essential for administrative tasks like installing software or modifying system files.
$ sudo apt update22. apt (Package Manager)
For Debian-based distributions like Ubuntu, the apt command is used to manage software packages. It allows you to install, update, and remove software.
- To update the package list:
$ sudo apt update- To install a package:
$ sudo apt install packagename- To remove a package:
$ sudo apt remove packagename23. wget (Download Files from the Web)
The wget command is used to download files from the internet via the terminal. It’s a simple and powerful tool for retrieving files using HTTP, HTTPS, and FTP.
$ wget http://example.com/file.tar.gz24. ssh (Secure Shell)
The ssh command allows you to securely log into a remote machine over the network. It’s widely used for managing remote servers.
$ ssh user@remotehostConclusion
Mastering these essential Linux commands will give you a solid foundation for working with the Linux command line. While this list covers the basics, the Linux commands terminal is vast and offers an incredible range of tools and utilities for users and administrators. With practice, these commands will become second nature, enabling you to navigate the Linux commands with confidence and efficiency. As you continue learning, you’ll discover more advanced Linux commands and techniques that will further enhance your Linux proficiency.