Latest / CyberCode Academy / Course 4 - Learning Linux Shell Scripting | Episode 2: Essential Unix/Linux Command Line Utilities and Advanced Techniques
Transcript
- 0:00Speaker 1: Welcome back to the deep dive. Today, we're diving into
- 0:03Speaker 1: something that feels a bit like well, an art form,
- 0:06Speaker 1: the Unix and Linux command line, specifically, how we combine
- 0:10Speaker 1: these simple little tools to do really complex stuff with data.
- 0:14Speaker 2: Exactly. It's that whole philosophy, isn't it. Small tools doing
- 0:18Speaker 2: one thing well, but you chain them together.
- 0:21Speaker 1: We're talking gp Awk said find the building.
- 0:26Speaker 2: Blocks right, and today we're really focusing on the core
- 0:30Speaker 2: utilities that you know, handle and transform data streams, stuff
- 0:34Speaker 2: that's absolutely essential for scripting, for cybersecurity, for just wrangling data.
- 0:38Speaker 1: Making those powerful one liners people talk about.
- 0:41Speaker 2: That's the mission. We'll dig into cat for viewing, try
- 0:43Speaker 2: for translating characters, then sort in unique.
- 0:46Speaker 1: For organizing, and the big one, the connector's oargs.
- 0:49Speaker 2: The command line glue. Yeah, let's start with the basics.
- 0:52Speaker 2: Though handling data.
- 0:53Speaker 1: First sounds good. Let's kick off with kat.
- 0:55Speaker 2: Okaykat short for concatnate most people probably know for just
- 0:58Speaker 2: displaying a foul's content on the screen.
- 1:00Speaker 1: Yeah, cat file name done right, But.
- 1:03Speaker 2: It's real job. It's name gives it away. It's concatenating,
- 1:07Speaker 2: joining things together and not just files. It handles standard
- 1:11Speaker 2: input too.
- 1:11Speaker 1: Okay, So how does that work in practice? If I
- 1:14Speaker 1: have data coming from a pipe, say from another command,
- 1:17Speaker 1: and I want to slot of files contents.
- 1:19Speaker 2: In the middle. Yeah, that's where the magic hyphen comes in,
- 1:22Speaker 2: just a single dash diyphen. Yeah. When you use where,
- 1:25Speaker 2: cat or actually many commanding tools expects a file name,
- 1:30Speaker 2: it tells the program okay, read from standard input right here.
- 1:33Speaker 1: So I could do like CAT file one, file two exactly.
- 1:36Speaker 2: And if you pipe data into that command, CAT reads
- 1:38Speaker 2: file one.
- 1:39Speaker 1: Yeah.
- 1:39Speaker 2: Then it reads all the data from the pipe because
- 1:41Speaker 2: of the lord, and then it reads file to it
- 1:43Speaker 2: just weaves it all together.
- 1:44Speaker 1: That is surprisingly simple but really powerful. I can see
- 1:48Speaker 1: how that helps build complex pipelines.
- 1:50Speaker 2: It's one of those little tricks that unlocks a lot. Now.
- 1:52Speaker 2: CAT has other useful bits too, like debugging helpers.
- 1:56Speaker 1: You mean, like finding those sneaky white space issues tabs versus.
- 2:00Speaker 2: Oh, absolutely, the eternal battle, especially in Python right where
- 2:04Speaker 2: indentation is everything.
- 2:05Speaker 1: Tell me about it.
- 2:06Speaker 2: Well. CAT has the dainty T flag capital T. Use that,
- 2:10Speaker 2: and it displays actual tab characters as I carrot I.
- 2:13Speaker 1: Ah, so you can instantly see if it's a tapper spaces.
- 2:16Speaker 2: Precisely makes debugging those kinds of errors way faster than
- 2:19Speaker 2: just staring at the screen wondering where your script is breaking.
- 2:21Speaker 1: Makes the invisible visible. What about line numbering?
- 2:25Speaker 2: Yep in numbers every single line. Good for a quick reference,
- 2:29Speaker 2: But there's also NAYB me B.
- 2:31Speaker 1: How's that different?
- 2:32Speaker 2: Now? B numbers lines too, but it skips the blank ones.
- 2:36Speaker 2: So if you're looking at code or a config file,
- 2:38Speaker 2: it only numbers the lines with actual content, often a
- 2:41Speaker 2: bit cleaner, you know.
- 2:42Speaker 1: Okay, so cat shows and combines. What if we need
- 2:45Speaker 1: to change the data stream itself, like manipulate the characters.
- 2:49Speaker 2: And then you tree the translator free is super powerful
- 2:52Speaker 2: for character stuff, but it's got one main rule. It
- 2:56Speaker 2: only reads from standard input. You can't give it a
- 2:58Speaker 2: file name directly.
- 2:59Speaker 1: Ah okay, so it's always part of a pipe.
- 3:01Speaker 2: Pretty much always. Yeah. Yeah. Its job is substitution, deletion,
- 3:04Speaker 2: or squeezing characters.
- 3:05Speaker 1: Substitution like changing case lowercase to uppercase.
- 3:09Speaker 2: That's a classic example. Tr az reads Tlindon converts all
- 3:14Speaker 2: upper case letters to lowercase, and Prince dislick now dead simple?
- 3:18Speaker 1: Nice? What else I heard something about simple crypto?
- 3:21Speaker 2: Yeah, you can use tree R for the rot thirteen cipher.
- 3:25Speaker 2: It's a basic substitution site whe where you rotate the
- 3:27Speaker 2: alphabet by thirteen places, so A becomes n, B becomes
- 3:30Speaker 2: O and so on.
- 3:32Speaker 1: How does tre R handle that?
- 3:33Speaker 2: You just give it the two sets of characters. The
- 3:35Speaker 2: first set is az, the second is nz AM. Tree
- 3:38Speaker 2: R maps the first character in set one to the
- 3:40Speaker 2: first and set two second to second, et cetera.
- 3:42Speaker 1: And the cool part.
- 3:42Speaker 2: The cool part is ROT thirteen is symmetrical. You run
- 3:45Speaker 2: the exact same tree of command to encrypt it, and
- 3:47Speaker 2: then run it again on the output to decrypt it.
- 3:49Speaker 1: Huh meat trick. Okay, what about squeezing You mentioned that.
- 3:52Speaker 2: Right the any ass option. This is great for cleaning
- 3:55Speaker 2: up messy text. Say you have a log file with
- 3:57Speaker 2: like tons of extra spaces between words, or maybe multiple
- 4:01Speaker 2: blank lines in a row.
- 4:02Speaker 1: Yeah, that happens.
- 4:02Speaker 2: Triarsby will squeeze any sequence of multiple spaces down to
- 4:06Speaker 2: just as single space or Triarstean squeezes multiple blank lines
- 4:10Speaker 2: into one. It just tidies things up. Very handy for
- 4:13Speaker 2: pre processing text. Okay, so we've viewed, combined, and transformed.
- 4:17Speaker 2: What's next? Usually we need to organize it.
- 4:19Speaker 1: Right exactly organization and that brings us straight to sort.
- 4:24Speaker 2: Sort seems straightforward. It sorts things, it does.
- 4:26Speaker 1: Takes input from files or stide in writes the sordid
- 4:30Speaker 1: result to stout simple premise. But the power isn't the
- 4:35Speaker 1: how how you tell it to sort.
- 4:36Speaker 2: Beyond just like dadish R for reverse order.
- 4:40Speaker 1: Oh yeah, the big one is datish N for numeric sorting.
- 4:43Speaker 1: And then there's key sorting.
- 4:44Speaker 2: Okay, tell me about keys. If I have data in columns,
- 4:47Speaker 2: maybe like a CSV or just space separated output, and
- 4:50Speaker 2: I want to sort based on say the third column.
- 4:53Speaker 1: That's the dasher flag, you'd use nash A three to
- 4:55Speaker 1: specify the third column the third key. Simple. But here's
- 4:58Speaker 1: the catch. The thing everyone for that's initially by default
- 5:02Speaker 1: sorts alphabetically lexicographically.
- 5:05Speaker 2: Ah so if my third column has numbers like one
- 5:08Speaker 2: hundred and twenty.
- 5:09Speaker 1: Without nash en sort will put one hundred and before
- 5:11Speaker 1: twenty because one comes before too.
- 5:12Speaker 2: Write alphabetical comparison exactly. So if you're sorting by a
- 5:16Speaker 2: column that contains numbers, you must use tie in along
- 5:19Speaker 2: with k so nine s K three inn that tells
- 5:22Speaker 2: it sort based on column three and treat the data
- 5:25Speaker 2: in that column numerically.
- 5:26Speaker 1: Got it? Nice k for the column nice en if
- 5:30Speaker 1: it's numbers. Can you get even more specific, like sort
- 5:33Speaker 1: based on just part of a line, not a whole column?
- 5:35Speaker 2: You absolutely can. You can specify character positions like sort
- 5:39Speaker 2: based only on characters four through eight on each line.
- 5:41Speaker 2: It's really flexible for fixed with data or complex formats.
- 5:45Speaker 1: Wow, okay, very precise. So once everything is perfectly sorted,
- 5:49Speaker 1: what comes next? Getting rid of duplicates or analyzing them.
- 5:52Speaker 2: That's where unique comes in spelled un iq unique and
- 5:56Speaker 2: a unique has one golden rule, probably the most important
- 5:58Speaker 2: thing to remember about it, which is it required a
- 6:00Speaker 2: sortied input absolutely mandatory. Why is that because unique only
- 6:04Speaker 2: compares adjacent lines. If you have two identical lines but
- 6:07Speaker 2: they're separated by a different line, Unique won't see them
- 6:10Speaker 2: as duplicates. Sorting first puts all identical lines right next
- 6:13Speaker 2: to each other.
- 6:14Speaker 1: Okay, that makes sense, Sort first, then unique. So what
- 6:17Speaker 1: can unique actually do once the input is sorted?
- 6:20Speaker 2: Well, the default behavior is just to print one copy
- 6:22Speaker 2: of each unique line. It filters out their repeated ones.
- 6:25Speaker 1: Useful. But are there other modes? Oh?
- 6:27Speaker 2: Yeah? The really interesting flags are id and CD and
- 6:31Speaker 2: c idgs you only the lines that were duplicated. So
- 6:35Speaker 2: if you want to find just the entries that appeared
- 6:36Speaker 2: more than once, DD is your friend.
- 6:39Speaker 1: Okay, isolates the duplicates.
- 6:41Speaker 2: Z nice is for counting it prefixes each unique line
- 6:45Speaker 2: with account of how many times that line appeared in
- 6:47Speaker 2: the original sordid input. Instant frequency analysis super useful.
- 6:52Speaker 1: Ah so sort you need to ce is classic combo
- 6:55Speaker 1: for getting counts of things.
- 6:56Speaker 2: One of the most common and powerful patterns on the
- 6:58Speaker 2: command line.
- 6:59Speaker 1: Definitely okay, this is great. We can view translate, sort
- 7:03Speaker 1: analyze uniqueness. But you mentioned glue earlier connecting commands, right,
- 7:08Speaker 1: the glue sargs.
- 7:09Speaker 2: This tackles a fundamental limitation of pipes, which is pipes
- 7:13Speaker 2: are awesome, right Yeah. They take the standard output of
- 7:15Speaker 2: one command and feed it directly into the standard input
- 7:18Speaker 2: of the next command stream processing. But lots of commands
- 7:22Speaker 2: don't read from standard input. Think about roam for deleting files,
- 7:26Speaker 2: or envy for moving them, or even just running a
- 7:29Speaker 2: custom script on a bunch of files.
- 7:30Speaker 1: Ah. Right, They expect you to type the filenames after
- 7:33Speaker 1: the command as arguments.
- 7:34Speaker 2: Exactly. They want command line arguments not a stream of
- 7:37Speaker 2: data piped into them. If you run fine type f ARM,
- 7:41Speaker 2: it just won't work. Armm ignores the piped input.
- 7:45Speaker 1: So how do we bridge that gap? How do we
- 7:47Speaker 1: take a list of filenames coming out of say find
- 7:49Speaker 1: and feed them as arguments to ARM.
- 7:52Speaker 2: That is precisely the job of zargs. It reads items
- 7:55Speaker 2: from standard input, like.
- 7:57Speaker 1: The list of filenames from find YEP.
- 7:59Speaker 2: And it makes those items, bundles them up, and executes
- 8:02Speaker 2: another command, passing those items as command line arguments to it.
- 8:05Speaker 2: It converts stin into arguments.
- 8:06Speaker 1: It's like a translator between piping and arguing.
- 8:08Speaker 2: That's a good way to put it, And as simplified
- 8:10Speaker 2: effect is if you just pipe multiline text into zarg's
- 8:13Speaker 2: echo for example, zargs reads each line, removes the new line,
- 8:16Speaker 2: adds a space instead, and feeds it all to echo.
- 8:19Speaker 2: Instant way to turn lines into a single space separated string.
- 8:22Speaker 1: Huh neat. But the main use is with commands like fine.
- 8:26Speaker 2: Yes, But there's a huge safety issue here, a really
- 8:29Speaker 2: important one.
- 8:30Speaker 1: Oh what's that?
- 8:31Speaker 2: By default? How does find separate the filenames?
- 8:35Speaker 1: It outputs uh with a new line character right each
- 8:37Speaker 1: file on a new line correct.
- 8:39Speaker 2: And by default, how does zarg separate the items it
- 8:42Speaker 2: reads from spridden to turn them.
- 8:44Speaker 1: Into arguments spaces and new lines?
- 8:48Speaker 2: Exactly? It treats spaces in new lines as delimiters. Now,
- 8:51Speaker 2: what happens if you have a file name with a
- 8:52Speaker 2: space in it, like my important document dot txt.
- 8:56Speaker 1: Uh oh? If Fine outputs that.
- 8:58Speaker 2: Linears reads, it seese the space and thinks it's two
- 9:01Speaker 2: separate items, my and importance.
- 9:04Speaker 1: And then it passes my and important as separate arguments
- 9:07Speaker 1: to whatever command you told it to run.
- 9:08Speaker 2: Like are disaster waiting to happen. You could delete the
- 9:11Speaker 2: wrong things or just completely fail. You cannot safely pipe
- 9:14Speaker 2: the default output of Fine directly tosargs. If your file
- 9:18Speaker 2: imes might contain spaces or other weird characters.
- 9:20Speaker 1: Okay, that's genuinely dangerous. So what's the safe way? The
- 9:23Speaker 1: professional way?
- 9:24Speaker 2: The solution involves changing the delimiter both programs use. You
- 9:27Speaker 2: tell Fine to use a different separator using its print.
- 9:30Speaker 1: Zero option perject fear. What does that do?
- 9:32Speaker 2: Instead of separating Fine names with a new line, it
- 9:35Speaker 2: separates them with a null character. Ask the character zero
- 9:38Speaker 2: zero zero. It's the one character that absolutely cannot appear
- 9:41Speaker 2: in a valid Unix Linux file name ah.
- 9:43Speaker 1: A completely safe separator exactly.
- 9:46Speaker 2: Then you tell disargs to expect this null character as
- 9:50Speaker 2: the delimiter using its null option.
- 9:52Speaker 1: That's dash zero, so fine to print three levers zero
- 9:55Speaker 1: Zero's a null Lulero's thing.
- 9:57Speaker 2: That's the magic combo find outputs null separator names. Zargs
- 10:01Speaker 2: nero reads null separated names and everything works perfectly, even
- 10:04Speaker 2: with spaces, new lines, quotes, whatever in the file names
- 10:08Speaker 2: always use null prints zero with no zero.
- 10:10Speaker 1: Good tip essential. Really is this generally better than using
- 10:14Speaker 1: Fine's own exec option?
- 10:15Speaker 2: Often yes, especially for performance those exec usually runs the
- 10:19Speaker 2: command once for every single file found. Zargs, on the
- 10:22Speaker 2: other hand, is smart. It gathers up a whole bunch
- 10:24Speaker 2: of file names from the input and runs the command
- 10:26Speaker 2: just once, with a long list of arguments that it
- 10:28Speaker 2: grabs the next batch.
- 10:29Speaker 1: Fewer command invocations, less overhead, makes sense.
- 10:31Speaker 2: Much more efficient for large numbers of files.
- 10:33Speaker 1: Okay, sargs is clearly more than just glue. It's pretty sophisticated.
- 10:37Speaker 1: Can it do more? You mentioned performance?
- 10:39Speaker 2: Yes. Building on that idea of running commands efficiently, sargs
- 10:43Speaker 2: can help us leverage modern hardware inlet CPUs today multiple cores.
- 10:48Speaker 2: Everything's multi core, right, But if you just run a
- 10:50Speaker 2: command sequentially processing one file, then the next, than the next,
- 10:53Speaker 2: you're probably only using one of those cores. The others
- 10:56Speaker 2: are just sitting.
- 10:57Speaker 1: Idle leaven performance on the table.
- 10:59Speaker 2: Totally, especially for CPU intensive tasks. Imagine you need to calculate,
- 11:04Speaker 2: say the MD five checksum for thousands of large files.
- 11:08Speaker 2: Doing that one by one could take ages.
- 11:11Speaker 1: So how does zargs help parallelize this?
- 11:13Speaker 2: It has another brilliant flag capital P this list you
- 11:16Speaker 2: specify the maximum number of processes to run.
- 11:19Speaker 1: In parallel, ah, so I can tell it how many
- 11:21Speaker 1: cores to use.
- 11:22Speaker 2: Pretty much, if you have say an eight core machine,
- 11:25Speaker 2: you could pipe your list of file names from find Entderpin,
- 11:28Speaker 2: martyrprint into zargs near some mail P eight, Mirrorlevinus, sum
- 11:32Speaker 2: and gargs would it would start up to eight milliary
- 11:34Speaker 2: five sum processes simultaneously. As soon as one finishes processing
- 11:38Speaker 2: its batch of files, zargs feeds it more work, keeping
- 11:42Speaker 2: those eight processes busy until all the files.
- 11:44Speaker 1: Are done, so it keeps multiple cores saturated with.
- 11:47Speaker 2: Work exactly for tasks where the CPU is the bottleneck.
- 11:50Speaker 2: This can give you a massive speed up like nearly
- 11:53Speaker 2: eight times faster in that example. Potentially it transforms sequential
- 11:57Speaker 2: processing into concurrent processing.
- 11:59Speaker 1: All with one extra flag on zars. That's impressive.
- 12:02Speaker 2: It really showcases how these tools, especially when combined correctly,
- 12:07Speaker 2: let you harness the full power of your system.
- 12:08Speaker 1: Okay, this has been a fantastic tour from just viewing
- 12:12Speaker 1: files with CAT to complex parallel processing with sargs.
- 12:16Speaker 2: And that's the beauty of it, isn't it. The synergy
- 12:19Speaker 2: Cat feeds two are, which feeds sort, which feeds unique,
- 12:22Speaker 2: which might feed zargs. Each step is simple, but the
- 12:25Speaker 2: combination is incredibly powerful and flexible. That's the art we
- 12:29Speaker 2: talked about at the start.
- 12:30Speaker 1: We really covered a lot making tabs visible with CAT,
- 12:33Speaker 1: dash TT, the rot thirteen trick with tr why you
- 12:36Speaker 1: must sort before unique, especially for counting with.
- 12:39Speaker 2: Nush C, and the absolute necessity of fine Nichish print
- 12:43Speaker 2: zergs nice zero for safety, plus the performance boost from
- 12:46Speaker 2: zargs nicetp Right.
- 12:48Speaker 1: So, to wrap this up and give people something to
- 12:50Speaker 1: chew on about, a little review challenge sounds good.
- 12:53Speaker 2: Let's test that pipeline. Thinking.
- 12:54Speaker 1: Okay, imagine you've got a big configuration file. Let's say
- 12:58Speaker 1: every important line ends with a semicolon. But the file's messy,
- 13:03Speaker 1: lots of duplicate lines, maybe with minor variations, if not
- 13:06Speaker 1: for that semi colon. You need a count of each
- 13:09Speaker 1: unique configuration setting ignoring the trailing semicolon.
- 13:12Speaker 2: Okay, so move the semicolon, then count the unique lines
- 13:15Speaker 2: that result.
- 13:16Speaker 1: Exactly what sequence of commands? What pipeline would you build
- 13:19Speaker 1: to do that?
- 13:20Speaker 2: Ooh, nice one, it brings it all together. You'd need
- 13:22Speaker 2: to pipe the file content first in to try to
- 13:24Speaker 2: delete the semi colon.
- 13:26Speaker 1: TRYYID perhaps yep.
- 13:29Speaker 2: Then take that cleaned up output and pipe it into
- 13:31Speaker 2: sort because unique needs sorted input.
- 13:33Speaker 1: Makes sense.
- 13:33Speaker 2: And finally pipe the output of sort into unique. Nice
- 13:36Speaker 2: c to get that count of each unique line.
- 13:39Speaker 1: Perfect catfile trer, uid sort uniqueness. That single line does
- 13:43Speaker 1: the whole job.
- 13:44Speaker 2: Master that kind of thing and combining these tools and
- 13:47Speaker 2: you unlock so much power for scripting, data analysis, security
- 13:52Speaker 2: tasks you name it.
- 13:53Speaker 1: Couldn't agree more. Keep practicing, keep exploring these commands. Thanks
- 13:56Speaker 1: for tuning in to the deep dive