Linux is a powerful and versatile operating system, favored by developers, system administrators, and tech enthusiasts worldwide. One of the key reasons for its popularity is the command line interface (CLI), which allows users to interact with the system at a deeper level than a graphical user interface (GUI). Whether you’re a beginner or looking to sharpen your skills, here are the top 10+ Linux commands you must know.
1. `ls` – List Directory Contents
The `ls` command is one of the most basic and frequently used commands in Linux. It lists the contents of a directory, showing you the files and subdirectories within it.
“`bash
ls
“`
You can add options like `-l` for a detailed list or `-a` to include hidden files.
“`bash
ls -la
“`
2. `cd` – Change Directory
Navigating through directories is essential when working in the Linux terminal. The `cd` (change directory) command allows you to move between directories.
“`bash
cd /path/to/directory
“`
To move up one level in the directory hierarchy, use:
“`bash
cd ..
“`
3. `pwd` – Print Working Directory
The `pwd` command displays the full path of the current working directory. This is useful when you need to know exactly where you are within the file system.
“`bash
pwd
“`
4. `cp` – Copy Files and Directories
The `cp` command is used to copy files and directories from one location to another.
“`bash
cp source_file destination_directory
“`
To copy directories, use the `-r` (recursive) option:
“`bash
cp -r source_directory destination_directory
“`
5. `mv` – Move or Rename Files and Directories
The `mv` command is versatile, allowing you to move files or directories to a new location, or rename them.
“`bash
mv old_name new_name
“`
“`bash
mv file.txt /new/location/
“`
6. `rm` – Remove Files and Directories
To delete files and directories, use the `rm` command. Be cautious with this command, especially when using options like `-r` for recursive deletion.
“`bash
rm file.txt
“`
To delete a directory and its contents, use:
“`bash
rm -r directory_name
“`
7. `touch` – Create a New Empty File
The `touch` command is used to create a new, empty file, or to update the timestamp of an existing file.
“`bash
touch newfile.txt
“`
8. `mkdir` – Create a New Directory
To create a new directory, use the `mkdir` command.
“`bash
mkdir new_directory
“`
To create a nested directory structure, use the `-p` option:
“`bash
mkdir -p parent_directory/child_directory
“`
9. `grep` – Search Text Within Files
The `grep` command searches for patterns within files. It’s extremely powerful for finding specific text or data within a large number of files.
“`bash
grep ‘search_term’ filename.txt
“`
For a recursive search through directories:
“`bash
grep -r ‘search_term’ /path/to/directory
“`
10. `cat` – Concatenate and Display Files
The `cat` command is used to display the contents of a file. It can also be used to concatenate multiple files and print their combined output.
“`bash
cat filename.txt
“`
To combine and display two files:
“`bash
cat file1.txt file2.txt
“`
11. `echo` – Display a Line of Text
The `echo` command is used to display a line of text or a variable’s value in the terminal. It’s often used in scripts to print output.
“`bash
echo “Hello, World!”
“`
You can also use it to write text into a file:
“`bash
echo “This is a test” > testfile.txt
“`
12. `find` – Search for Files and Directories
The `find` command is extremely powerful for locating files and directories based on various criteria, such as name, type, or modified date.
“`bash
find /path/to/search -name “filename.txt”
“`
To search for files modified in the last 7 days:
“`bash
find /path/to/search -mtime -7
“`
13. `chmod` – Change File Permissions
File permissions in Linux determine who can read, write, or execute a file. The `chmod` command is used to modify these permissions.
“`bash
chmod 755 filename.sh
“`
This example gives the owner full permissions and read-execute permissions to others.
14. `chown` – Change File Ownership
To change the ownership of files or directories, use the `chown` command.
“`bash
chown new_owner:new_group filename.txt
“`
15. `sudo` – Execute Commands with Superuser Privileges
The `sudo` command allows you to execute a command with superuser (root) privileges. It’s essential for performing tasks that require administrative access.
“`bash
sudo apt-get update
“`
16. `apt-get` – Package Manager for Debian-based Systems
If you’re using a Debian-based distribution like Ubuntu, the `apt-get` command is used to manage packages (install, update, remove software).
“`bash
sudo apt-get install package_name
“`
Mastering these Linux commands will significantly improve your efficiency and control over the system. They form the foundation of daily operations on Linux and will empower you to tackle more advanced tasks. Whether you’re a beginner or an experienced user, having a good grasp of these commands will make your journey with Linux much more rewarding.