Operations 17 min read

What Linux Commands Should You Master for Java Interview Troubleshooting?

This article categorizes essential Linux commands for Java developers, explains why interviewers focus on real‑world usage, provides scene‑based command tables, demonstrates log‑analysis and performance‑monitoring workflows, and offers concise answers to common interview follow‑up questions.

Java Architect Handbook
Java Architect Handbook
Java Architect Handbook
What Linux Commands Should You Master for Java Interview Troubleshooting?

Interview Focus Points

Practical Development Experience – Interviewers expect actual Linux development, deployment, and online troubleshooting. Typical starter commands are ls, cd, pwd; advanced commands include tail -f, grep -A 20, jstack, netstat -anp.

Problem‑Tracing Ability – Scenarios such as CPU spikes, memory leaks, disk full, or port conflicts require quick location of the cause with the right commands.

Breadth of Knowledge – Beyond basics, mastery of the text‑processing three musketeers ( grep / awk / sed), performance‑monitoring tools, and Java diagnostic utilities demonstrates technical depth.

Core Answer – Scene‑Based Command Classification

I group the most frequently used Linux commands by troubleshooting scenario, making them easier to remember and to present in an interview.

Linux 常用命令按场景排查示意图
Linux 常用命令按场景排查示意图

1. File & Directory Operations (High Frequency)

ls

– List directory contents. Example: ls -lh (human‑readable size), ls -lt (sort by modification time). cd – Change directory. Example: cd - (return to previous directory), cd ~ (home). pwd – Show current path. cp – Copy files/directories. Example: cp -r dir1 dir2 (recursive copy). mv – Move or rename. Example: mv old.txt new.txt. rm – Delete. Example: rm -rf dir (use with extreme caution in production) . mkdir – Create directories. Example: mkdir -p a/b/c (recursive). find – Search files. Example: find / -name "*.log" -mtime +7. tree – Tree view of directories. Example: tree -L 2 (show two levels).

2. File Viewing & Editing (Log‑Reading Essentials)

cat

– Display whole file. less – Paginated view; less +F file works like tail -f but allows upward scrolling. head / tail – View file head or tail. Example: tail -f app.log, tail -n 100 app.log, head -n 50 file. echo – Output text or variable values, e.g., echo $JAVA_HOME. vi / vim – Edit files, useful for quick config tweaks during troubleshooting.

When reading logs I always start with tail -f xxx.log combined with grep to filter the key information.

3. Text‑Processing Three Musketeers (Core Troubleshooting Weapons)

grep – Text Search

# Show matching line plus the next 20 lines (useful for stack traces)
grep -A 20 "NullPointerException" app.log

# Recursively search Java files for a class definition
grep -rn "public class" --include="*.java" .

# Invert match and ignore case
grep -vi "debug" app.log

# Count matching lines
grep -c "ERROR" app.log

awk – Column‑wise Text Analysis

# Show top 5 CPU‑consuming processes
ps aux | sort -rnk 3 | head -5

# Count occurrences of each IP in an access log
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head

# Split by ':' and print column 1 and 3 (e.g., /etc/passwd)
awk -F ':' '{print $1, $3}' /etc/passwd

sed – In‑place Text Editing

# Replace "old" with "new" in a file (in‑place)
sed -i 's/old/new/g' file.txt

# Show lines 10‑20 only
sed -n '10,20p' file.txt

# Delete empty lines
sed '/^$/d' file.txt

4. Process & Port Management (Online Diagnosis Essentials)

top

/ htop – Real‑time system resource view; top -1 shows each CPU core. ps aux or ps -ef – List processes; pipe to grep java to find Java processes. jps – JDK‑provided tool that lists only Java processes. netstat -anp | grep 8080, netstat -tunlp, ss -tunlp, lsof -i:8080 – Check which process occupies a port. kill -9 12345 – Force kill (SIGKILL). killall java – Kill by name (use cautiously). nohup java -jar app.jar > /dev/null 2>&1 & – Run Java app in background.

5. Java Diagnostic Tools (JDK‑Provided, Bonus Points)

jps

– List Java processes (simpler than ps for Java). jstack – Print thread stacks; useful for CPU spikes or deadlock analysis. jmap – Dump memory snapshot; aids OOM or memory‑leak investigation. jstat – Show GC statistics; monitor garbage‑collection frequency. jinfo – View or modify JVM parameters on a running process. jhat – Analyze heap dumps together with jmap. arthas – Alibaba’s open‑source diagnostic tool; strongly recommended for online troubleshooting.

CPU‑Spike Diagnosis Example

# 1. Find the Java process with high CPU
top

# 2. Identify the hot thread (convert PID to hex)
top -Hp 12345

# 3. Convert decimal thread ID to hex (e.g., 12367 → 304f)
printf "%x
" 12367

# 4. Print stack of that thread and grep for the hex ID
jstack 12345 | grep -A 30 "304f"

This workflow is what interviewers love to hear; stating it clearly demonstrates solid practical experience.

6. System Performance Monitoring

free -h

– Show memory usage (human‑readable). df -h – Disk usage. du -sh /var/log – Size of a specific directory; du -sh * – Size of all sub‑directories. uptime – System load averages (1, 5, 15 minutes). iostat -x 1 – Disk I/O per second. vmstat 1 – Virtual memory stats per second (focus on r, si, so). dmesg | tail -50 – Recent kernel messages.

7. Network‑Related Commands

ping www.baidu.com

– Test connectivity. telnet 192.168.1.100 8080 – Test port reachability. curl http://localhost:8080/api/user – Simple GET request.

curl -X POST -H "Content-Type: application/json" -d '{"name":"tom"}' http://localhost:8080/api/user

– POST with JSON. wget https://example.com/file.tar.gz – Download file. nslookup www.baidu.com – DNS lookup. ifconfig (legacy) / ip addr (modern) – Show network interfaces.

8. Permissions & Users

chmod 755 script.sh

– Set rwxr-xr-x. chmod +x deploy.sh – Add execute permission. chown user:group file – Change owner. su - deploy – Switch to another user. sudo command – Execute with root privileges.

9. Compression & Extraction

tar -zcvf app.tar.gz app/

– Create gzip tarball. tar -zxvf app.tar.gz – Extract. tar -zxvf app.tar.gz -C /opt – Extract to specific directory. zip -r app.zip app/ / unzip app.zip – Zip utilities.

10. Other High‑Frequency Commands

systemctl status nginx

, systemctl start nginx, systemctl enable nginx – Service management (CentOS 7+). crontab -e, crontab -l – Edit and list scheduled jobs; example: 0 2 * * * /home/backup.sh (daily 2 AM backup). history | grep "mysql" – Search command history. date / date "+%Y-%m-%d %H:%M:%S" – Show current time.

Pipe & redirection examples: cat app.log | grep "ERROR" | wc -l, echo "hello" > file.txt (overwrite), echo "hello" >> file.txt (append), command > /dev/null 2>&1 (discard output).

High‑Frequency Follow‑Up Questions

CPU spike diagnosis? Answer flow: top → find PID → top -Hp PID → locate hot thread → jstack PID → grep thread ID → locate offending code.

How to check which process occupies a port? At least two methods: netstat -anp | grep PORT, lsof -i:PORT, or ss -tunlp | grep PORT.

Difference between grep , awk , sed ? grep – text search; awk – column‑wise analysis; sed – in‑place text editing/replacement.

Accidentally ran rm -rf ? If backups exist, tools like extundelete may help, but usually recovery is impossible. In production, disable rm -rf / and prefer moving files to /tmp or using trash‑cli instead of direct deletion.

Difference between kill -9 and kill -15 ? kill -15 (SIGTERM) requests graceful shutdown, allowing hooks and resource cleanup; kill -9 (SIGKILL) forces immediate termination and may cause data loss. For Java apps, prefer kill -15 to let the JVM close gracefully.

Memory Mnemonics (Scene‑Based Recall)

Log viewing: tail -f + grep Process inspection: ps aux + top Port checking: netstat + lsof Disk status: df -h + du -sh Memory status: free -h + top Java issue tracing: jps + jstack +

jmap

Conclusion

Organize answers by scenario – e.g., topjstackgrep – to demonstrate real‑world troubleshooting depth. Mention modern tools such as arthas for extra credit.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Javadevopsperformance-monitoringLinuxtroubleshootinginterviewcommand-line
Java Architect Handbook
Written by

Java Architect Handbook

Focused on Java interview questions and practical article sharing, covering algorithms, databases, Spring Boot, microservices, high concurrency, JVM, Docker containers, and ELK-related knowledge. Looking forward to progressing together with you.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.