cut, xargs, watch, lsofThis guide explains powerful Linux commands for field extraction, batch execution, repeated monitoring, and file-process tracking.
cut — Extract Text by Columns or DelimitersExtract specific fields or character positions from text or files.
Syntax:
cut [options] [file]
Common options:
-d → Delimiter (default is TAB)-f → Field number(s)-c → Character position(s)Examples:
cut -d: -f1 /etc/passwd # Show usernames
cut -f2 data.csv # Show 2nd field (TAB-delimited)
cut -c1-5 file.txt # Show first 5 characters of each line
xargs — Build and Execute Command Lines from InputRead items from input and pass them as arguments to another command.
Syntax:
command | xargs [command]
Examples:
find . -name "*.log" | xargs rm # Delete all .log files
cat list.txt | xargs -n1 mkdir # Make directories from list
echo "file1 file2 file3" | xargs tar -cvf archive.tar
Useful options:
-n1 → One argument per command-d'\n' → Use newline as delimiterwatch — Repeatedly Run a CommandRun a command at fixed intervals and watch its output live.
Syntax:
watch [options] command
Examples:
watch df -h # Monitor disk usage
watch -n 5 'ls -l | grep ".log"' # Run every 5 seconds
watch -d free -m # Highlight changes in memory
Options:
-n <seconds> → Interval between updates-d → Highlight differenceslsof — List Open Files and Associated ProcessesView which files are opened by which processes (files, sockets, pipes, etc.).
Syntax:
lsof [options]
Examples:
lsof # Show all open files
lsof -i :80 # Show processes using port 80
lsof /var/log/syslog # Show who is using the file
lsof -u username # Show files opened by a user
| Command | Purpose | Example |
|---|---|---|
cut |
Extract specific fields or chars | cut -d: -f1 /etc/passwd |
xargs |
Run command on multiple inputs | cat list.txt \| xargs rm |
watch |
Re-run command periodically | watch df -h |
lsof |
Show open files and ports | lsof -i :22 |
cut, xargs, and lsof for powerful pipelines:lsof -i | cut -d" " -f1 | sort | uniq
xargs to process thousands of items efficiently.watch is excellent for debugging or live monitoring scripts and logs.