Linux Commands
Commands to list the files and directories in Linux Operating System
1. `ls`: List files and directories in the current directory.
Explanation: The `ls` command without any options lists the files and directories in the current working directory.
Example:
```
$ ls
file1.txt file2.txt folder1 folder2
```
2. `ls -l`: List files and directories with detailed information.
Explanation: The `-l` option provides a long listing format that displays detailed information about each file and directory.
Example:
```
$ ls -l
total 24
-rw-r--r-- 1 username group 1024 Aug 6 10:00 file1.txt
-rw-r--r-- 1 username group 2048 Aug 6 10:01 file2.txt
drwxr-xr-x 2 username group 4096 Aug 6 10:02 folder1
drwxr-xr-x 3 username group 8192 Aug 6 10:03 folder2
```
3. `ls -a`: List all files and directories, including hidden ones.
Explanation: The `-a` option shows all files and directories, including hidden ones that start with a dot `.`.
Example:
```
$ ls -a
. .. file1.txt file2.txt .hiddenfile folder1 folder2
```
4. `ls -h`: List files and directories with human-readable file sizes.
Explanation: The `-h` option displays file sizes in a human-readable format (e.g., 1K, 2M, 3G).
Example:
```
$ ls -lh
-rw-r--r-- 1 username group 1.0K Aug 6 10:00 file1.txt
-rw-r--r-- 1 username group 2.0K Aug 6 10:01 file2.txt
drwxr-xr-x 2 username group 4.0K Aug 6 10:02 folder1
drwxr-xr-x 3 username group 8.0K Aug 6 10:03 folder2
```
5. `ls -R`: List files and directories recursively.
Explanation: The `-R` option shows the contents of directories recursively, including subdirectories.
Example:
```
$ ls -R
.:
file1.txt file2.txt folder1 folder2
./folder1:
file3.txt
./folder2:
file4.txt file5.txt
```
6. `ls /path/to/directory`: List files and directories in a specific directory.
Explanation: By specifying the path to a directory, you can list its contents.
Example:
```
$ ls /home/username/documents
file1.txt file2.txt folder1 folder2
```
7. `ls -t`: List files and directories by modification time (newest first).
Explanation: The `-t` option sorts the output by modification time, with the newest files listed first.
Example:
```
$ ls -t
folder2 file2.txt folder1 file1.txt
```
8. `ls -r`: List files and directories in reverse order.
Explanation: The `-r` option reverses the order of the output.
Example:
```
$ ls -r
folder2 folder1 file2.txt file1.txt
```
9. `ls -i`: List files and directories with their inode numbers.
Explanation: The `-i` option displays the inode numbers of files and directories.
Example:
```
$ ls -i
1423434 file1.txt 1423435 file2.txt 1423436 folder1 1423437 folder2
```
10. `ls -m`: List files and directories as a comma-separated list.
Explanation: The `-m` option formats the output as a comma-separated list.
Example:
```
$ ls -m
file1.txt, file2.txt, folder1, folder2
```
11. `ls -p`: List files and directories with a trailing slash for directories.
Explanation: The `-p` option appends a trailing slash (`/`) to directory names.
Example:
```
$ ls -p
file1.txt file2.txt folder1/ folder2/
```
12. `ls -F`: List files and directories with file type indicators.
Explanation: The `-F` option appends file type indicators to the output (e.g., `/` for directories, `*` for executables).
Example:
```
$ ls -F
file1.txt file2.txt folder1/ folder2/
```
13. `ls -G`: List files and directories with colored output (requires a colored terminal).
Explanation: The `-G` option provides colorized output for easy visual distinction.
Example (Note: The actual colors may vary):
```
$ ls -G
file1.txt file2.txt folder1 folder2
```
14. `ls -d`: List only directories.
Explanation: The `-d` option lists only the directories themselves, not their contents.
Example:
```
$ ls -d */
folder1/ folder2/
```
15. `ls -1`: List files and directories in a single column.
Explanation: The `-1` option lists the items in a single column, one per line.
Example:
```
$ ls -1
file1.txt
file2.txt
folder1
folder2
```
16. `ls -U`: List files and directories without sorting.
Explanation: The `-U` option prevents sorting the output.
Example:
```
$ ls -U
file1.txt folder2 folder1 file2.txt
```
17. `ls -S`: List files and directories sorted by size (largest first).
Explanation: The `-S` option sorts the output by file size, with the largest files listed first.
Example:
```
$ ls -S
folder2 file2.txt folder1 file1.txt
```
18. `ls -X`: List files and directories sorted by extension.
Explanation: The `-X` option sorts the output by file extension.
Example:
```
$ ls -X
file1.txt file2.txt folder2 folder1
```
19. `tree`: List files and directories in a tree-like format (requires installation of the `tree` package).
Explanation: The `tree` command shows the directory structure in a tree format.
Example:
```
$ tree
.
├── file1.txt
├── file2.txt
├── folder1
│ └── file3.txt
└── folder2
├── file4.txt
└── file5.txt
2 directories, 5 files
```
20. `find /path/to/directory`: List files and directories using the `find` command (recursively).
Explanation: The `find` command can list files and directories recursively in a specific directory.
Example:
```
$ find /home/username/documents
/home/username/documents
/home/username/documents/file1.txt
/home/username/documents/file2.txt
/home/username/documents/folder1
/home/username/documents/folder1/file3.txt
/home/username/documents/folder2
/home/username/documents/folder2/file4.txt
/home/username/documents/folder2/file5.txt
```
These commands offer various options to customize the output and help you explore the contents of directories on your Linux system.
Commands to Filter and Search for files and directories in Linux
1. `ls`: List files and directories in the current directory.
Explanation: The `ls` command lists files and directories in the current working directory.
Example:
```
$ ls
file1.txt file2.txt folder1 folder2
```
2. `ls -l`: List files and directories with detailed information.
Explanation: The `-l` option provides a long listing format with detailed information.
Example:
```
$ ls -l
total 24
-rw-r--r-- 1 username group 1024 Aug 6 10:00 file1.txt
-rw-r--r-- 1 username group 2048 Aug 6 10:01 file2.txt
drwxr-xr-x 2 username group 4096 Aug 6 10:02 folder1
drwxr-xr-x 3 username group 8192 Aug 6 10:03 folder2
```
3. `find`: Search for files and directories based on various criteria.
Explanation: The `find` command searches for files and directories recursively based on specified criteria.
Example (search for all files in the current directory and subdirectories):
```
$ find . -type f
./file1.txt
./file2.txt
./folder1/file3.txt
./folder2/file4.txt
./folder2/file5.txt
```
4. `locate`: Quickly find files and directories based on a pre-built index.
Explanation: The `locate` command uses a pre-built index to quickly find files and directories.
Example (search for files with "file" in the name):
```
$ locate file
/path/to/file1.txt
/path/to/file2.txt
```
5. `grep`: Search for a specific pattern within files.
Explanation: The `grep` command searches for a specified pattern within files.
Example (search for "pattern" in files):
```
$ grep "pattern" file1.txt
This is a sample pattern.
```
6. `grep -r`: Search recursively within files and directories.
Explanation: The `-r` option allows `grep` to search recursively in directories.
Example (search for "pattern" in all files within a directory):
```
$ grep -r "pattern" /path/to/directory
/path/to/directory/file1.txt: This is a sample pattern.
/path/to/directory/subdir/file2.txt: Another pattern here.
```
7. `grep -i`: Perform a case-insensitive search.
Explanation: The `-i` option makes the `grep` search case-insensitive.
Example:
```
$ grep -i "Pattern" file.txt
This is a sample PATTERN.
```
8. `grep -v`: Invert the search to display lines that do not match.
Explanation: The `-v` option inverts the search to show lines that do not match the pattern.
Example:
```
$ grep -v "exclude" file.txt
This line is included.
```
9. `grep -l`: List only filenames containing the specified pattern.
Explanation: The `-l` option lists only the filenames containing the pattern.
Example:
```
$ grep -l "pattern" *.txt
file1.txt
file2.txt
```
10. `grep -c`: Count the occurrences of the pattern in each file.
Explanation: The `-c` option counts the occurrences of the pattern in each file.
Example:
```
$ grep -c "pattern" *.txt
file1.txt:2
file2.txt:1
```
11. `grep -w`: Search for the whole word (exact match).
Explanation: The `-w` option searches for the whole word that matches the pattern.
Example:
$ grep -w "word" file.txt
This is the word you're looking for.
12. `grep -n`: Display line numbers along with matching lines.
Explanation: The `-n` option displays line numbers along with the matching lines.
Example:
```
$ grep -n "line" file.txt
3:This is a line of text.
```
13. `grep -E` (or `egrep`): Use extended regular expressions.
Explanation: The `-E` option allows `grep` to use extended regular expressions.
Example (search for "pattern" or "string"):
```
$ grep -E "pattern|string" file.txt
This line contains the pattern.
Another line with the string.
```
14. `grep -A` and `grep -B`: Display lines before and/or after matching lines.
Explanation: The `-A` option displays lines after matching lines, while `-B` option displays lines before matching lines.
Example (show 2 lines after the matching line):
```
$ grep -A 2 "pattern" file.txt
Line before.
This is the matching pattern.
Line after 1.
Line after 2.
```
15. `grep -C`: Display lines before and after matching lines.
Explanation: The `-C` option displays lines both before and after matching lines.
Example (show 2 lines before and after the matching line):
```
$ grep -C 2 "pattern" file.txt
Line before 1.
Line before 2.
This is the matching pattern.
Line after 1.
Line after 2.
```
16. `grep -rL`: List files that do not contain the pattern.
Explanation: The `-rL` option lists files that do not contain the specified pattern.
Example:
```
$ grep -rL "pattern" /path/to/directory
/path/to/directory/file3.txt
```
17. `grep -q`: Quiet mode (no output), useful for scripting.
Explanation: The `-q` option suppresses normal output and is often used in scripting.
Example:
```
$ if grep -q "pattern" file.txt; then echo "Pattern found"; fi
Pattern found
```
18. `find -name`: Search for files and directories by name.
Explanation: The `find` command with `-name` option searches for files and directories by name.
Example (search for files named "file.txt"):
```
$ find /path/to/directory -name "file.txt"
/path/to/directory/file.txt
/path/to/anotherdir/file.txt
```
19. `find -type`: Search for files and directories by type.
Explanation: The `find` command with `-type` option searches for files and directories by their type (e.g., `f` for files, `d` for directories).
Example (search for directories
):
```
$ find /path/to/directory -type d
/path/to/directory
/path/to/directory/subdir
```
20. `find -size`: Search for files by size.
Explanation: The `find` command with `-size` option searches for files by size (e.g., `+10M` for files larger than 10 megabytes).
Example (search for files larger than 1MB):
```
$ find /path/to/directory -size +1M
/path/to/directory/largefile1.txt
/path/to/directory/subdir/largefile2.txt
```
These commands provide various options to filter and search for files and directories based on specific criteria on your Linux system.
Commands to retrieve system information in Linux
Commands to retrieve system information in Linux, along with explanations, examples, and sample outputs for each command:
1. `uname`: Display system information.
Explanation: The `uname` command provides basic system information such as kernel name, release, version, and machine.
Example:
$ uname -a
Linux hostname 5.4.0-81-generic #91-Ubuntu SMP Thu Jul 15 19:09:17 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
2. `lsb_release`: Display distribution-specific information.
Explanation: The `lsb_release` command shows Linux Standard Base (LSB) and distribution-specific information.
Example:
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.3 LTS
Release: 20.04
Codename: focal
3. `cat /etc/os-release`: Show information about the operating system.
Explanation: This command reads the `/etc/os-release` file, which contains various system information.
Example:
$ cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.3 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
4. `hostnamectl`: Display hostname and system information.
Explanation: The `hostnamectl` command provides information about the system's hostname, operating system, kernel, and more.
Example:
$ hostnamectl
Static hostname: hostname
Icon name: computer
Machine ID: 1234567890abcdef1234567890abcdef
5. `df`: Show disk space usage.
Explanation: The `df` command displays information about disk space usage on file systems.
Example:
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 100G 20G 80G 20% /
6. `free`: Display system memory usage.
Explanation: The `free` command provides information about system memory (RAM) and swap usage.
Example:
$ free -h
total used free shared buff/cache available
Mem: 3.9G 1.2G 2.2G 512M 525M 2.4G
Swap: 2.0G 0B 2.0G
7. `top`: Display real-time system information and processes.
Explanation: The `top` command shows real-time system statistics and a list of processes.
Example:
$ top
top - 10:33:45 up 5 days, 2:20, 1 user, load average: 0.24, 0.36, 0.42
Tasks: 271 total, 1 running, 269 sleeping, 0 stopped, 1 zombie
%Cpu(s): 1.5 us, 1.2 sy, 0.0 ni, 96.6 id, 0.0 wa, 0.0 hi, 0.8 si, 0.0 st
KiB Mem : 4044872 total, 2317992 free, 935888 used, 792992 buff/cache
8. `uptime`: Display system uptime.
Explanation: The `uptime` command shows how long the system has been running and the average load.
Example:
$ uptime
10:40:54 up 5 days, 2:27, 1 user, load average: 0.23, 0.34, 0.41
9. `iostat`: Display CPU, I/O statistics, and device utilization.
Explanation: The `iostat` command provides information about CPU and I/O statistics.
Example:
$ iostat
Linux 5.4.0-81-generic (hostname) 08/06/2023 _x86_64_ (4 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
3.78 0.00 1.13 0.11 0.00 94.98
10. `lscpu`: Display CPU architecture information.
Explanation: The `lscpu` command provides detailed information about the CPU architecture.
Example:
$ lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
11. `lsblk`: List block devices (disks and partitions).
Explanation: The `lsblk` command displays information about block devices, such as disks and partitions.
Example:
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 238.5G 0 disk
├─sda1 8:1 0 200G 0 part /
12. `lshw`: List hardware information.
Explanation: The `lshw` command provides detailed information about the system's hardware.
Example (requires `sudo`):
$ sudo lshw
description: Computer
product: ...
13. `lspci`: List PCI devices.
Explanation: The `lspci` command lists all PCI devices connected to the system.
Example:
$ lspci
00:00.0 Host bridge: ...
14. `lsusb`: List USB devices.
Explanation: The `lsusb` command lists all USB devices connected to the system.
Example:
$ lsusb
Bus 002 Device 003: ...
15. `hwinfo`: Display detailed hardware information.
Explanation: The `hwinfo` command provides detailed hardware information.
Example (requires `sudo`):
$ sudo hwinfo --short
cpu:
Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz, 1632 MHz
Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz, 1000 MHz
16. `inxi`: Display system information in a compact format.
Explanation: The `inxi` command provides a concise overview of system information.
Example (requires installation):
$ inxi -F
System: Host: hostname Kernel: 5.4.0-81-generic x86_64 bits: 64 Desktop: GNOME Distro: Ubuntu 20.
04.3 LTS
17. `dmidecode`: Display hardware information from DMI (Desktop Management Interface).
Explanation: The `dmidecode` command retrieves information from the DMI table.
Example (requires `sudo`):
$ sudo dmidecode -t memory
Memory Array Mapped Address
Starting Address: 0x00000000000
Ending Address: 0x000FFFFFFF
Range Size: 4 GB
Physical Device Handle: 0x0009
Memory Error Information Handle: Not Provided
Device Locator: ChannelA-DIMM0
Bank Locator: BANK 0
Memory Type: DDR4
Memory Type Detail: Synchronous Unbuffered (Unregistered)
Memory Speed: 2133 MT/s
Manufacturer: 857F00008xxx
Serial Number: 12345678
Asset Tag: 9876543210
Part Number: F4-2133C15-8GVR
Rank: 1
Configured Memory Speed: 2133 MT/s
Minimum Voltage: Unknown
Maximum Voltage: Unknown
Configured Voltage: 1.200 V
18. `uptime`: Display system uptime and load averages.
Explanation: The `uptime` command shows how long the system has been running and load averages.
Example:
$ uptime
15:20:00 up 20:11, 1 user, load average: 0.18, 0.27, 0.31
19. `dmesg`: Display kernel ring buffer messages.
Explanation: The `dmesg` command displays kernel ring buffer messages, including boot messages and hardware events.
Example:
$ dmesg | tail -20
[ 33.048172] Bluetooth: RFCOMM socket layer initialized
[ 33.048180] Bluetooth: RFCOMM ver 1.11
[ 1261.179122] usb 1-3: USB disconnect, device number 2
[ 1283.204159] usb 1-3: new high-speed USB device number 4 using xhci_hcd
[ 1283.353125] usb 1-3: New USB device found, idVendor=0781, idProduct=5580, bcdDevice= 1.00
20. `lsmod`: List loaded kernel modules.
Explanation: The `lsmod` command lists the currently loaded kernel modules.
Example:
$ lsmod
Module Size Used by
nls_utf8 16384 1
isofs 49152 1
udf 94208 0
crc_itu_t 16384 1 udf
Display information about currently logged in users
`who` commands in Linux along with explanations, examples, and sample outputs for each command:
1. `who`: Display information about currently logged in users.
Explanation: The `who` command shows a list of currently logged in users, their terminal, login time, and IP address.
Example:
$ who
username tty1 2023-08-06 10:00 (:0)
2. `whoami`: Display the username of the current user.
Explanation: The `whoami` command prints the effective username of the current user.
Example:
$ whoami
username
3. `w`: Display information about currently logged in users and their activities.
Explanation: The `w` command shows a detailed list of currently logged in users, their activities, and system load.
Example:
$ w
10:00:34 up 1 day, 2:21, 3 users, load average: 0.24, 0.26, 0.33
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
username pts/0 :0 09:45 0.00s 0.07s 0.00s w
4. `whois`: Display information about domain ownership and registration.
Explanation: The `whois` command queries a WHOIS database to retrieve information about domain ownership.
Example:
$ whois example.com
Domain Name: EXAMPLE.COM
Registry Domain ID: 12345678_DOMAIN_COM-VRSN
Registrar WHOIS Server: whois.example-registrar.com
5. `whatis`: Display short descriptions of system commands.
Explanation: The `whatis` command provides a brief description of a given command.
Example:
$ whatis ls
ls (1) - list directory contents
6. `finger`: Display user information.
Explanation: The `finger` command provides information about a specific user or all logged in users.
Example:
$ finger username
Login: username Name: John Doe
Directory: /home/username Shell: /bin/bash
On since Fri Aug 6 10:00 (UTC) on pts/0 from :0
No mail.
Project:
Plan:
7. `last`: Display information about recently logged in users.
Explanation: The `last` command shows a list of recent logins, including usernames, terminal, login times, and more.
Example:
$ last
username pts/0 :0 Fri Aug 6 10:00 still logged in
8. `lastlog`: Display information about last logged in users.
Explanation: The `lastlog` command displays a list of when each user last logged in.
Example:
$ lastlog
Username Port From Latest
username pts/0 :0 Fri Aug 6 10:00:00 +0000 2023
9. `write`: Send messages to other users.
Explanation: The `write` command allows you to send messages to other logged-in users.
Example:
$ write username
Hello, how are you?
^D
10. `mesg`: Control write access to your terminal.
Explanation: The `mesg` command controls whether other users can send messages to your terminal using the `write` command.
Example (allow messages):
$ mesg y
Example (deny messages):
$ mesg n
Commands to gather Hardware information in Linux
Commands to gather hardware information in Linux, along with explanations, examples, and sample outputs for each command:
1. `lshw`: Display detailed hardware information.
Explanation: The `lshw` command provides comprehensive details about the system's hardware components.
Example (requires `sudo`):
$ sudo lshw
description: Computer
product: ...
2. `lscpu`: Display CPU architecture information.
Explanation: The `lscpu` command provides detailed information about the CPU architecture.
Example:
$ lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
3. `lsblk`: List block devices (disks and partitions).
Explanation: The `lsblk` command displays information about block devices, such as disks and partitions.
Example:
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 238.5G 0 disk
├─sda1 8:1 0 200G 0 part /
4. `lspci`: List PCI devices.
Explanation: The `lspci` command lists all PCI devices connected to the system.
Example:
$ lspci
00:00.0 Host bridge: ...
5. `lsusb`: List USB devices.
Explanation: The `lsusb` command lists all USB devices connected to the system.
Example:
$ lsusb
Bus 002 Device 003: ...
6. `hwinfo`: Display detailed hardware information.
Explanation: The `hwinfo` command provides comprehensive hardware details.
Example (requires `sudo`):
$ sudo hwinfo --short
cpu:
Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz, 1632 MHz
Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz, 1000 MHz
7. `inxi`: Display system information in a compact format.
Explanation: The `inxi` command provides a concise overview of system information.
Example (requires installation):
$ inxi -F
System: Host: hostname Kernel: 5.4.0-81-generic x86_64 bits: 64 Desktop: GNOME Distro: Ubuntu 20.04.3 LTS
8. `dmidecode`: Display hardware information from DMI (Desktop Management Interface).
Explanation: The `dmidecode` command retrieves information from the DMI table.
Example (requires `sudo`):
$ sudo dmidecode -t memory
Memory Array Mapped Address
Starting Address: 0x00000000000
Ending Address: 0x000FFFFFFF
Range Size: 4 GB
Physical Device Handle: 0x0009
Memory Error Information Handle: Not Provided
Device Locator: ChannelA-DIMM0
Bank Locator: BANK 0
Memory Type: DDR4
Memory Type Detail: Synchronous Unbuffered (Unregistered)
Memory Speed: 2133 MT/s
9. `cat /proc/cpuinfo`: Display CPU information.
Explanation: The `/proc/cpuinfo` file provides detailed information about the CPU(s).
Example:
$ cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
10. `cat /proc/meminfo`: Display memory information.
Explanation: The `/proc/meminfo` file provides information about system memory.
Example:
$ cat /proc/meminfo
MemTotal: 4044872 kB
MemFree: 2317992 kB
11. `cat /proc/version`: Display Linux kernel version.
Explanation: The `/proc/version` file shows the Linux kernel version.
Example:
$ cat /proc/version
Linux version 5.4.0-81-generic (buildd@lgw01-amd64-030) ...
12. `cat /proc/partitions`: Display partition information.
Explanation: The `/proc/partitions` file lists information about disk partitions.
Example:
$ cat /proc/partitions
major minor #blocks name
8 0 250059096 sda
8 1 200781250 sda1
13. `cat /proc/swaps`: Display swap space information.
Explanation: The `/proc/swaps` file provides details about swap usage.
Example:
$ cat /proc/swaps
Filename Type Size Used Priority
/swapfile file 2097148 16588 -2
14. `lsmem`: Display memory information.
Explanation: The `lsmem` command displays details about memory information.
Example (requires installation):
$ lsmem
range setup
size: 3.84 GiB
width: 64 bits
15. `smartctl`: Display SMART data of storage devices.
Explanation: The `smartctl` command shows Self-Monitoring, Analysis and Reporting Technology (SMART) data of storage devices.
Example (requires `sudo` and a valid storage device):
$ sudo smartctl -a /dev/sda
SMART overall-health self-assessment test result: PASSED
16. `lsdev`: List devices on the system.
Explanation: The `lsdev` command lists devices on the system.
Example (requires installation):
$ lsdev
Host bridge : Intel Corporation Xeon E3-1200 v5/E3-1500 v5/6th Gen Core Processor Host Bridge/DRAM Registers
17. `lsinitrd`: List content of an initramfs image.
Explanation: The `lsinitrd` command displays the content of an initramfs image.
Example (requires installation and a valid initramfs image):
$ sudo lsinitrd /boot/initrd.img-5.4.0-81-generic
Commands to provide USER information and manage USERS in Linux
Commands to provide USER information and manage USER in Linux, along with explanations, examples, and sample outputs for each command:
1. `who`: Display information about currently logged-in users.
Explanation: The `who` command shows a list of currently logged-in users, their terminal, login time, and IP address.
Example:
$ who
username tty1 2023-08-06 10:00 (:0)
2. `whoami`: Display the username of the current user.
Explanation: The `whoami` command prints the effective username of the current user.
Example:
$ whoami
username
3. `w`: Display information about currently logged-in users and their activities.
Explanation: The `w` command shows a detailed list of currently logged-in users, their activities, and system load.
Example:
$ w
10:00:34 up 1 day, 2:21, 3 users, load average: 0.24, 0.26, 0.33
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
username pts/0 :0 09:45 0.00s 0.07s 0.00s w
4. `finger`: Display user information.
Explanation: The `finger` command provides information about a specific user or all logged-in users.
Example:
$ finger username
Login: username Name: John Doe
Directory: /home/username Shell: /bin/bash
On since Fri Aug 6 10:00 (UTC) on pts/0 from :0
No mail.
Project:
Plan:
5. `id`: Display user and group information.
Explanation: The `id` command shows information about the current user's identity and group memberships.
Example:
$ id
uid=1000(username) gid=1000(username) groups=1000(username),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),116(lpadmin),126(sambashare)
6. `groups`: Display group memberships for a user.
Explanation: The `groups` command lists the groups a user is a member of.
Example:
$ groups username
username : username adm cdrom sudo dip plugdev lpadmin sambashare
7. `passwd`: Change user password.
Explanation: The `passwd` command allows a user to change their password.
Example (changing user's password):
$ passwd
Changing password for username.
(current) UNIX password:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
8. `useradd`: Add a new user.
Explanation: The `useradd` command is used to create a new user account.
Example (adding a new user):
$ sudo useradd -m newuser
9. `userdel`: Delete a user account.
Explanation: The `userdel` command removes a user account.
Example (deleting a user):
$ sudo userdel -r olduser
10. `usermod`: Modify user account properties.
Explanation: The `usermod` command is used to modify user account properties.
Example (changing a user's home directory):
$ sudo usermod -d /new/home/directory username
11. `chfn`: Change user information.
Explanation: The `chfn` command allows you to change user information, such as the user's real name and office.
Example (changing user information):
$ chfn username
Changing the user information for username
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
12. `chsh`: Change user's login shell.
Explanation: The `chsh` command is used to change a user's default login shell.
Example (changing user's shell to Zsh):
$ chsh -s /usr/bin/zsh username
13. `su`: Switch user or become superuser.
Explanation: The `su` command allows you to switch to another user's account or become the superuser.
Example (switching to another user):
$ su - otheruser
Password:
14. `sudo`: Execute commands as another user (usually superuser).
Explanation: The `sudo` command allows authorized users to execute commands as another user, typically the superuser.
Example (running a command with sudo):
$ sudo apt update
[sudo] password for username:
15. `visudo`: Edit the sudoers file safely.
Explanation: The `visudo` command opens the sudoers file for editing, ensuring safe modification of sudo privileges.
Example (edit the sudoers file):
$ sudo visudo
16. `gpasswd`: Manage group passwords.
Explanation: The `gpasswd` command is used to administer group passwords and membership.
Example (adding a user to a group):
$ sudo gpasswd -a username groupname
17. `chage`: Change user password expiration and aging information.
Explanation: The `chage` command is used to set password expiration and aging information for a user.
Example (setting password expiration):
$ sudo chage -E 2024-12-31 username
18. `newusers`: Update user information from a text file.
Explanation: The `newusers` command reads user information from a text file and creates or updates user accounts.
Example (creating multiple users from a file):
bash
$ sudo newusers < userlist.txt
19. `usermod`: Modify user account properties.
Explanation: The `usermod` command is used to modify user account properties.
Example (changing user's group membership):
bash
$ sudo usermod -aG groupname username
20. `chown` and `chgrp`: Change file ownership and group.
Explanation: The `chown` command changes the ownership of a file, and the `chgrp` command changes the group ownership.
Example (changing file ownership):
bash
$ sudo chown newowner:groupname filename
Commands to work with Files & Directories in Linux
Commands to work with FILES and DIRECTORIES in Linux, along with explanations, examples, and sample outputs for each command:
1. `ls`: List files and directories.
Explanation: The `ls` command is used to list the contents of a directory.
Example:
$ ls
file1.txt directory1 file2.txt
2. `pwd`: Display the current working directory.
Explanation: The `pwd` command shows the full path of the current working directory.
Example:
$ pwd
/home/username
3. `cd`: Change the current directory.
Explanation: The `cd` command is used to navigate to a different directory.
Example (changing to a subdirectory):
$ cd directory1
4. `cp`: Copy files and directories.
Explanation: The `cp` command is used to copy files or directories from one location to another.
Example (copying a file):
$ cp file1.txt backup/
5. `mv`: Move (rename) files and directories.
Explanation: The `mv` command is used to move files or directories to a different location or rename them.
Example (moving a file):
$ mv file2.txt documents/
6. `rm`: Remove (delete) files and directories.
Explanation: The `rm` command is used to delete files or directories.
Example (removing a file):
$ rm file1.txt
7. `mkdir`: Create directories.
Explanation: The `mkdir` command is used to create new directories.
Example (creating a directory):
$ mkdir new_directory
8. `rmdir`: Remove empty directories.
Explanation: The `rmdir` command is used to remove empty directories.
Example (removing a directory):
$ rmdir empty_directory
9. `touch`: Create empty files or update timestamps.
Explanation: The `touch` command is used to create empty files or update the timestamps of existing files.
Example (creating a new file):
$ touch new_file.txt
10. `cat`: Display the contents of a file.
Explanation: The `cat` command is used to display the contents of a file.
Example:
$ cat file.txt
This is the content of the file.
11. `more` and `less`: View file contents interactively.
Explanation: The `more` and `less` commands allow you to view file contents interactively, one screen at a time.
Example (using `less`):
$ less large_file.txt
12. `head` and `tail`: Display the beginning or end of a file.
Explanation: The `head` and `tail` commands show the first or last few lines of a file.
Example (using `head`):
$ head -n 10 file.txt
$ tail -f file.txt
13. `wc`: Count lines, words, and characters in a file.
Explanation: The `wc` command counts the number of lines, words, and characters in a file.
Example:
$ wc file.txt
10 50 300 file.txt
14. `chmod`: Change file permissions.
Explanation: The `chmod` command is used to change the permissions of a file or directory.
Example (changing permissions):
$ chmod 755 script.sh
15. `chown` and `chgrp`: Change file ownership and group.
Explanation: The `chown` command changes the ownership of a file, and the `chgrp` command changes the group ownership.
Example (changing ownership):
$ chown new_owner:new_group file.txt
16. `find`: Search for files and directories.
Explanation: The `find` command is used to search for files and directories based on various criteria.
Example (finding files with a specific extension):
$ find /path/to/directory -name "*.txt"
17. `grep`: Search for text within files.
Explanation: The `grep` command searches for a specified text pattern within files.
Example (searching for a keyword):
$ grep "keyword" file.txt
18. `diff`: Compare files line by line.
Explanation: The `diff` command compares two files and displays the differences.
Example (comparing two files):
$ diff file1.txt file2.txt
19. `tar`: Compress and extract files.
Explanation: The `tar` command is used to bundle multiple files and directories together into a single archive file, which can be compressed using other utilities like `gzip` or `bzip2`. It is commonly used for creating backups and transferring files while preserving their directory structures and permissions.
Example (creating a tar archive and compressing with gzip):
bash
$ tar -czvf archive.tar.gz directory/
Example (extracting a tar archive):
bash
$ tar -xzvf archive.tar.gz
20. `zip` and `unzip`: Create and extract zip archives.
Explanation: The `zip` and `unzip` commands are used to create and extract zip archives.
1. `zip`: Create zip archives.
Explanation: The `zip` command is used to create compressed zip archives from files and directories.
Example (creating a zip archive):
bash
$ zip -r archive.zip directory/
2. `unzip`: Extract zip archives.
Explanation: The `unzip` command is used to extract files and directories from zip archives.
Example (extracting a zip archive):
bash
$ unzip archive.zip