Hack Like a Pro: Linux Basics for the Aspiring Hacker, Part 10 (Manipulating Text) « Null Byte :: WonderHowTo

Welcome back, my aspiring hackers!As mentioned several times inprevious Linux tutorials, nearly everything in Linux is a file, and very often they are text files. For instance, all of the configuration files in Linux are text files. To reconfigure an application in Linux, we simply need to open the configuration file, change the text file, re-save, and then restart the application and our reconfiguration is applied.With so many text files, manipulating text becomes crucial in managing Linux and Linux applications. In this tutorial, we'll look at several of the commands and techniques for manipulating text in Linux. For demonstration purposes, we'll use files from the world's best NIDS,Snort.Step 1: Cat That FileAs demonstrated in anearlier tutorial,catis probably the most basic text display command. Let's cat the Snort config file found in/etc/snort.cat /etc/snort/snort.confAs you can see, the snort.conf is displayed on our screen until it comes to the end of the file. Not the most convenient way to work with this file.Step 2: Take the HeadIf we just want to view the beginning of a file, we can use theheadcommand. This command displays the first 10 lines of a file, by default.head /etc/snort/snort.confIf we want to see more or less than the default 10 lines, we can tell head how many lines we want to see by putting the number of lines we want to see (with the-switch) between the command and the file name.head -30 /etc/snort/snort.confHere we can see that only the first 30 lines of snort.conf are displayed.Step 3: Grab That TailSimilar to the head command, we view the last lines of a file by using thetailcommand. Let's use it on the snort.conf.tail /etc/snort/snort.confNotice that it displays some of the last "includes" of the rules files, but not all of them. Let's now see if we can display all the rule "includes" by grabbing the last 40 lines of the snort.conf.tail -40 /etc/snort/snort.confNow we can view nearly all the rule includes all on one screen.Step 4: Numbering Those LinesSometimes—especially with very long files—we may want the file displayed with line numbers. This is probably the case with the snort.conf, as it has 838 lines. This makes it easier to reference changes and come back to the same place within a file. To display a file with line number, we simply type:nl snort.confNote that each line now has a number making referencing much easier.Step 5: I Grep ThatAfter cat,grepis probably the most widely used text manipulation command. It's a filtering command; in other words, it enables us to filter the content of a file for display. If for instance, we wanted to see all the instances of where the word "database" occurs in our snort.conf file, we could ask cat to only display those lines where it occurs by typing:cat /etc/snort/ snort.conf | grep databaseThis command will first grab the snort.conf and then "pipe" it (|) to grep which will take it as input and then look for the occurrences of the word "database" and only display those lines. Grep is a powerful and essential command for working in Linux as it can save us hours searching for every occurrence of a word or command.Step 6: I Sed That WorksThesedcommand essentially allows us to search for occurrences of a word or text pattern and then do some work on it. The name comes from the concept of a stream editor and is a contraction of those two words. In its most basic form, sed operates like the find and replace function in Windows. Let's search for the word "mysql" in the snort.conf file using grep.cat /etc/snort/snort.conf | grep mysqlWe can see that the grep command found five occurrences of the word mysql.Let's say we want sed to replace every occurrence of mysql and with MySQL (remember, Linux is case sensitive) and then save the new file to snort2.conf. We could do this by typing:sed s/mysql/MySQL/g snort.conf > snort2.confThis command says, "search (s) for the word mysql and replace it with the word MySQL globally (i.e. wherever you find it in the file)."Now, when we grep snort2.conf for mysql, we see that none were found and when we grep for MySQL, we find five occurrences of MySQL.cat /etc/snort/snort.conf | grep MySQLIf we just want to replace only the first occurrence of the word mysql, we could leave out the trailinggand it would only replace the first occurrence.sed s/mysql/MySQL/ snort.conf > snort2.confThe sed command can also be used to find and replace any specific occurrence of a word. For instance, if I want to only replace the third occurrence of the word mysql, I can simply place the number of the occurrence at the end of the command and sed will only replace the third occurrence of the word "mysql" with "MySQL".sed s/mysql/MySQL/3 snort.conf > snort2.confStay Tuned for MoreThat's it for this lesson, but there are many more to come, so check out our section onlearning Linux basicsto stay up to date. If you have any questions on using sed, cat, head, tail, nl, or grep, ask away in the comments below. You can also visit theNull Byte forumfor help on unrelated matters.Want to start making money as a white hat hacker?Jump-start your hacking career with our2020 Premium Ethical Hacking Certification Training Bundlefrom the newNull Byte Shopand get over 60 hours of training from cybersecurity professionals.Buy Now (90% off) >Other worthwhile deals to check out:97% off The Ultimate 2021 White Hat Hacker Certification Bundle99% off The 2021 All-in-One Data Scientist Mega Bundle98% off The 2021 Premium Learn To Code Certification Bundle62% off MindMaster Mind Mapping Software: Perpetual LicenseOriginal penguin eyeimage via ShutterstockRelatedNews:How to Study for the White Hat Hacker Associate Certification (CWA)Hack Like a Pro:Linux Basics for the Aspiring Hacker, Part 11 (Apache Web Servers)How To:The Essential Skills to Becoming a Master HackerHack Like a Pro:Linux Basics for the Aspiring Hacker, Part 6 (Networking Basics)Hack Like a Pro:Linux Basics for the Aspiring Hacker, Part 1 (Getting Started)Hack Like a Pro:Scripting for the Aspiring Hacker, Part 3 (Windows PowerShell)How to Hack Like a Pro:Getting Started with MetasploitNews:What to Expect from Null Byte in 2015Hack Like a Pro:Perl Scripting for the Aspiring Hacker, Part 1Hack Like a Pro:Linux Basics for the Aspiring Hacker, Part 13 (Mounting Drives & Devices)Hack Like a Pro:Linux Basics for the Aspiring Hacker, Part 12 (Loadable Kernel Modules)Hack Like a Pro:Linux Basics for the Aspiring Hacker, Part 22 (Samba)Hack Like a Pro:Linux Basics for the Aspiring Hacker, Part 25 (Inetd, the Super Daemon)Hack Like a Pro:Linux Basics for the Aspiring Hacker, Part 21 (GRUB Bootloader)Hack Like a Pro:Linux Basics for the Aspiring Hacker, Part 2 (Creating Directories & Files)Hack Like a Pro:Linux Basics for the Aspiring Hacker, Part 24 (The Linux Philosophy)Hack Like a Pro:Linux Basics for the Aspiring Hacker, Part 17 (Client DNS)Hack Like a Pro:Linux Basics for the Aspiring Hacker, Part 9 (Managing Environmental Variables)Hack Like a Pro:Linux Basics for the Aspiring Hacker, Part 5 (Installing New Software)Hack Like a Pro:An Introduction to Regular Expressions (Regex)Hack Like a Pro:Finding Potential SUID/SGID Vulnerabilities on Linux & Unix SystemsHack Like a Pro:Linux Basics for the Aspiring Hacker, Part 23 (Logging)Goodnight Byte:HackThisSite Walkthrough, Part 9 - Legal Hacker TrainingGoodnight Byte:HackThisSite Walkthrough, Part 6 - Legal Hacker TrainingGoodnight Byte:HackThisSite Walkthrough, Part 10 - Legal Hacker TrainingCommunity Byte:HackThisSite Walkthrough, Part 9 - Legal Hacker TrainingCommunity Byte:HackThisSite Walkthrough, Part 10 - Legal Hacker TrainingGoodnight Byte:HackThisSite Walkthrough, Part 4 - Legal Hacker TrainingCommunity Byte:HackThisSite Walkthrough, Part 6 - Legal Hacker TrainingGoodnight Byte:HackThisSite Walkthrough, Part 5 - Legal Hacker TrainingGoodnight Byte:HackThisSite Walkthrough, Part 8 - Legal Hacker TrainingCommunity Byte:HackThisSite Walkthrough, Part 1 - Legal Hacker TrainingHack Like a Pro:Hacking Samba on Ubuntu and Installing the MeterpreterCommunity Byte:HackThisSite Walkthrough, Part 2 - Legal Hacker TrainingNews:Let Me Introduce MyselfHow To:How Hackers Take Your Encrypted Passwords & Crack Them