Linux Command Line Reference for System Diagnostics and File Management

Linux Command Line Reference for System Diagnostics

This reference details essential Linux commands used for system investigation, including checking disk usage, locating files, analyzing file content, and retrieving general system information.

1. Estimating Disk and File Size

du -sh /SOURCE
Estimates the total size of the specified directory or file /SOURCE.
df -h /SOURCE
Provides information on the hard disk partition usage where the file or directory /SOURCE resides.
df -h
Lists all hard disk partitions and displays their sizes and usage statistics in human-readable format.

2. Locating Files and Commands

which EXECUTABLE
Determines if the specified EXECUTABLE (program name) is installed, located in the system’s PATH, and shows its full path.
whereis PROGRAM
Determines the locations of the binary, source, and manual (man) files for the specified PROGRAM.
whatis COMMAND
Displays a brief description of the specified COMMAND.
find /SOURCE -name 'NAME'
Prints to the screen the path if a file named NAME is found within the directory /SOURCE.
find /SOURCE -name '*pattern*'
Prints to the screen all files containing the specified pattern in their name within the directory /SOURCE.
find . -name '*PATTERN*'
Prints to the screen all files containing the specified PATTERN in their name, starting from the current directory and traversing the directory tree.
slocate -r PATTERN
Searches the locate database and prints all files containing the text PATTERN in their name. Access to the database may require root privileges.
apropos PATTERN
Searches the whatis database and prints all entries that include the specified PATTERN.

3. Investigating File Content and Attributes

3.1. File Type and Listing

file /SOURCE
Determines and prints the file type to the display.
ls -l /SOURCE
Lists the contents of /SOURCE, displaying detailed information including permissions, size, and modification date of files.
ls -l /SOURCE | less
Lists the contents of /SOURCE. The output is piped (|) to less for paged viewing, useful when the list is too long for the console.
less /SOURCE
Prints the content of the file /SOURCE to the screen, allowing for paged viewing.

3.2. Searching File Content (Grep)

grep -i 'PATTERN' /SOURCE
Searches inside the file /SOURCE for lines matching the text PATTERN (case-insensitive, -i) and prints the full matching lines to the screen.
grep -Ri 'PATTERN' /SOURCE
Searches recursively (-R) in the directory /SOURCE (and its subdirectories) for lines matching the PATTERN (case-insensitive, -i). Prints the file name and the corresponding matching lines.
grep -Ri 'PATTERN' /SOURCE | less
Searches recursively in the directory /SOURCE for lines matching the PATTERN. The output is piped (|) to less for paged viewing.

4. Retrieving General System Information

who -b
Displays the date and time the system was last booted.
who -r
Displays the current system runlevel.
whoami
Displays the effective username of the current user.
uname -r
Displays the version of the running kernel.
arch
Prints the machine architecture to the screen.
lspci
Provides detailed information about all PCI devices.
id
Prints the user identification number (UID) and current group identification numbers (GIDs).
pwd
Prints the current working directory.
printenv
Prints all environment variables.
dmesg
Prints the last messages generated by the kernel buffer to the screen.
dmesg | grep -i 'error'
Filters the kernel messages (dmesg) and prints only those lines containing the text ‘error’ (case-insensitive).
ps
Prints the identification numbers (PIDs) of running processes.
top
Provides comprehensive, real-time information on system status, processes, CPU usage, and memory.
cat /proc/cpuinfo
Provides detailed information about the processor(s).
cat /proc/mounts
Provides information on partitions or network resources currently mounted on the machine.
alias
Lists all defined command aliases.
lsmod
Lists all modules currently loaded into the kernel, along with additional information. May require root privileges.
free
Lists the amount of used and available physical RAM and swap memory.
history
Lists the commands previously executed by the user.
manpath
Prints the search path for manual (man) pages.
mount
Lists all currently mounted devices in the system.
type COMMAND
Provides the location of the executable COMMAND and indicates whether it is an alias, function, or built-in shell command.
toe
Lists available terminal types.
set
Lists all defined shell variables and functions.
whoami
Displays the current user. This command produces the same result regardless of arguments used.