Category: Linux

It groups together posts about Linux.

  • Mastering Essential Command Line Tools: Grep, Find, Locate, Piping, Awk, Nano, and Xargs Demystified

    Welcome to a guide on essential command line tools. Let’s explore the functionality of grep, find, locate, piping, awk, nano, and xargs. These tools are indispensable for navigating and manipulating files and data in the command line environment.

    Grep

    Grep is a powerful tool for searching text files. It allows you to find specific patterns or text strings within files quickly.

    Example usages:

    grep "michael" names.txt #It searches for the word "michael" inside the file "names.txt that's in the current directory
    grep -r "michael" .. #It searches recursively for all the occurrences of the word "michael" in all the subdirectories of the current directory's parent directory
    grep -i "jovem" captura.txt #It searches for "jovem" in "captura.txt" file, but this time case insensitive
    

    Find

    Find helps you locate files and directories within the filesystem. It’s useful for searching for files based on various criteria like name, size, or modification date.

    Example usage:

    find . -name '*.txt' #It searches for all the files with extension "txt" located in the current directory
    

    Locate

    Locate provides a fast way to search for files on your system. It uses a pre-built database of filenames to quickly locate files without having to search the entire filesystem. This command is simply awesome: with two words you save time (and your job)!

    Example usage:

    locate pg_hba.conf
    

    In case you find nothing, but you’re sure that the file exists, maybe the indexes are outdated and you have to run (golden hint):

    sudo updatedb
    

    Piping

    Piping allows you to combine the output of one command with the input of another. It’s useful for chaining commands together to perform complex operations.

    Example usage:

    find . -name "*.txt" | grep -i "africa" #It will give you all the occurrences of the word "africa" (case insensitive) in all the txt files in the current directory
    

    Awk

    Awk is a versatile text processing tool. It allows you to manipulate and analyze text data using simple scripts.

    Example usage:

    awk '{print $1}' file.txt #It shows the first word or element of each line from file.txt
    

    Nano

    Nano is a lightweight text editor that’s easy to use. It’s ideal for making quick edits to files in the command line environment.

    Example usage:

    nano filename
    

    Xargs

    Xargs is used to build and execute command lines from standard input. It’s useful for processing large sets of data or files.

    Example usage:

    find . -name '*.tmp' | xargs rm #It deletes all the .tmp files from the current directory
    

    These command line tools are essential for performing various tasks efficiently. Whether you’re searching for files, manipulating text data, or editing files, mastering these tools will enhance your productivity on the command line.

    By Igor Magalhães Oliveira

  • History, grep, piping and something else: a true case

    How many times did someone give you the task “this system doesn’t have documentation, it had a failure and stopped and we don’t know how to put this system up again… help us!”?

    It’s quite common when the knowledge management is not taken seriously inside the company and you have only weak documentation. The problem worsens when you have to handle legacy systems. Rely on me: i know what i’m telling you, so i give you my shoulder.

    But calm down. If you have a terminal and you can access the machine where the system should be running, maybe we shall solve everything.

    history

    The history command really can save your life. It lists the last commands typed in the Linux terminal, by the current user.

    So, in case you need access the list of commands typed by another user, please follow the sequence:

    sudo su
    su - user_who_typed_something_good
    history
    

    It’s suitable to say that when you type “su – user” you change the current user to user, but, more than that, you’ll be transported to his home directory with all of his environment variables and shell environment.

    fc

    It stands for “find command” and it will show you all the executed commands between two positions, like fc -l 100 200 that will show you the last commands between the positions 100 and 200.

    Now, i think we know a lot about these commands and it’s time to dive deeply and see a practical case. Let’s say that the history command give us:

    Unfortunately we had to scroll too much and we lost time (and eyes) doing that. If for example, we were looking for the occurrences of commands with the expression “fetch” we could, more wisely, use our famous grep command in order to pipe and filter the result of the history command:

    history | grep fetch

    Et voilà!

    As of now, don’t forget: if you use an OS based in Linux, history and grep could save your life. Once upon a time, they saved mine.

    By Igor Magalhães Oliveira

  • Linux command line crash course: main introductory terminal commands

    ln the Information Technology world is quite common to face Linux systems, either because it’s “cheaper” to keep Linux server with good security tools, or because it provides the user a more complete control of the operating system.

    Regardless the motivation behind the choice for using Linux systems, it’s an obligatory requirement to the IT professional know how to use, even a little, the Command Line Interface. I’ll help you with that, giving you a minimal set of “have to know” commands.

    LS

    It stands for “list”. Yes: it simply lists the content of the current folder, showing you files and folders.

    ls -l

    It shows everything in a “long format”, providing informations about permissions, ownership, size, etc.

    At the image above, “r” stands for “read”, “w” for “write” and “x” for “execute”, permissions. We have three blocks with those permissions, each one containing the sequence “rwx”: the first block corresponds to the permission of the owner; the second, permissions of the owner’s group; finally, the third, other users’ permissions.

    ls -lh

    Similar to the previous one, but it shows sizes in a “human” readable shape, showing unities like “MB”, “KB”, and so on, instead of “bytes”. Pay attention to this “h” in linux, because almost always it stands for “human readable”.

    ls -a

    It shows all, including hidden files and folders.

    ls ..

    It shows the content of the parent directory. Pay attention to the two dots: it will always represent the parent directory.

    PWD

    It stands for “Print Working Directory” and it’s exactly what this command does.

    CD

    It allows you to “enter” in the specified directory.

    cd ..

    It puts you in the parent directory.

    cd ~

    It puts you in the current user’s home.

    cd –

    It’s a magical command that takes you to the previous visited directory. Realise the difference between it and “cd ..”. Look the image below:

    Did you get it?

    MKDIR

    It creates a specified directory

    CP

    It copies a file or directory. In the case of directories, you have to add the parameter “-r” in order to do the copy recursively.

    MV

    It moves a file or directory to another location. We may use mv command to provide a simple strategy to turn a specified file as a hidden file, like that:

    mv file.txt .file.txt

    When you have a file name starting with a dot in Linux, it means it’s a hidden file.

    Did you get it?

    MKDIR

    It creates a specified directory

    Friends, for now it’s all! I hope you liked! Keep in touch for suggestions!

    By Igor Magalhães Oliveira