Essential Linux Commands: Your Guide to Command-Line Basics

Essential Linux Commands: Your Guide to Command-Line Basics

Photo by Lukas on Unsplash

Welcome to the world of Linux, where the terminal holds the keys to a universe of possibilities. If you're new to Linux and feeling a bit puzzled about this "terminal" thing, don't worry! This blog is your compass, guiding you to become a command-line champ.

Before we get our hands dirty, let's clear the air about one thing. The system you installed is Linux, but not just Linux. Linux is the brain, the core that powers the OS. You might have installed the "Ubuntu" distribution or any of its kind, but don't worry if you're not sure what that means. Skip ahead and we'll break it down together.

Now, Imagine the kernel as the captain of a ship, it manages hardware, services, and software, bridging your computer's soul and body. Created by Linus Torvalds in '91, the Linux kernel is the heartbeat of the OS. But it can not work alone. It needs sidekicks – software and user interfaces – to put on a full OS show. So, installing Linux, like the cool "Ubuntu," bundles the kernel with a bunch of apps, libraries, and tools. This combination grants you backstage access, allowing you to steer your hardware with skill.

Alright, enough theory! Let's tackle the fun stuff – Linux commands. These are your spells for ruling the terminal and working wonders. These commands work across both Debian-based and Fedora-based Linux distributions, making them valuable skills for any Linux user:

Basic Commands:

  1. ls: List the contents of a directory. When you are in a directory in a Linux terminal and want to see what files and folders are there just use the 'ls' command.

     ls
     output: file1.txt  file2.txt  pictures/ videos/
    
  2. cd: Change your current directory. so you just listed all the folders aka directories in the folder you are currently in. the list includes the pictures folder and you want to get inside this folder. use the 'cd' (change directory) command followed by the directory or folder you want to get inside:

     cd pictures/
    
  3. pwd: Print the current working directory. this pwd command shows you where are you now in respect of you / or the root directory. In the example, we can see we are currently inside the pictures folder of the user Juned.

     pwd
     output: /home/user/juned/pictures
    
  4. mkdir: Create a new directory. to create a new folder or directory in any folder where you have access to it, use the mkdir(make directory) command followed by the name of the directory you want to create.

     mkdir photos
    
  5. rm: Remove files or directories. to delete a file from the current directory you are currently in just use the command rm(stands for remove) followed by the filename that you want to delete.

     rm letter.doc
    
  6. cp: Copy files or directories. To copy a file or folder from one directory to another use the cp command. this command takes two arguments, first one is the file/folder you want to copy, second is the folder where you want to copy the file/folder to. Here in this example, I want to copy the file.txt to the Documents folder.

     cp file.txt Documents/
    
  7. mv: Move or rename files/directories. same as the cp command, but the mv command moves the file without keeping any copy in the previous folder.

     mv file.txt Documents/
    
  8. touch: Create an empty file. this is one of the most handy commands to use in a Linux terminal. If you want to create any type of file in a directory from a simple dot.txt file to a Python program with the name virus.py you can create it with the touch command. just type touch followed by the filename along with the extension.

     touch virus.py
    
  9. nano or Vim: Text editors for creating and editing files. Okay, to be honest, this is an advanced topic for a beginner in a Linux environment. if you are a new user I would suggest just keep using a normal text editor like gedit or anything for at least 1-2 months. but if you still want to use nano/vim and spend hours of your valuable time figuring out how to get out of vim then, use nano/vim command each one for each software followed by the file that you want to edit.

     nano file.txt
    

    I will try to write another blog on how to use nano and vim. make sure to follow this blog.

  10. history: View command history. to see all the commands you written earlier use the history command and it will list all the commands.

    history
    output: 1 ls, 2 cd directory/, 3 pwd, ...
    
  11. cat: Display the contents of a file. cat command reads the data on a file and shows it on the terminal. for example, if you want to have a quick look at the Python program you just wrote without opening it in a text editor, use cat command

  12. cat virus.py
    output: 
    # This is just Hello, world! python program.
    print('Hello, world!')
    
  13. echo: Print text to the terminal.

    echo "Hello, World!"
    output: Hello, World!
    
  14. apt-get / dnf: Package management tools for installing and managing software. to install any software using the terminal first type sudo apt-get update. and then type sudo apt-get install name of the program you want to install. you can get the name of the program from the respective website of that program or with a search on the internet. for example to install the text editor gedit.

    sudo apt-get install gedit
    

that is enough for you to get started with the Linux terminal. but if you want to learn more, keep going.

Advanced Commands:

  1. grep: Search for a specific pattern in files. this command sees the inside of a file for a pattern you want to search. for example, a txt file country.txt containing 10 counties' names with one line about the country. you want to have a quick check if this file contains the text "Bangladesh" in it.

     grep "Bangladesh" country.txt
     output: Bangladesh - The Land of Eternal Beauty
    
  2. chmod: Change file permissions. each Linux file has three types of permission, read-write-execute. if you want to change the permission of a file to be able to execute it, use the chmod command followed by +x (for execute) and then the name of the file.

     chmod +x script.sh
    
  3. chown: Change file ownership.

     chown user:group file.txt
    

    I will write another blog on the topics of Linux permissions, User groups, etc. Follow.

  4. sudo: Execute commands with superuser privileges. similar to Windows administrative privileges Linux has something called a superuser. the superuser can do anything in a system, even deleting files that are required for the system to run. to run any command with superuser privilege, use sudo (superuser do) before the command. remember that any file created or modified with a sudo user can not be edited by normal users.

     sudo nano atom-bomb-formula.txt
    
  5. ps: Display information about running processes. processes are the running instances of programs or commands. the parent process of all processes in Linux is 'init'. To see information about any process we can use the ps (process status) command followed by the name of the process. To see the list of the process we can use the pe -e command.

     ps -e
     ps aux
    
  6. kill: Terminate processes. we can kill/stop a process with the kill command followed by the process id.

     kill process_id
    
  7. df: Display disk space usage of file systems. this command displays the space occupied by files/folders and other stuff on the system.

     df -h
    
  8. du: Display the file and directory space usage. this command displays space occupied by a particular file or folder.

     du -sh directory/
    
  9. top: Display system resource usage and running processes in real-time. It provides a dynamic real-time view of a running system. It can display system summary information as well as a list of processes or threads currently being managed by the Linux kernel

     top
    
  10. free: Display memory usage. shows the amount of memory (RAM) that has been used by the system and how much is free.

    free -h
    
  11. wget: Download files from the web. Just like downloading any file from the internet with one click on a red download button, you can get the file with the wget command also. just type the exact URL to the file and it will be downloaded in the folder you are currently in.

    wget https://example.com/file.txt
    
  12. curl: Transfer data to/from a server. This command in Linux is used to transfer data to or from a server using various protocols, such as HTTP, HTTPS, FTP, and more. It can be used to download files, perform HTTP requests, and even automate tasks like testing APIs or fetching web content.

    curl https://example.com/api/data
    
  13. tar: Compress and extract files. just like making a zip of a folder, we can use the tar command to make a tarball of a folder/directory that combines and compresses multiple files. Tarballs are common file formats on the Linux operating system. the command goes like fire tar then some letters denoting the compression type, then the name for the new compressed file with .tar extension, and finally the folder/directory you want to compress.

    tar -cvf archive.tar files/
    

    There will be another blog about tar too.

  14. ps aux | grep: Combine commands to filter and find specific processes. Okay, so pipelining in Linux is combining two or multiple commands together to do a single task more efficiently. and here we are doing it. here in this example, we are using ps aux to list all running processes and then grep to search for a process with a pattern.

    ps aux | grep process_name
    
  15. ifconfig / ip: Display network interface configuration. One of the reasons that I use Linux is its usefulness in monitoring networks and performing network-related tasks with ease. Here in the example, it will display detailed information about all network interfaces, providing essential data like IP addresses, MAC addresses, network configurations, and more

    ifconfig -a
    
  16. ssh: Connect to remote machines securely. Stands for "Secure Shell" and is used to establish secure remote connections to other machines over a network. It allows users to log into a remote server or computer securely, providing encrypted communication and authentication methods to ensure data privacy and security during remote access. Let's say I want to remotely access a server with the IP address 192.168.1.100 using the username juned. I can use the following ssh command in the terminal:

    ssh juned@192.168.1.100
    
  17. man: Access the manual pages for commands. this command is used to see the manual page of the command. simply put man before any command to see the manual page of that command.

    man ls
    
  18. find: Search for files and directories. Used to search for files and directories that have names with the pattern provided, we can use the find command. this command goes like this, find, then the path where to look for the file, then -name and the pattern of the name of the file. the example is shown as we want to check for a file with the name nid in its name and it is in the documents folder.

    find /home/juned/Document -name "nid"
    
  19. shutdown: Power off or restart the system. as the command says, this command is used to shutdown the machine.

    sudo shutdown -h now
    
  20. reboot: Restart the system.

    sudo reboot
    
  21. uname: Display system information.

    uname -a
    
  22. date: Display or set the system date and time.

    date
    
  23. alias: Create shortcuts (aliases) for longer commands. this command is handy if you type the same command several times and the command is long to type. you can set an alias for the command and next time you can type the alias to perform the same task but with less typing. In this example, we can set l as the alias to show detailed information about the files and folder in the current folder which we use ls -l to do.

    alias l="ls -l"
    
  24. head / tail: Display the beginning or end of a file. see only the first 10 lines or last 5 lines of a file.

    head -n 10 file.txt
    tail -n 5 file.txt
    
  25. zip / unzip: Compress and extract files using ZIP format.

    zip archive.zip files/
    unzip archive.zip
    

Remember that practice and experimentation are key to becoming proficient with these commands. As you use the Linux command line more frequently, you'll become more comfortable and capable of efficiently managing your system. Stay tuned for more insights and knowledge in my upcoming blogs. Until then, may your terminal adventures be fruitful and your Linux journey continues to unfold!