Operations 41 min read

50 Essential Linux Commands: Functions, Examples, and Practical Tips

This article compiles a concise reference of fifty commonly used Linux commands, detailing each command’s purpose, typical usage syntax, concrete examples, and handy tips to help developers and system administrators quickly master essential command‑line operations.

Programmer1970
Programmer1970
Programmer1970
50 Essential Linux Commands: Functions, Examples, and Practical Tips
Linux commands illustration
Linux commands illustration

1. netstat command (view network connection status)

Function: Displays all active network connections on the system, including TCP, UDP, and UNIX domain sockets.

Usage: netstat [options] Common options: -a show all connections and listening ports; -t show TCP connections; -u show UDP connections; -n display addresses and ports numerically; -p show PID and program name; -r show routing table; -s show network interface statistics; -l show only listening sockets; -c continuously refresh.

Example: netstat – show all connections; netstat -t – show only TCP connections.

Tip: Use netstat -an to display all connections with numeric addresses; netstat -p shows process IDs and names (may require root).

2. pgrep command (find process IDs by name)

Function: Finds the PID(s) of processes matching a given name.

Usage: pgrep [options] pattern Options: -l also display process name; -u limit to a specific user; -n show newest matching process; -o show oldest; -v invert match; -a show full command line; -f match against the full command line; -c output only the count.

Example: pgrep -l nginx – find processes named "nginx" and show name and PID; pgrep -u www-data – find processes run by user www-data.

Tip: pgrep -f searches the full command line; pgrep -l shows name and PID.

3. tar command (archive and compress files)

Function: Packages files and directories into an archive and optionally compresses it.

Usage: tar [options] filename Example: tar -cvf archive.tar file1 file2 – create archive.tar containing file1 and file2; tar -czvf archive.tar.gz dir – archive dir and compress with gzip.

Tip: tar -xvf extracts; tar -tf lists archive contents.

4. scp command (secure copy files)

Function: Copies files and directories between local and remote hosts.

Usage: scp [options] source destination Example: scp file.txt user@remote:/path/to/dir – copy file.txt to remote host; scp -r dir user@remote:/path/to/dir – copy directory recursively.

Tip: Use scp -P port to specify a non‑default SSH port; scp -q suppresses progress output.

5. netstat command (view network connections) – duplicate entry

Function: Shows all active network connections, including TCP, UDP, and UNIX sockets.

Usage: netstat [options] Example: netstat – display all connections; netstat -t – show only TCP.

Tip: netstat -a shows listening and non‑listening sockets; netstat -n displays numeric IPs and ports.

6. pgrep command (find process IDs by name) – duplicate entry

Function: Finds the PID(s) of processes matching a given name.

Usage: pgrep process_name Example: pgrep apache2 – find PIDs of "apache2"; pgrep -u user sshd – find sshd processes started by user.

Tip: pgrep -f searches the full command line; pgrep -l shows name and PID.

7. top command (monitor system processes)

Function: Continuously displays running processes and resource usage.

Usage: top Example: Run top directly.

Tip: Default sort is by CPU usage; Shift+M sorts by memory; top -u username shows only that user’s processes; -c shows full command line; -p PID monitors a specific PID; -H shows thread tree; -i ignores zombie processes.

8. free command (show memory usage)

Function: Displays total, used, and free memory.

Usage: free [options] Example: free – show memory in kilobytes; free -m – show in megabytes.

Tip: Remember to count cached and buffered memory as available; for more detail use cat /proc/meminfo.

9. df command (disk space usage)

Function: Shows filesystem disk space usage.

Usage: df [options] filesystem Example: df – all mounted filesystems; df -h – human‑readable format.

Tip: df -i shows inode usage; df -T shows filesystem type.

10. du command (disk usage of files/directories)

Function: Shows size of files and directories.

Usage: du [options] file/dir Example: du file.txt – size of a file; du -h /path/to/dir – human‑readable size of a directory.

Tip: Append a trailing slash to avoid counting symlinks or mount points; du -s shows total size only.

11. ps command (view process information)

Function: Displays information about running processes.

Usage: ps [options] Example: ps – processes of current user; ps -ef – full‑format list of all processes.

Tip: ps -u username shows processes of a specific user; ps -ef | grep process_name filters by name; ps aux shows all processes including system ones.

12. grep command (search file contents)

Function: Searches files for a given pattern.

Usage: grep [options] pattern file Example: grep "keyword" file.txt – lines containing "keyword"; grep -r "pattern" dir – recursive search.

Tip: grep -i ignores case; grep -v shows non‑matching lines.

13. awk command (text processing and reporting)

Function: Processes and analyzes text files line by line, can generate reports.

Usage: awk [options] 'script' file Example: awk '{print $1}' file.txt – print first field of each line; awk '3 >= 90 {print $1,$2}' file.txt – print first and second fields where third field ≥ 90.

Tip: awk -F sets field separator;

awk 'BEGIN{print "header"} {print $0} END{print "footer"}' file.txt

adds header/footer.

14. sed command (stream editor)

Function: Edits and transforms text streams.

Usage: sed [options] 'script' file Example: sed 's/old/new/g' file.txt – replace all "old" with "new"; sed '/pattern/d' file.txt – delete lines matching pattern.

Tip: sed -i edits files in place; sed -n prints only matched lines.

15. find command (search files and directories)

Function: Searches for files or directories matching criteria.

Usage: find [path] [options] [expression] Example: find /path/to/dir -name "*.txt" – find all .txt files; find /path/to/dir -type d -mtime +7 – directories modified more than 7 days ago.

Tip: find -exec runs a command on each match; find -print0 | xargs -0 safely handles spaces in names.

16. curl command (transfer data)

Function: Transfers data from servers, supports HTTP, HTTPS, FTP, etc.

Usage: curl [options] URL Example: curl https://www.example.com – fetch page; curl -o file.txt https://www.example.com/data.txt – download to file.

Tip: curl -H "header" adds custom header; curl -X POST -d "data" sends POST request.

17. crontab command (schedule recurring tasks)

Function: Sets or removes scheduled system tasks.

Usage: crontab [options] Example: crontab -e – edit current user’s crontab; crontab -l – list tasks.

Tip: crontab -r removes all tasks; crontab -u username views another user’s tasks.

18. sort command (sort lines of text)

Function: Sorts lines in a text file.

Usage: sort [options] file Example: sort file.txt – alphabetical sort; sort -rn file.txt – numeric reverse sort.

Tip: sort -u removes duplicate lines; sort -t sets field delimiter.

19. wc command (word, line, byte count)

Function: Counts lines, words, and characters in a file.

Usage: wc [options] file Example: wc file.txt – shows line, word, byte counts; cat file.txt | wc -l – count lines only.

Tip: wc -c counts bytes; wc -m counts characters (including multibyte).

20. kill command (terminate processes)

Function: Sends a signal to a process to terminate it.

Usage: kill [options] PID Example: kill 1234 – send default SIGTERM; kill -9 1234 – force kill.

Tip: killall terminates by process name.

21. ifconfig command (network interface configuration)

Function: Views and configures network interface information.

Usage: ifconfig [options] interface Example: ifconfig – show all interfaces; ifconfig eth0 up – enable eth0.

Tip: ifconfig -a shows all interfaces, including inactive; ifconfig eth0 down disables the interface.

22. ping command (test network connectivity)

Function: Sends ICMP echo requests to a target IP to test reachability.

Usage: ping [options] host Example: ping www.example.com; ping -c 5 192.168.0.1 – send 5 packets.

Tip: ping -i sets interval; ping -s sets packet size.

23. ssh command (secure remote login)

Function: Executes commands or logs into a remote host over an encrypted channel.

Usage: ssh [options] user@host Example: ssh [email protected]; ssh -p 2222 [email protected] – specify port.

Tip: ssh -X enables X11 forwarding; ssh-copy-id copies local public key for password‑less login.

24. wget command (non‑interactive download)

Function: Downloads files or webpages from a URL.

Usage: wget [options] URL Example: wget https://www.example.com/file.txt; wget -P /path/to/save https://www.example.com/file.txt.

Tip: wget -c continues interrupted downloads; wget -r recursively downloads a site.

25. vim command (text editor)

Function: Edits text files in the terminal.

Usage: vim [options] filename Example: vim file.txt – open file; vim -O2 file1.txt file2.txt – split window horizontally.

Tip: :w save; :q quit; :wq save and quit; i enters insert mode.

26. tail command (view file end)

Function: Shows the last few lines of a file.

Usage: tail [options] filename Example: tail file.txt – last 10 lines; tail -f file.txt – follow new additions.

Tip: tail -n sets number of lines; tail -F follows even if file is renamed.

27. cat command (concatenate and view files)

Function: Displays file contents.

Usage: cat [options] filename Example: cat file.txt; cat file1.txt file2.txt > file3.txt – concatenate.

Tip: cat -n numbers lines; cat -E shows $ at line ends.

28. more command (page through file)

Function: Paginates file output.

Usage: more [options] filename Example: more file.txt; ls -l | more.

Tip: Space advances a page, q quits, /keyword searches.

29. iostat command (disk I/O statistics)

Function: Shows I/O statistics for disks.

Usage: iostat [options] [interval] [count] Example: iostat – all disks; iostat -x sda – detailed stats for sda.

Tip: iostat -p shows per‑partition stats; iostat -d shows per‑device stats.

30. vmstat command (virtual memory statistics)

Function: Displays virtual memory and system activity.

Usage: vmstat [options] [interval] [count] Example: vmstat; vmstat 1 5 – five samples at 1‑second intervals.

Tip: vmstat -s shows detailed memory stats; vmstat -d shows disk I/O.

31. mpstat command (CPU performance monitoring)

Function: Shows performance statistics for multiple CPUs.

Usage: mpstat [options] [interval] [count] Example: mpstat; mpstat -P ALL – all CPUs.

Tip: mpstat -u shows CPU utilization; mpstat -I ALL shows interrupt stats.

32. pidstat command (process performance monitoring)

Function: Shows performance stats for a specific process or all processes.

Usage: pidstat [options] [interval] [count] Example: pidstat; pidstat -p 1234 – stats for PID 1234.

Tip: pidstat -d shows disk I/O; pidstat -r shows memory; pidstat -p ALL shows all processes.

33. ls command (list files and directories)

Function: Lists files and sub‑directories in the current directory.

Usage: ls [options] [file/dir] Example: ls; ls -l – long format; ls -a – include hidden files.

Tip: ls -lh – human‑readable sizes; ls -t – sort by modification time.

34. cd command (change directory)

Function: Changes the current working directory.

Usage: cd [directory] Example: cd /home; cd ..; cd ~.

Tip: cd - returns to previous directory; cd .. moves up one level.

35. mkdir command (create directories)

Function: Creates new directories.

Usage: mkdir [options] dirname Example: mkdir new_dir; mkdir -p /path/to/dir – create parent directories as needed.

Tip: mkdir -m mode dir sets permissions; mkdir -v dir1 dir2 shows creation details.

36. rm command (remove files and directories)

Function: Deletes files and directories.

Usage: rm [options] file/dir Example: rm file.txt; rm -r dir – recursive delete.

Tip: rm -i prompts before each removal; rm -rf forces deletion (use with caution).

37. cp command (copy files and directories)

Function: Copies files and directories.

Usage: cp [options] source target Example: cp file.txt /path/to/destination; cp -r dir /path/to/destination.

Tip: cp -i prompts before overwrite; cp -u copies only when source is newer.

38. mv command (move or rename files and directories)

Function: Moves or renames files and directories.

Usage: mv [options] source target Example: mv file.txt /path/to/destination; mv old_name new_name.

Tip: mv -i prompts before overwrite; mv -u moves only when source is newer.

39. chmod command (change file permissions)

Function: Modifies file or directory permissions.

Usage: chmod [options] mode file/dir Example: chmod +x script.sh; chmod 644 file.txt.

Tip: Numeric mode chmod 755 file.txt is quick; chmod -R applies recursively.

40. chown command (change file owner)

Function: Changes the owner (and optionally group) of a file or directory.

Usage: chown [options] owner[:group] file/dir Example: chown user1 file.txt; chown user1:group1 file.txt.

Tip: chown -R changes ownership recursively; chown --reference=ref_file target_file copies ownership from reference.

41. nohup (run program in background)

Function: Runs a command that continues after the terminal is closed, redirecting output to a file.

Usage: nohup command [args...] Example: nohup python my_script.py & – runs script in background, output goes to nohup.out.

Tip:

nohup java -Dserver.port=10086 -jar order.jar > output.log 2>&1 &

runs a JAR in background, redirecting stdout and stderr to output.log.

Note: By default output goes to nohup.out; you can redirect with > and 2>. Use jobs to list background jobs, fg to bring one to foreground, and Ctrl+C to stop.

42. java command (run Java applications)

Function: Starts the Java Virtual Machine and runs a Java program.

Usage: java [options] class [args...] Example: java -version; java -jar app.jar.

Tip: java -Xmx sets maximum heap size; java -Xms sets initial heap size.

43. javac command (compile Java source)

Function: Compiles Java source files into bytecode.

Usage: javac [options] sourcefile Example: javac HelloWorld.java.

Tip: javac -d sets output directory; javac -cp sets classpath.

44. jps command (list Java processes)

Function: Shows Java processes running on the system.

Usage: jps [options] Example: jps – list Java processes.

Tip: jps -l shows full class name; jps -v shows JVM arguments.

45. jstat command (JVM statistics monitor)

Function: Displays JVM statistical information.

Usage: jstat [options] pid [interval] [count] Example: jstat -gcutil 12345 – garbage‑collection stats for PID 12345.

Tip: jstat -gc shows GC stats; jstat -class shows class loading info.

46. jstack command (Java stack trace)

Function: Generates stack traces of Java threads.

Usage: jstack [options] pid Example: jstack 12345.

Tip: jstack -l includes lock information.

47. jmap command (Java memory map)

Function: Generates a memory snapshot of a Java process.

Usage: jmap [options] pid Example: jmap -heap 12345 – heap info for PID 12345.

Tip: jmap -histo produces an object histogram.

48. jcmd command (JVM diagnostic tool)

Function: Executes various JVM diagnostic commands.

Usage: jcmd pid command [options] Example: jcmd 12345 GC.run – forces garbage collection.

Tip: jcmd 12345 help lists all supported commands.

49. jinfo command (JVM configuration info)

Function: Shows JVM configuration details.

Usage: jinfo [options] pid Example: jinfo 12345.

Tip: jinfo -flags lists all JVM flags; jinfo -sysprops shows system properties.

50. jhat command (JVM heap dump analysis)

Function: Loads a Java heap dump file into the JVM for analysis.

Usage: jhat [options] heapdump_file Example: jhat heapdump.bin.

Tip: Use jmap to generate the heap dump; jhat -port sets the web server port.

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.

LinuxShellCommand LineNetworkingSystem Administration
Programmer1970
Written by

Programmer1970

Formerly called 'Code to 35'. Add our main WeChat ID to access a wealth of shared resources (algorithms, interview prep, tech stacks: Java, Python, Go, big data). We mainly share serious development techniques, focusing on output-driven input. Occasionally we post life snippets and gossip. Our aim is to attract precise traffic and test advertising opportunities.

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.