Latest / CyberCode Academy / Course 4 - Learning Linux Shell Scripting | Episode 3: Comprehensive Unix File and Directory Management Utilities
Transcript
- 0:00Speaker 1: Welcome back to the deep dive. Today. We're grabbing kind
- 0:03Speaker 1: of a master key for the command line. Our mission.
- 0:06Speaker 1: We want to strip away the usual layers and really
- 0:08Speaker 1: get into the most powerful, the most precise tools that
- 0:12Speaker 1: Unix and Linux systems use to manage data. We're talking
- 0:15Speaker 1: about unlocking some serious control over your environment for better security,
- 0:19Speaker 1: better speed.
- 0:20Speaker 2: Yeah, this is really the bedrock when you're in a
- 0:23Speaker 2: Unix like environment. The whole system follows this one, really
- 0:26Speaker 2: elegant idea. Almost everything is treated like a file. Your directories, hardware, devices,
- 0:31Speaker 2: network connections, even memory. It all looks like a file
- 0:35Speaker 2: to the operating system kernel.
- 0:36Speaker 1: Okay, so if everything is a file, then mastering the
- 0:39Speaker 1: tools that manipulate files, well, that means you basically gain
- 0:43Speaker 1: control over the whole system's inner workings.
- 0:45Speaker 2: Right exactly. It's the shortcut really to becoming properly informed.
- 0:49Speaker 2: Doesn't matter if you're managing systems day to day or
- 0:51Speaker 2: your deep in development work.
- 0:52Speaker 1: We're moving way beyond just copying files here. This is
- 0:55Speaker 1: about raw system manipulation.
- 0:57Speaker 2: And we need to understand the logic behind these low
- 1:00Speaker 2: level tools. So let's start with one that's well, it's
- 1:03Speaker 2: incredibly powerful, but also carry some real risk d d
- 1:07Speaker 2: D often called the data duplicator.
- 1:09Speaker 1: That's right, and it's crucial for jobs where you need
- 1:12Speaker 1: absolute precision with data.
- 1:14Speaker 2: Right, because most people just use CP to copy files.
- 1:16Speaker 2: So why DD what makes it so different?
- 1:18Speaker 1: Well? DD works at the block level. It doesn't really
- 1:21Speaker 1: care about files and the way C does. It just
- 1:23Speaker 1: clones raw inputs straight to output block level. Okay, you
- 1:26Speaker 1: tell the input file with if and the output file
- 1:29Speaker 1: with of, and it's essential for things like cloning entire
- 1:33Speaker 1: drives or you know, for our first practical example, making
- 1:36Speaker 1: files of a very specific size for testing things.
- 1:40Speaker 2: So instead of the system deciding how the file gets
- 1:42Speaker 2: put together, we dictate it bite by bite almost pretty much.
- 1:45Speaker 1: You control the building blocks. You use BS for the
- 1:48Speaker 1: block size, how many bytes in each chunk, and count
- 1:51Speaker 1: for how many of those chunks to copy.
- 1:52Speaker 2: So if I needed, say a ten megabyte test file,
- 1:55Speaker 2: you could do BS one M that's one megabyte per block,
- 1:59Speaker 2: and then count ten ten blocks. Easy. And you can
- 2:03Speaker 2: use the standard k MG for kilobytes megabytes igabytes usually
- 2:09Speaker 2: based on UNDERD and twenty four bytes the standard for
- 2:12Speaker 2: memory and storage.
- 2:12Speaker 1: Okay, but if you're creating a file like that, you
- 2:14Speaker 1: need something to fill it with, and that's where dev
- 2:17Speaker 1: zero comes in. Right, that's one of those special file things.
- 2:19Speaker 2: Exactly. Dev zero isn't actual storage. It's a like an
- 2:23Speaker 2: interface the kernel provides. Anytime you try to read from it,
- 2:25Speaker 2: it just gives you an endless stream of null bytes zeros.
- 2:29Speaker 1: So perfect input for dd if you just need a
- 2:32Speaker 1: big blank file for testing something or maybe starting a
- 2:35Speaker 1: disk image.
- 2:36Speaker 2: Precisely, it's an inexhaustible source of zeros.
- 2:38Speaker 1: But you mentioned risk. Because DDD is so low level,
- 2:41Speaker 1: I really need to stress this caution point, don't we
- 2:43Speaker 1: It is.
- 2:44Speaker 2: Dangerous extremely if you mix up your input file and
- 2:47Speaker 2: your output file, or you just point of at the
- 2:49Speaker 2: wrong device. Yeah, you can instantly overwrite an entire disk. Gone,
- 2:53Speaker 2: no questions asked.
- 2:54Speaker 1: Wow, okay, so double check, triple check that of parameter.
- 2:57Speaker 2: Absolutely. Precision isn't optional with the DS. It demands your
- 3:01Speaker 2: full attention because it skips a lot of the normal
- 3:03Speaker 2: safety checks.
- 3:05Speaker 1: Okay, so we've carefully created some data. Now what about
- 3:08Speaker 1: analyzing it, making sure things are right, cleaning up duplicates.
- 3:11Speaker 1: Maybe you can actually use mathematical set logic on text
- 3:15Speaker 1: files with a command called Colm.
- 3:17Speaker 2: That's right, Colm, Actually comm think like venn diagrams, but
- 3:22Speaker 2: for lines and files. Intersection difference that kind of thing, okay,
- 3:26Speaker 2: com yeah, com. It compares two files line by line.
- 3:29Speaker 2: But here's the catch, the absolute requirement. Both files must
- 3:33Speaker 2: be sorted alphabetically first, otherwise it just doesn't work correctly.
- 3:37Speaker 1: Towarded first, got it, and the output is pretty deep, right.
- 3:39Speaker 2: Structured, very structured. By default. You get three columns column
- 3:43Speaker 2: one dot lines that are only in the first file,
- 3:46Speaker 2: call them two dot lines only in the second file.
- 3:48Speaker 2: And column three often the most useful one lines that
- 3:51Speaker 2: are common to both files. That's your intersection.
- 3:53Speaker 1: Okay, So if I only wanted the common lines the intersection,
- 3:56Speaker 1: I tell it to suppress columns one.
- 3:58Speaker 2: And two exactly. Or if you wanted what's called the
- 4:00Speaker 2: set difference, say everything in file A that's not in
- 4:03Speaker 2: file B, you'd suppress columns two and three. You control
- 4:05Speaker 2: this with simple number flax muodi one manus two minus three.
- 4:10Speaker 1: That sounds incredibly useful for scripting, especially well, especially for
- 4:13Speaker 1: finding duplicate files, which you mentioned, because you can't just
- 4:16Speaker 1: rely on file names being the same, right, The content
- 4:19Speaker 1: has to.
- 4:19Speaker 2: Match precisely and that brings us to checksums. The way
- 4:23Speaker 2: we guarantee content is identical is by creating a unique
- 4:26Speaker 2: digital fingerprint for the file's.
- 4:28Speaker 1: Data, like an MD five sum or sahasum.
- 4:30Speaker 2: Yep MD five SAHA one, SAHA two, five six, any
- 4:34Speaker 2: of those. They take the file's contents and generate a short,
- 4:37Speaker 2: fixed length string. If even one tiny bit inside the
- 4:41Speaker 2: file changes, the checksum changes completely, so identical content always
- 4:45Speaker 2: produces the exact same checksum.
- 4:46Speaker 1: Okay, but calculating a checksum on say a huge video
- 4:49Speaker 1: file takes time and CPU power, so finding duplicates across
- 4:53Speaker 1: a whole drive could be slow. Is there a smarter way?
- 4:55Speaker 2: There is the efficiency hack. The smart way is to
- 4:58Speaker 2: first filter by file size, use something like l's l's.
- 5:01Speaker 1: Ah because identical files have to be the same.
- 5:03Speaker 2: Size exactly, so you only bother calculating the checksums for
- 5:07Speaker 2: files that already have matching sizes. That cuts down the
- 5:10Speaker 2: workload massively. Then you can use tools like AWK maybe
- 5:14Speaker 2: and definitely come with that set logic to compare the checksums,
- 5:17Speaker 2: find the duplicates, and decide which ones to keep and
- 5:20Speaker 2: which to delete.
- 5:21Speaker 1: That's a really clever workflow. Combining l's checksums and come
- 5:24Speaker 1: to solve a complex problem efficiently. Okay, let's switch gears
- 5:28Speaker 1: to the security side. Permissions. Everyone's seen that output from LSAL,
- 5:33Speaker 1: that string of nine characters like rw xr xr right.
- 5:36Speaker 2: Those nine characters are the core of file security. They
- 5:40Speaker 2: dictate who can do what. They're split into three sets.
- 5:42Speaker 1: Of three user group others.
- 5:44Speaker 2: User that's the owner group, a defined set of users,
- 5:47Speaker 2: and others everyone else. And the letters are simple R
- 5:50Speaker 2: for read, W for write, X for execute.
- 5:53Speaker 1: And we change these using CHAMAD. There's the symbolic way,
- 5:57Speaker 1: which feels intuitive like chamud G plus w gives the
- 6:01Speaker 1: group right permission. But there's also the faster way, the
- 6:04Speaker 1: octal numeric method.
- 6:05Speaker 2: Yes, the numbers. It seems cryptic at first, but it's
- 6:08Speaker 2: very efficient. Each permission gets a number. Read is four
- 6:11Speaker 2: right is to execute.
- 6:12Speaker 1: As one because four to two one add up nicely exactly.
- 6:15Speaker 2: They correspond to the bits one hundred and four zero
- 6:17Speaker 2: centi zero zero one one. So if you want read
- 6:19Speaker 2: and write r W, that's four plus two will six
- 6:22Speaker 2: full permissions. R wx is four plus two plus one
- 6:24Speaker 2: will go seven. So if you see cham at six
- 6:26Speaker 2: four to four, file dot txt. That means user gets
- 6:29Speaker 2: six RW, group gets four R, and others get four R.
- 6:33Speaker 2: Quick and precise.
- 6:34Speaker 1: Okay, But here's the tricky part. Those permissions are WX
- 6:39Speaker 1: means slightly different things for a directory compared to a
- 6:41Speaker 1: regular file, don't.
- 6:42Speaker 2: They They do, and this catches people out all the time.
- 6:44Speaker 2: For a directory, execute X is maybe the most important.
- 6:47Speaker 2: It means you can access things inside the directory or
- 6:50Speaker 2: traverse it. Without X, you can't see d into it
- 6:53Speaker 2: or access files within it, even if you can see
- 6:55Speaker 2: it exists, even if you.
- 6:56Speaker 1: Have read permission on the directory.
- 6:58Speaker 2: Even then, read R on a director lets you list
- 7:00Speaker 2: its contents, right W lets you create or delete files
- 7:04Speaker 2: inside that directory. But X is the key to get in.
- 7:07Speaker 1: That makes sense and sticky With directory security, there's the
- 7:10Speaker 1: sticky bit. Sounds old, but it's actually super relevant for
- 7:13Speaker 1: shared spaces.
- 7:14Speaker 2: It really is. The stickybit is a special permission, usually
- 7:18Speaker 2: shown as a T in the last position the others
- 7:21Speaker 2: execute slot. Think of the team directory, which everyone can.
- 7:24Speaker 1: Write to, right, total chaos if anyone could delete anyone
- 7:26Speaker 1: else's temporary files exactly.
- 7:28Speaker 2: The sticky bit prevents that. When it's set on a directory,
- 7:32Speaker 2: chu mod plus T even if you have right permission
- 7:35Speaker 2: to that directory, you can only delete files that you
- 7:37Speaker 2: own or create it.
- 7:39Speaker 1: Ah, so it stops users messing with each other's stuff
- 7:42Speaker 1: in shared writable folders like TPIP clever.
- 7:45Speaker 2: It's a neat piece of system logic overriding standard permissions
- 7:49Speaker 2: for a specific purpose. We should also quickly mention chound
- 7:52Speaker 2: for changing the owner and group.
- 7:54Speaker 1: Right chound user dot group file name, and.
- 7:57Speaker 2: Also the set UID permission. That's a big one for security.
- 8:00Speaker 1: D or SUID. That's when an executable file runs not
- 8:04Speaker 1: as the user who launched it, but as a user
- 8:06Speaker 1: who owns the file.
- 8:07Speaker 2: Correct you see it as an S instead of an
- 8:09Speaker 2: X in the user's execute permission slot. If a program
- 8:13Speaker 2: needs root privileges to do something specific like changing a
- 8:16Speaker 2: password with password, but a normal user needs to run it,
- 8:20Speaker 2: you make the password program owned by root and set
- 8:23Speaker 2: the SUID bit. Then when a normal user runs it,
- 8:26Speaker 2: that one process runs is root, performs its tasks, and
- 8:29Speaker 2: then exits.
- 8:30Speaker 1: Necessary for some things, but obviously big target if that
- 8:33Speaker 1: program has a vulnerability.
- 8:35Speaker 2: Huge security implications. Yes, needs careful handling.
- 8:38Speaker 1: Okay, now for the ultimate lockdown. What if standard permissions
- 8:42Speaker 1: even root access aren't enough. What if you need to
- 8:45Speaker 1: make file truly untouchable.
- 8:46Speaker 2: Then you need to go beyond standard permissions and look
- 8:48Speaker 2: at extended attributes, specifically the chat tommand which works on
- 8:52Speaker 2: filesystems like x to four chat and the key attribute
- 8:56Speaker 2: is immutability. Yes, the immutability attribute set with chat plus.
- 9:00Speaker 1: I filename And what does plus i actually do?
- 9:02Speaker 2: It makes the file immutable, unchangeable. It cannot be deleted, renamed, modified,
- 9:06Speaker 2: or even linked to by anyone, not even the root user,
- 9:09Speaker 2: not even wow, not until someone explicitly removes the attribute
- 9:14Speaker 2: with chatcard a filename. It's like setting the file in
- 9:16Speaker 2: digital concrete.
- 9:18Speaker 1: I can immediately see a use case like upcresol dot
- 9:22Speaker 1: cof the DNS settings. Sometimes network managers or ISP connections
- 9:27Speaker 1: try to overwrite your custom settings.
- 9:28Speaker 2: There perfect example, if you manually set up your DNS
- 9:32Speaker 2: servers in acresol dot com and want to make sure
- 9:34Speaker 2: nothing overwrites it, you set the immutable flag with chathar
- 9:38Speaker 2: plus ixkrezolv dot com. Problem solved. No script, no update,
- 9:44Speaker 2: not even you as root can change it without first
- 9:46Speaker 2: removing the flag.
- 9:47Speaker 1: That's serious protection against configuration drift.
- 9:50Speaker 2: It's about the highest level of file protection the filesystem
- 9:52Speaker 2: itself offers.
- 9:53Speaker 1: Okay, let's pivot a bit. We saw how devices can
- 9:56Speaker 1: look like files like dev zero. Can we go the
- 9:58Speaker 1: other way, make a file ACCLI physical disc. We're talking
- 10:01Speaker 1: loopback filesystems absolutely.
- 10:03Speaker 2: A loopback filesystem is basically just a regular file, often
- 10:06Speaker 2: quite large, that contains an entire formatted filesystem inside it.
- 10:10Speaker 2: You can then mount this file and the system treats
- 10:12Speaker 2: it just like it would treat a partition on a
- 10:14Speaker 2: hard drive or a USB stick.
- 10:16Speaker 1: So cool. Great for testing filesystems, or maybe creating encrypted container.
- 10:19Speaker 2: Exactly, or working with disc images without needing actual spare
- 10:23Speaker 2: physical discs. It neatly combines tools we've already discussed.
- 10:26Speaker 1: So how do you set one up? It sounds like
- 10:28Speaker 1: a few steps.
- 10:28Speaker 2: It is pretty straightforward, though, three main steps. First, you
- 10:32Speaker 2: need the container file itself. Use dd and dev zero
- 10:36Speaker 2: to create a large empty file. Let's say DDF dev
- 10:39Speaker 2: zero of my virtualdisc dot mmg bs one m count
- 10:44Speaker 2: twenty forty eight for a two GB file.
- 10:46Speaker 1: Okay, so now we have a big file full of zeros.
- 10:48Speaker 2: Step two, format it just like you'd format a real drive.
- 10:52Speaker 2: Use mkfs dot st four My virtual disk, dot, MG
- 10:55Speaker 2: or whatever filesystem you want. This writes the filesystem structure
- 10:58Speaker 2: inside that file.
- 10:59Speaker 1: So now it's not just zeros. It's a formatted, albeit
- 11:01Speaker 1: empty XT four filesystem living inside a file precisely.
- 11:04Speaker 2: And step three mount it. You need a mount point,
- 11:07Speaker 2: just a directory, say mkdd MN toolbuck then use them
- 11:10Speaker 2: at command, but with a special option mount tall loop
- 11:13Speaker 2: my virtualdisc dot IMMG m NTA loop back.
- 11:16Speaker 1: Ah. The io loop option that's the magic part.
- 11:19Speaker 2: That's the key. It tells the kernel treat this file
- 11:22Speaker 2: not as a file but as a block device and
- 11:24Speaker 2: boomt loopback. Now acts like a mounted two GBXT four partition.
- 11:28Speaker 2: You can CD into it, create files, set permissions, everything.
- 11:32Speaker 1: That's like virtualization at the filesystem level without needing a
- 11:35Speaker 1: full virtual machine exactly.
- 11:37Speaker 2: It's incredibly flexible. And for more complex things like disk
- 11:40Speaker 2: images that already contain multiple partitions inside them, you can
- 11:44Speaker 2: use tools like loose up or KParts to map those
- 11:47Speaker 2: internal partitions as separate loop devices too.
- 11:49Speaker 1: Wow. Okay, that's powerful stuff. So we've covered creating files,
- 11:53Speaker 1: analyzing them, securing them, even making files act like discs.
- 11:57Speaker 1: Let's wrap up with some quick hits for efficiency navigating
- 12:01Speaker 1: the command line, especially deep directory trees, speed tricks.
- 12:05Speaker 2: Right navigating efficiently. This is where the directory stack comes
- 12:08Speaker 2: in handy. The commands are pushed and popped.
- 12:10Speaker 1: Push and pop sounds like stack operations and programming.
- 12:13Speaker 2: It is. It uses a lifeless stack last in first out.
- 12:17Speaker 2: Let's say you're working deep in some project source code,
- 12:19Speaker 2: maybe home, user, projects, school, aff suck, Modulia, but you
- 12:22Speaker 2: constantly need to jump over to a configuration directory like
- 12:25Speaker 2: at psychoolapcom, dot d instead of typing or pasting that
- 12:29Speaker 2: long path.
- 12:30Speaker 1: Every time you use pushed it cyclelebc exactly.
- 12:33Speaker 2: Push does two things. It changes your directory to at aklebcom,
- 12:36Speaker 2: dot dot d, and it pushes your old directory home
- 12:40Speaker 2: dot modulia onto the top of this invisible stack.
- 12:43Speaker 1: Okay, so now I'm the configured directory, I do my work.
- 12:46Speaker 1: How do I get back instantly?
- 12:48Speaker 2: Just type popped. It takes the top directory off the
- 12:51Speaker 2: stack home, dot modulia and instantly changes your working directory back.
- 12:55Speaker 2: There no typing paths. You can push multiple directories onto
- 12:58Speaker 2: the stack and pop back through them. It's a massive timesaver.
- 13:01Speaker 2: If you jump between several locations frequently, I.
- 13:03Speaker 1: Need to start using that more. Okay, final quick tip
- 13:06Speaker 1: dealing with huge files, especially log files. CAT just floods
- 13:10Speaker 1: the screen.
- 13:11Speaker 2: Right, use head to see the beginning, tail to see
- 13:13Speaker 2: the end. But the real gem for system monitoring is
- 13:16Speaker 2: tail DAF the TAILF for follow exactly tail dash file
- 13:20Speaker 2: name doesn't just show the last ten lines and exit.
- 13:23Speaker 2: It sits there, watching the file as new lines get
- 13:26Speaker 2: added to the end of the file. Like log entries
- 13:28Speaker 2: appearing tyl, lash F prints them to your screen in
- 13:31Speaker 2: real time.
- 13:32Speaker 1: Essential for watching server logs or debugging output as it happens.
- 13:35Speaker 2: Absolutely essential. It keeps running until the process writing to
- 13:38Speaker 2: the file stops or you hit cyrol plus C to
- 13:41Speaker 2: stop it yourself.
- 13:42Speaker 1: Okay, what a journey from the raw power of DD
- 13:46Speaker 1: and the logic of calm, through locking things down with
- 13:49Speaker 1: chatter and the sticky bit making files act like discs
- 13:52Speaker 1: with loop back, and finally speeding up navigation with pushed.
- 13:56Speaker 1: We've really peaked under the hood.
- 13:57Speaker 2: Yeah, and the key takeaway isn't just memorizing command, it's
- 14:00Speaker 2: getting that feel for the underlying concepts files as data streams,
- 14:04Speaker 2: checksums for integrity, permissions as bitflags, extended attributes for deeper control.
- 14:10Speaker 2: Understanding that logic is what leads to real mastery.
- 14:12Speaker 1: Absolutely and to help you put this into practice right away,
- 14:15Speaker 1: here's a question to think about. Imagine you're trying to
- 14:18Speaker 1: solve a really tricky network problem. You suspect something is
- 14:21Speaker 1: messing with your echost's file, which maps host names to
- 14:25Speaker 1: IP addresses locally. You need to make absolutely sure this
- 14:29Speaker 1: file cannot be changed by anything, not a system update,
- 14:33Speaker 1: not a script, nothing. While you investigate, which chat attribute
- 14:37Speaker 1: would you use and why is it more effective in
- 14:39Speaker 1: this scenario than just using standard TRON permissions to make
- 14:42Speaker 1: the file read only.
- 14:44Speaker 2: Mull that one over. Think about the difference between whose
- 14:46Speaker 2: standard permissions apply to versus the power of that extended attribute.
- 14:51Speaker 2: Try plying with pushed and popped on your own system too.
- 14:53Speaker 2: Actually using these tools is the best way to make
- 14:55Speaker 2: them stick.
- 14:56Speaker 1: Great advice. Thanks for diving deep with us today. We'll
- 14:58Speaker 1: catch in the next one.