How to Parse CSV Data in Bash

A Comma Separated Values file is a text file that holds tabulated data. CSV is a type of delimited data. As the name suggests, a comma “,” is used to separate each field of data—or value—from its neighbors.

CSV is everywhere. If an application has import and export functions, it’ll almost always support CSV. CSV files are human-readable. You can look inside them with less, open them in any text editor, and move them from program to program. For example, you can export the data from an SQLite database and open it in LibreOffice Calc.

However, even CSV can become complicated. Want to have a comma in a data field? That field needs to have quotation marks “"” wrapped around it. To include quotation marks in a field each quotation mark needs to be entered twice.

Of course, if you are working with CSV generated by a program or script that you have written, the CSV format is likely to be simple and straightforward. If you’re forced to work with more complex CSV formats, with Linux being Linux, there are solutions we can use for that too.

[mai mult...]

How to List All Users In a Group on Linux

Files and directories on Linux have a set of permissions for the owner, another set for the group the file is allocated to, and permissions for everyone who isn’t in one of the previous two categories. Each set of permissions defines whether the members of that category can read, write, or execute the file. In the case of a directory, the execute action equates to being able to cd into the directory.

The default group for a file or directory is the default group of the owner. That’s usually the person who created it. The group permissions are used to allow a collection of users to have controlled access to the files and directories of the other members of that group.

[mai mult...]

How to Calculate Subnet Masks on Linux With ipcalc

Subnetting is a way to break a large network into smaller, connected pieces. Each piece is called a subnet. You might choose to organize your network so that your sales team uses one subnet, HR use another subnet, customer support use yet another subnet, and so on.

There are significant benefits to this. The first has to do with security and control. Without subnetting, everything is one big “flat” network. With subnetting, you can decide which subnets can talk to other subnets. Different subnets have different IP address ranges and use different subnet masks, which we’ll talk about in a moment.

Your router must be configured to allow traffic from one subnet to reach another subnet. And, because the router is a managed device, that gives you control over the type of traffic and interaction that is allowed between different subnets.

Subnetting can also prevent unauthorized users and malware from roaming through your network unchecked. Or at the very least, it’ll slow them down. Think of it like a submarine. If you get a hull breach in one section, you can close bulkhead doors so the rest of the vessel doesn’t get flooded. Subnets are like those bulkhead doors.

Often, there are performance benefits purely from the act of subnetting a large network. If your network is big enough and busy enough, that performance increase will come from the reduction of network traffic inside each subnet. The drop in ARP traffic alone might make things seem more responsive.

And of course, once your network is compartmentalized, it’s easier for your IT staff to understand, maintain, and support your infrastructure.

IP Addresses and Subnet Masks

That all sounds great, and it is. But it means we need to be very particular in our IP addressing. We need to use part of the IP address for the network ID, and part of the IP address for the device addressing. With subnets, we also need to use part of the IP address for the subnet.

IPv4 IP addresses use four three-digit numbers separated by periods. It’s called dot-decimal notation. The range of these numbers is 0 to 255. The first two numbers are the network ID. The third is used to hold the subnet ID, and the fourth number is used to hold the device address. That’s in simple cases.

Numbers are represented inside computers as sequences of binary values. If there are so few devices in the subnet that there are unused high bits in the device address number range, these “spare” binary bits can be used by the subnet ID.

How does the router or any other network device know what the composition of the IP address is? What indicates whether the subnet ID is wholly contained in the third number or if it poaches some of the high bits of the fourth number? The answer to that is the subnet mask.

The subnet mask looks like an IP address. It is four three-digit numbers, and the range of the numbers is from 0 to 255. But they really need to be thought about in their binary form.

Every binary bit that is a 1 in the subnet mask means the corresponding bit in the IP address refers to the network ID or subnet ID. Everything that is a zero in the subnet mask means the corresponding bit in the IP address refers to a device address.

Let’s take a typical IP address and apply a subnet mask to it. The subnet mask has 255 for each of the first three numbers, and 0 for the fourth.

  • IP address: 192.168.1.0
  • Subnet mask: 255.255.255.0 = 11111111.11111111.11111111.00000000

In binary 255 is 11111111. If the subnet mask bits are set to one, the corresponding bits in the IP address refer to the network ID and subnet ID. 255 in the subnet mask means all of the bits in the corresponding number in the IP address refer to the network ID or subnet ID.

The fourth number is zero, meaning no bits are set to one. So that number refers to the network device addresses. So our subnet mask of 255.255.255.0 means the first three numbers of the IP address hold the network ID and subnet ID, and the last number is reserved for network device addresses.

That means that a side effect of all this is that the subnet mask also determines how many bits in the IP address can be used to identify individual devices. In other words, the subnet mask determines which bits in the IP address identify the subnet and how many devices that subnet can contain.

Altering the subnet mask has a dramatic effect on the network. That’s why we need to get it right.

[mai mult...]

How to Rename a Directory on Linux

Your Data Is Safe

Renaming directories is something we all need to do from time to time.

We might create a directory and misspell its name, and we want to put it right. Often, the purpose of a directory changes over time or through the life of a project, and you want to adjust the name to reflect its new use. Perhaps you’ve decompressed an archive file and it’s created a directory tree with the directory names in uppercase and you’d like them in lowercase. Whatever the reason. renaming a directory doesn’t do anything to the data held inside it. It changes the path to that data, but the files and directories inside your renamed directory aren’t touched.

Don’t rename system directories. Changing the path to system files and commands is going to have a detrimental effect on the running of your computer, to say the least. If you need to use sudo to rename a directory—unless you really know what you’re doing—the chances are you shouldn’t be renaming it.

[mai mult...]

How to Use Linux Signals in Bash Scripts

Signals are short, fast, one-way messages sent to processes such as scripts, programs, and daemons. They let the process know about something that has happened. The user may have hit Ctrl+C, or the application may have tried to write to memory it doesn’t have access to.

If the author of the process has anticipated that a certain signal might be sent to it, they can write a routine into the program or script to handle that signal. Such a routine is called a signal handler. It catches or traps the signal, and performs some action in response to it.

[mai mult...]

How to Check Your BIOS Version and Update it

Multiple Windows computer on a desk

You probably shouldn’t update your BIOS, but sometimes you need to. Here’s how to check what BIOS version your computer is using and flash that new BIOS version onto your motherboard as quickly and safely as possible. Be very careful when updating your motherboard’s BIOS! If your computer freezes, crashes, or loses power during the process, the BIOS or UEFI firmware may be corrupted.

[mai mult...]

How to Traverse a Directory Tree on Linux

Directories on Linux let you group files in distinct, separate collections. The downside is it becomes tedious moving from directory to directory to perform a repetitive task. Here’s how to automate that.

The first command you learn when you’re introduced to Linux is probably ls, but cd won’t be far behind it. Understanding directories and how to move around them, particularly nested subdirectories, is a fundamental part of understanding how Linux organizes itself, and how you can organize your own work into files, directories, and subdirectories.

Grasping the concept of a tree of directories—and how to move between them—is one of the many little milestones you pass as you familiarize yourself with the landscape of Linux. Using cd with a path takes you to that directory. Shortcuts like cd ~ or cd on its own take you back to your home directory, and cd .. moves you up one level in the directory tree. Simple.

However, there isn’t an equally simple means of running a command in all directories of a directory tree. There are different ways we can achieve that functionality, but there isn’t a standard Linux command dedicated to that purpose.

Some commands, such as ls, have command-line options that force them to operate recursively, meaning they start in one directory and methodically work through the entire directory tree below that directory. For ls, it’s the -R (recursive) option.

If you need to use a command that doesn’t support recursion, you have to provide the recursive functionality yourself.

[mai mult...]