Linux Commands You Should Know as a Beginner Allthinglinux.com, October 19, 2024October 19, 2024 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/user 2. 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 Videos Common 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 .bashrc 3. 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.txt 5. 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 newfolder To create a directory along with its parent directories (if they don’t exist), you can use the -p option: $ mkdir -p /path/to/newfolder 6. 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 directoryname 7. 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.txt 9. 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.txt If you want to combine two files and save the result in a new file: $ cat file1.txt file2.txt > combined.txt 10. 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.txt 11. 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 ls You 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.txt You can also use numeric values to set permissions: r = 4 w = 2 x = 1 For example, to set read and write permissions for the owner, and read-only permissions for the group and others: $ chmod 644 file.txt 13. 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.txt 14. ps (Process Status) The ps command displays information about the currently running processes. It is useful for monitoring system activity. $ ps Common 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 PID To force kill a process, use the -9 option: $ kill -9 PID 16. 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 -h The -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/directory 18. 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. $ top To 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/directory 20. 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.tar If the archive is compressed with gzip (creating a .tar.gz file), use: $ tar -xzvf archive.tar.gz 21. 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 update 22. 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 packagename 23. 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.gz 24. 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@remotehost Conclusion 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. Linux Basics Linux Commands
Linux Basics Mastering Linux File Permissions October 23, 2024October 23, 2024 File permissions in Linux are a crucial part of system security and functionality. Understanding and… Read More
Linux Basics Basic Linux Networking Tools and Commands October 22, 2024October 22, 2024 Networking is one of the most critical aspects of modern computing. It facilitates communication between… Read More
Linux Basics Introduction to Linux System Logs November 8, 2024November 8, 2024 System logging is a critical component of Linux system administration, providing valuable insights into system… Read More