Skip to Content

cut Command in Linux

The cut command in Linux is a handy tool used to pull out specific parts of text from each line in a file or command output. Think of it like using scissors to cut out certain columns or fields of data, making it easier to focus on just the information you need.  


cut tutorial for DevOps 

The cut command is an essential tool in a DevOps engineer's . It allows you to extract specific data from a stream or file, making it invaluable for log analysis, system monitoring, and automation tasks. Whether you're handling disk usage data, CPU stats, or network information, cut can help you parse and filter out exactly what you need.


What is the cut Command?

The cut command extracts specific sections (or fields) from a file or input stream. It’s commonly used in conjunction with other commands to filter, process, and format data in a scriptable manner.

Basic Syntax of cut command :

cut [options] [file...]


Key Options of the cut Command

Option

Description
-fSelect the fields (columns) to extract.
-dSpecify the delimiter that separates fields.
-cExtract specific characters from each line.
-bExtract specific bytes (instead of characters or fields).
-sSuppress lines that do not contain the delimiter.
--output-delimiterSet a custom delimiter for the output (e.g., comma for CSVs).


Example of cut Command

Let's now look at some real-world examples where the cut command is useful for scripting and monitoring systems.

1. Extracting Disk Usage Information with df -h

The df -h command provides a human-readable summary of disk space usage, but often the output has more data than you need. You can use cut to extract only the relevant information.

Running df -h

df -h

Output :

Filesystem Size Used Avail Use% Mounted on /dev/sda1 50G 30G 15G 70% / /dev/sdb1 100G 50G 45G 55% /data

This shows the totalused, and available disk space, along with the mount point.

Case a : Extract Filesystem and Usage from df -h

df -h | cut -d ' ' -f 1,5

🔹 Explanation:

  • df -h shows disk usage in a human-readable format.
  • -d ' ' specifies space as the delimiter.
  • -f 1,5 extracts the Filesystem (column 1) and Usage percentage (column 5).

Output (case 1) :

Filesystem  Use%
/dev/sda1   85%
/dev/sdb1   55%

Case b : Extract Only the Disk Usage Percentage

df -h | grep '/dev/sda1' | cut -d ' ' -f 5

🔹 Explanation:

  • grep '/dev/sda1' filters the output to only show /dev/sda1.
  • cut -d ' ' -f 5 extracts just the usage percentage of /dev/sda1.

Output:

85%

This is a great way to monitor disk space and trigger alerts in a script when the disk usage exceeds a threshold.

2. Extracting CPU Usage with top Command

The top command provides dynamic real-time information about running processes and system stats, including CPU and memory usage. However, sometimes you need only specific fields for monitoring or alerting.

Running top in Batch Mode

top -bn1 | grep "Cpu(s)"

Output:

%Cpu(s): 6.2 us, 2.1 sy, 0.0 ni, 91.5 id, 0.2 wa, 0.0 hi, 0.0 si, 0.0 st

This shows the CPU usage breakdown, where :

  • us = user space
  • sy = system space
  • id = idle time

Case c : Extract CPU Usage from top (every 1 second)​

top -bn1 | grep "Cpu(s)" | cut -d ' ' -f 2

🔹 Explanation:

  • top -bn1 runs top in batch mode for 1 iteration.
  • grep "Cpu(s)" filters out lines containing CPU usage stats.
  • cut -d ' ' -f 2 extracts the CPU usage percentage.

Output :

6.2

This is useful for monitoring CPU load in a script, where you might trigger an action (like scaling) if CPU usage exceeds a certain threshold.

3. Extracting Memory Usage Information from free Command

The free command provides details about memory usage on your system. With cut, you can extract specific data like used memoryavailable memory, etc.

Running free -h

free -h

Output:


total used free shared buff/cache available Mem: 16Gi 8.5Gi 2.1Gi 1.0Gi 5.4Gi 6.5Gi Swap: 2.0Gi 0.0Gi 2.0Gi

This gives detailed information about the total memoryused memoryfree memory, and swap usage.

Case d : Extract Total and Used Memory from free -h

free -h | grep Mem | cut -d ' ' -f 2,3

🔹 Explanation:

  • free -h outputs memory stats in human-readable format.
  • grep Mem filters out the line containing memory usage stats.
  • cut -d ' ' -f 2,3 extracts the total and used memory columns.

Output:

16Gi 8.5Gi

You can incorporate this into a script that sends alerts if the available memory falls below a certain threshold.

4. Monitoring Network Usage with netstat

The netstat command is widely used for displaying network connections, routing tables, and interface statistics. By using cut, you can filter out specific pieces of data from the output.

Running netstat -tuln

netstat -tuln

Output:

Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp6 0 0 :::80 :::* LISTEN tcp6 0 0 :::443 :::* LISTEN

This shows active TCP/UDP ports and their states, with -t filtering for TCP connections, -u for UDP connections, -l for listening ports, and -n to show numerical addresses instead of resolving hostnames.

Case e : Extract Active TCP Connections from netstat

netstat -tuln | cut -d ' ' -f 1,4

🔹 Explanation:

  • netstat -tuln shows active listening ports and TCP connections.
  • cut -d ' ' -f 1,4 extracts the protocol (TCP/UDP) and the local address (IP + port).

Output:

Proto Local Address
tcp   0.0.0.0:22
tcp6  :::80
tcp6  :::443

This is useful for monitoring open ports or active services on a server.

5. Extracting Process Information with ps Command

The ps command shows the status of running processes. DevOps engineers can use ps to monitor system processes and manage resource allocation.

Running ps aux to Show Processes

ps aux --sort=-%cpu | head -n 10

Output:

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1234 25.3 1.2 123456 5678 ? S 14:23 2:50 /usr/bin/python3 www-data 2345 15.2 0.8 98765 4321 ? S 14:25 1:30 nginx: worker process root 3456 10.1 0.5 54321 1234 ? S 14:26 0:50 /usr/bin/mysql

This shows the top 10 processes sorted by CPU usage.

Case f : Extract the PID and Command of Processes with High CPU Usage

ps aux --sort=-%cpu | head -n 10 | cut -d ' ' -f 1,11

🔹 Explanation:

  • ps aux --sort=-%cpu lists processes sorted by CPU usage in descending order.
  • head -n 10 limits the output to the top 10 processes.
  • cut -d ' ' -f 1,11 extracts the user and command columns.

Output:

USER       COMMAND
root       /usr/bin/python3
www-data   nginx: worker process
root       /usr/bin/mysql

This is particularly helpful in performance monitoring scripts that track resource-hungry processes.


Using cut command in Automation Scripts

cut command is used in automated scripts to gather and process data for reporting, alerting, or scaling decisions.

Here’s an example of using cut in a disk usage alerting script:

Disk Usage Alert Script

#!/bin/bash

# Check disk usage
disk_usage=$(df -h | grep '/dev/sda1' | cut -d ' ' -f 5)

# Check if usage is above 80%
if [[ ${disk_usage%?} -gt 80 ]]; then
  echo "Warning: Disk space usage is above 80%. Current usage: $disk_usage"
fi

🔹 Explanation:

  • This script checks the disk usage of /dev/sda1 and extracts the usage percentage.
  • If the usage is above 80%, it sends a warning.

In summary, the cut command is a versatile and fundamental tool for extracting specific data from files and command outputs in Linux.


Back

Check out these tutorials :