0% found this document useful (0 votes)
20 views

System Information Linux command.

System Information • uname — Print system information. • top — Display running processes. • ps — View active processes. • df — Display disk space usage. • du — Display file/directory space usage. • free — Display memory usage. • uptime — Display system uptime. • hostname — Show or set system’s hostname. • lsof — List open files. • env — Display environment variables. • export — Set or export environment variables. • echo — Print text or variables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

System Information Linux command.

System Information • uname — Print system information. • top — Display running processes. • ps — View active processes. • df — Display disk space usage. • du — Display file/directory space usage. • free — Display memory usage. • uptime — Display system uptime. • hostname — Show or set system’s hostname. • lsof — List open files. • env — Display environment variables. • export — Set or export environment variables. • echo — Print text or variables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 33

System Information

 uname — Print system information.

 top — Display running processes.

 ps — View active processes.

 df — Display disk space usage.

 du — Display file/directory space usage.

 free — Display memory usage.

 uptime — Display system uptime.

 hostname — Show or set system’s hostname.

 lsof — List open files.

 env — Display environment variables.

 export — Set or export environment variables.

 echo — Print text or variables.

Command: uname
Definition:
The uname command in Linux is used to display basic system information, such as the kernel name,
version, and other details about the operating system.

Pattern to Use:
uname [options]

Options:

1. No arguments — Displays the kernel name (default behavior).

2. -a — Displays all available system information.

3. -s — Prints the kernel name.

4. -n — Displays the network hostname.

5. -r — Shows the kernel release version.

6. -v — Displays the kernel version.

7. -m — Shows the machine hardware name.


8. -p — Displays the processor type (may return "unknown" on some systems).

9. -i — Displays the hardware platform (may return "unknown" on some systems).

10. -o — Shows the operating system name.

Examples:

1. Display the kernel name:


Command: uname

2. Display all system information:


Command: uname -a
Output (example):

Output Breakdown

i. Linux
Indicates the operating system kernel name.
ii. app01
The hostname of the system.
iii. 5.14.0-505.el9.x86_64
The kernel version and release number:
a. 5.14.0 is the kernel version.
b. 505 is the release number.
c. el9 indicates the distribution (Enterprise Linux 9).
d. x86_64 specifies the architecture (64-bit).
iv. #1 SMP PREEMPT_DYNAMIC
a. #1: Build version of the kernel.
b. SMP: Symmetric Multi-Processing enabled, meaning the kernel
supports multiple CPUs/cores.
c. PREEMPT_DYNAMIC: Dynamic preemption mode for responsiveness.
v. Thu Sep 5 07:54:07 UTC 2024
The date and time when the kernel was built.
vi. x86_64 x86_64 x86_64
a. The first x86_64: Machine hardware name.
b. The second x86_64: Processor type.
c. The third x86_64: Hardware platform.
vii. GNU/Linux
Indicates the operating system distribution is based on GNU/Linux.

3. Display the kernel release version:


Command: uname -r
Output

4. Display the kernel version:


Command: uname -v
Output

5. Display the machine hardware name:


Command: uname -m
Output
6. Display the operating system name:
Command: uname -o
Output

7. Display the network hostname:


Command: uname -n
Output

8. Combine multiple options for detailed info:


Command: uname -snrv
Output (example):

The uname command is especially useful for scripts and debugging to retrieve specific system details.

Command: top
Definition:
The top command in Linux is used to display real-time information about running processes, including
their resource usage, such as CPU, memory, and runtime. It is a dynamic, interactive tool for monitoring
system performance.
Pattern to Use:
top [options]

Options:

1. No arguments — Launches top with default settings.

2. -d [seconds] — Sets the update interval in seconds.

3. -n [iterations] — Displays a limited number of updates and exits.

4. -u [user] — Shows processes for a specific user.

5. -p [pid] — Monitors a specific process by its PID.

6. -b — Runs in batch mode for non-interactive use (useful in scripts).

7. -c — Displays the command line for each process.

Interactive Commands (while running top):

1. h — Displays help within top.

2. k — Kills a process by entering its PID.

3. r — Changes the priority (nice value) of a process.

4. q — Quits top.

5. z — Toggles color mode.

6. 1 — Displays CPU usage for each core.

7. M — Sorts processes by memory usage.

8. P — Sorts processes by CPU usage.

9. T — Sorts processes by runtime.

Examples:

1. Run top with default settings:


Command: top
Output
2. Run top with updates every 3 seconds:
Command: top -d 3
Output: Updates the display every 3 seconds.

3. Monitor only processes of a specific user:


Command: top -u vagrant
Output: Displays processes run by the vagrant user.

4. Monitor a specific process by PID:


Command: top -p 597
Output: Displays real-time stats for process ID 597.
5. Run in batch mode and save output to a file:
Command: top -b -n 1 > top_output.txt
Output: Saves a snapshot of top output to top_output.txt.

6. View sorted by memory usage in real-time:


Interactive Command: Press M while running top.
Output: Displays processes sorted by memory usage.
Command: ps
Definition:
The ps (Process Status) command in Linux is used to display information about the currently running
processes. It provides a snapshot of processes, their IDs, resource usage, and other attributes.

Pattern to Use:
ps [options]

Options:

1. No arguments — Displays processes for the current shell session.

2. -e — Displays all processes.

3. -f — Displays a full-format listing (with more details).

4. -u [user] — Shows processes for a specific user.

5. -p [pid] — Displays details of a specific process by its PID.

6. -a — Displays processes for all users, except session leaders.

7. -x — Includes processes without a terminal (daemon processes).

8. -l — Provides a long listing format.


9. --forest — Displays processes in a tree-like hierarchy.

10. --sort [key] — Sorts processes based on a specific field (e.g., %cpu).

Examples:

1. View processes for the current shell:


Command: ps
Output: Lists processes in the current terminal session.

2. Display all processes running on the system:


Command: ps -e
Output: Lists all processes for all users.
3. Full details for all processes:
Command: ps -ef
Output: Displays processes with additional details like user, PID, and start time.
4. View processes for a specific user:
Command: ps -u root
Output: Displays processes owned by the root user.

5. Monitor a specific process by its PID:


Command: ps -p 1234
Output: Displays details for process ID 1234.
6. View daemon processes (without a terminal):
Command: ps -x
Output: Lists processes running in the background.

7. Display a hierarchical tree of processes:


Command: ps --forest
Output: Displays processes in a tree-like format showing parent-child relationships.
8. Sort processes by CPU usage:
Command: ps -e --sort=-%cpu
Output: Lists all processes, sorted by CPU usage in descending order.
9. Sort processes by CPU usage (highest first):

Command: ps -e --sort=-%cpu
Output: Lists all processes, sorted by CPU usage in descending order.

10. Top 10 processes by memory usage:


Command: ps -eo pid,comm,%mem --sort=-%mem | head -n 11
Output: Lists the top 10 processes by memory usage along with their PID and command.
11. Top 10 processes by swap usage:
Command: ps -eo pid,comm,sz --sort=-sz | head -n 11
Output: Lists the top 10 processes by swap space usage (indicated by the sz field).

Command: df
Definition:
The df (Disk Filesystem) command in Linux is used to display information about the disk space usage of
file systems. It shows details such as total size, used space, available space, and the mount point of file
systems.

Pattern to Use:
df [options] [file|directory]

Options:

1. No arguments — Displays disk space usage for all mounted file systems.

2. -h — Displays output in human-readable format (e.g., KB, MB, GB).


3. -T — Shows the type of the file system.

4. -a — Includes pseudo, duplicate, and inaccessible file systems in the output.

5. -i — Displays inode usage instead of block usage.

6. --total — Provides a total at the bottom of the output.

7. --block-size=[size] — Displays output in a specific block size (e.g., 1M, 1G).

Examples:

1. Display disk space usage for all file systems:


Command: df
Output: Lists all file systems with their disk space usage.

2. Human-readable disk space usage:


Command: df -h
Output: Shows disk space usage with sizes in KB, MB, or GB.

3. Display the file system type:


Command: df -T
Output: Includes the file system type (e.g., ext4, xfs) in the output.
4. Show inode usage:
Command: df -i
Output: Displays inode usage for each file system.

5. Include all file systems, even empty ones:


Command: df -a
Output: Lists all file systems, including special ones like /proc.
6. Check disk space usage for a specific directory:
Command: df -h /home
Output: Shows disk space usage for the file system containing the /home directory.

7. Display output in 1GB blocks:


Command: df --block-size=1G
Output: Shows disk usage with block sizes in gigabytes.

8. Get a total disk usage summary:


Command: df -h --total
Output: Adds a total line at the bottom showing combined disk usage across all file systems.

Command: du
Definition:
The du (Disk Usage) command in Linux is used to estimate and display the disk space used by files and
directories. It provides a summary of disk usage for each directory and its contents.
Pattern to Use:
du [options] [file|directory]

Options:

1. No arguments — Displays disk usage of the current directory and its subdirectories.

2. -h — Displays the output in a human-readable format (e.g., KB, MB, GB).

3. -s — Shows only a total summary for the specified directory.

4. -a — Includes all files (not just directories) in the output.

5. -c — Displays a grand total at the end.

6. --max-depth=[n] — Limits the output to a specific depth of subdirectories.

7. -d [level] — Specifies the depth level for displaying directory usage.

8. -x — Prevents crossing file system boundaries.

9. --exclude=[pattern] — Excludes files or directories matching the specified pattern.

10. -B [size] — Displays disk usage with a specific block size (e.g., 1M, 1G).

Examples:

1. Display disk usage for the current directory:


Command: du
Output: Lists disk usage for the current directory and its subdirectories.

2. Human-readable disk usage:


Command: du -h
Output: Shows disk usage in KB, MB, or GB.
3. Show total disk usage for a directory:
Command: du -s /home/user
Output: Displays the total disk usage for the /home/user directory.

4. Include all files, not just directories:


Command: du -a /home/user
Output: Lists disk usage for all files and directories under /home/user.
5. Display a grand total:
Command: du -ch /home/user
Output: Shows disk usage for all files and directories and includes a total at the end.

6. Limit the depth of subdirectories:


Command: du -h --max-depth=1 /home/user
Output: Displays disk usage for /home/user and its immediate subdirectories only.

7. Prevent crossing file system boundaries:


Command: du -x /
Output: Shows disk usage within the current file system only (prevents crossing into other
mounted file systems).
8. Exclude certain files or directories:
Command: du --exclude='*.log' -h
Output: Lists disk usage, excluding files that match the .log pattern.
Command: free
Definition:
The free command in Linux is used to display the amount of free and used memory in the system,
including physical memory (RAM), swap space, and buffers. It helps monitor memory usage.
Pattern to Use:
free [options]

Options:

1. No arguments — Displays the memory usage in kilobytes.

2. -h — Displays the output in a human-readable format (e.g., KB, MB, GB).

3. -b — Displays memory in bytes.

4. -k — Displays memory in kilobytes (default).

5. -m — Displays memory in megabytes.

6. -g — Displays memory in gigabytes.

7. -t — Displays the total memory (sum of physical and swap memory).

8. -s [seconds] — Continuously displays memory usage, updating at the specified interval (in
seconds).

9. -c [count] — Specifies the number of times the memory usage is displayed when using the -s
option.

10. --si — Displays memory in powers of 1000 (e.g., 1K = 1000 bytes, 1M = 1000KB).

Examples:

1. Display memory usage in kilobytes:


Command: free
Output: Shows memory usage in kilobytes (default).

2. Human-readable memory usage:


Command: free -h
Output: Shows memory usage in a human-readable format (e.g., MB, GB).

3. Display memory usage in megabytes:


Command: free -m
Output: Shows memory usage in megabytes.
4. Display total memory (physical + swap):
Command: free -t
Output: Includes total memory usage across physical and swap memory.

5. Continuously display memory usage every 2 seconds:


Command: free -s 2
Output: Updates and shows memory usage every 2 seconds.

6. Display memory in gigabytes:


Command: free -g
Output: Shows memory usage in gigabytes.

Command: uptime
Definition:
The uptime command in Linux is used to display how long the system has been running, the number of
users currently logged in, and the system load averages for the past 1, 5, and 15 minutes.

Pattern to Use:
uptime [options]
Options:

1. No arguments — Displays the current uptime, number of users, and system load averages.

2. -p — Displays the uptime in a more human-readable, "pretty" format (e.g., "up 5 days, 3 hours").

3. -s — Displays the system boot time.

4. -h — Displays the output in a human-readable format (optional, similar to the -p option).

Examples:

1. Display system uptime:


Command: uptime
Output: Shows the current time, how long the system has been up, the number of users, and the
system load averages for the past 1, 5, and 15 minutes.

2. Display uptime in a human-readable format:


Command: uptime -p
Output: Shows the uptime in a more human-readable format (e.g., "up 10 days, 4 hours").

3. Display the system boot time:


Command: uptime -s
Output: Shows the date and time when the system was last booted.
Command: hostname
Definition:
The hostname command in Linux is used to display or set the system's hostname, which is the name
assigned to the machine on the network.

Pattern to Use:
hostname [options] [hostname]

Options:

1. No arguments — Displays the current system hostname.

2. -f — Displays the fully qualified domain name (FQDN) of the system.

3. -i — Displays the IP address of the system.

4. -I — Displays all network addresses (IPv4 and IPv6) of the system.

5. -s — Displays the short hostname (without domain).

6. -d — Displays the domain name of the system.

7. -v — Displays the version of the hostname command.

8. --help — Displays help information about the command.

Examples:

1. Display the current hostname:


Command: hostname
Output: Shows the current hostname of the system.

2. Display the fully qualified domain name (FQDN):


Command: hostname -f
Output: Shows the full domain name, including the hostname and domain.

3. Display the IP address of the system:


Command: hostname -i
Output: Shows the IP address associated with the hostname.
4. Display the domain name:
Command: hostname -d
Output: Shows the domain name of the system.

Command: lsof
Definition:
The lsof (List Open Files) command in Linux is used to list all open files and the processes that opened
them. It is useful for identifying files that are currently in use by processes, network connections, and the
status of mounted file systems.

Pattern to Use:
lsof [options]

Options:

1. No arguments — Lists all open files for all processes.

2. -a — Combines multiple conditions (e.g., listing files that are both opened and belong to a
specific user).

3. -c [command] — Lists open files for processes that match the specified command.

4. -u [user] — Lists open files for the specified user.

5. -p [pid] — Lists open files for the specified process ID (PID).

6. -i — Displays network connections (TCP, UDP) and their associated processes.

7. -n — Shows network addresses in numeric form (IP addresses and port numbers).

8. -t — Displays only process IDs (PID) without other details.

9. +D [directory] — Recursively lists open files in the specified directory.

10. -d [fd] — Lists open files associated with a specific file descriptor (e.g., stdin, stdout).

11. -h — Displays help information about the command.

12. --help — Provides help information for lsof usage.

Examples:

1. List all open files for all processes:


Command: lsof
Output: Displays a list of all open files along with their associated processes.
2. List open files for a specific user:
Command: lsof -u root
Output: Lists all open files for the root user.

3. Display network connections (TCP/UDP):


Command: lsof -i
Output: Lists open network connections along with the associated processes.

4. Show open files for a specific process (by PID):


Command: lsof -p 1234
Output: Lists all open files for the process with PID 1234.

5. Display open files for a specific command:


Command: lsof -c apache
Output: Lists open files for processes running the apache command.

6. List all open files in a specific directory:


Command: lsof +D /var/log
Output: Recursively lists open files under the /var/log directory.

7. Display only the process IDs (PIDs) of open files:


Command: lsof -t
Output: Shows only the process IDs for open files.

8. Display open files in numeric form for network connections:


Command: lsof -i -n
Output: Displays network connections with numeric IP addresses and port numbers.

9. Get help information for lsof:


Command: lsof --help
Output: Shows detailed help for using the lsof command.

Command: env
Definition:
The env command in Linux is used to display or modify the environment variables for the current
session. It can be used to set or clear environment variables, as well as to execute commands with a
modified environment.

Pattern to Use:
env [options] [COMMAND [ARGUMENTS]]

Options:

1. No arguments — Displays the environment variables for the current session.


2. -i — Starts with an empty environment, removing all current environment variables.

3. -u [variable] — Removes the specified environment variable.

4. -v — Prints the environment variables in a more detailed format.

5. --help — Displays help information about the env command.

Examples:

1. Display the environment variables for the current session:


Command: env
Output: Lists all environment variables and their values for the current session.

2. Start with an empty environment:


Command: env -i
Output: Runs a command with a completely clean environment, without any inherited variables.

3. Run a command with a modified environment variable:


Command: env VAR=value command
Output: Runs the command with the environment variable VAR set to value.

Example:
Command: env MYVAR=hello echo $MYVAR
Output: Displays hello, as the MYVAR is set to hello just for the execution of echo.

4. Remove a specific environment variable:


Command: env -u VAR
Output: Removes the specified variable VAR from the environment for the current session.

Example:
Command: env -u PATH
Output: Removes the PATH environment variable.

5. Display environment variables in verbose mode:


Command: env -v
Output: Lists environment variables with more details.

6. Execute a command with a modified environment:


Command: env VAR=value command
Output: Executes the command with the modified environment.

Command: export
Definition:
The export command in Linux is used to set environment variables and make them available to child
processes. It allows variables to be passed to subprocesses, making it essential for setting environment
variables that need to be inherited by programs or scripts executed from the shell.
Pattern to Use:
export [variable_name[=value]]

Options:

1. No arguments — Displays a list of all environment variables that have been exported.

2. variable_name=value — Sets an environment variable and exports it for child processes.

3. -n — Unsets (removes) the export property from a variable.

4. -p — Displays all exported environment variables in the format declare -x


variable_name="value".

Examples:

1. Set an environment variable:


Command: export VAR=value
Output: Sets the environment variable VAR with the value value and makes it available to child
processes.

2. Check the exported environment variables:


Command: export
Output: Displays a list of all currently exported environment variables.

3. Set and export a variable for a command:


Command: export VAR=value && command
Output: Sets the variable VAR and runs the command with that environment variable.

Example:
Command: export MYVAR=hello && echo $MYVAR
Output: Displays hello as the MYVAR environment variable is set and passed to the echo command.

4. Unset an exported variable:


Command: export -n VAR
Output: Removes the export property from VAR, meaning it will no longer be inherited by child
processes.

5. Display exported environment variables with -p option:


Command: export -p
Output: Lists all exported environment variables in the declare -x format.

Command: echo
Definition:
The echo command in Linux is used to display a line of text or variables to the terminal. It is useful for
printing strings, variables, and the output of commands.
Pattern to Use:
echo [options] [string]

Options:

1. No arguments — Prints a blank line (newline).

2. -n — Prevents the trailing newline (output is not followed by a new line).

3. -e — Enables interpretation of escape sequences (e.g., \n, \t, etc.).

4. -E — Disables the interpretation of escape sequences (default behavior).

5. --help — Displays help information for the echo command.

Escape Sequences:

 \n — Newline.

 \t — Horizontal tab.

 \r — Carriage return.

 \\ — Backslash.

 \b — Backspace.

 \a — Alert (bell sound).

 \v — Vertical tab.

Examples:

1. Display a simple string:


Command: echo "Hello, World!"
Output: Prints Hello, World!.

2. Display a string without a newline:


Command: echo -n "Hello, World!"
Output: Prints Hello, World! without a trailing newline (so the next prompt will appear on the
same line).

3. Display with escape sequences enabled:


Command: echo -e "Line1\nLine2"
Output: Prints:

Line1

Line2

4. Display with a tab between words:


Command: echo -e "Hello\tWorld"
Output: Prints Hello World (with a tab space between "Hello" and "World").
5. Display a string with a backslash:
Command: echo -e "This is a backslash: \\"
Output: Prints This is a backslash: \.

6. Display the value of an environment variable:


Command: echo $HOME
Output: Displays the value of the HOME environment variable (e.g., /home/user).

7. Display multiple words/variables:


Command: echo "The home directory is $HOME"
Output: Prints The home directory is /home/user.

8. Display help for the echo command:


Command: echo --help
Output: Shows detailed help for using the echo command.

You might also like