unix commands part 2
unix commands part 2
-------------------------------
chmod: Changes file or directory permissions.
The chmod (change mode) command in Unix/Linux is used to change the file or
directory permissions.
Permissions control who can read, write, or execute a file or directory. Here's how
to use chmod effectively:
Read (r) – Allows reading the file or listing the directory contents.
Write (w) – Allows modifying the file or adding/removing files in a directory.
Execute (x) – Allows running the file (if it's a script or program) or entering the
directory.
ls -l filename
Output:
-rwxr-xr-- 1 user group 1234 Sep 19 12:00 filename
This means:
Owner (u): rwx (read, write, execute)
Group (g): r-x (read, execute)
Others (o): r-- (read only)
4 = Read (r)
2 = Write (w)
1 = Execute (x)
0 = No permission (-)
To set permissions, you sum the values for each category (user, group,
others):
Example:
This sets:
Owner: 7 (rwx)
Group: 5 (r-x)
Others: 5 (r-x)
In symbolic mode, you modify permissions by specifying the category (u, g, o), the
operation (+, -, =), and the permission (r, w, x).
+ to add permission
- to remove permission
= to set exact permission
--------------------------------
chown
----------------
chgrp
----------------
----------------------------------------------------------------------
top:
Displays real-time system processes and resource usage (CPU, memory).
top -d 5
2. top:
top - 14:03:05 up 10 days, 3:12, 2 users, load average: 0.35, 0.42, 0.45
Tasks: 154 total, 1 running, 153 sleeping, 0 stopped, 0 zombie
%Cpu(s): 3.2 us, 1.5 sy, 0.0 ni, 94.3 id, 0.6 wa, 0.0 hi, 0.4 si, 0.0 st
KiB Mem : 8095636 total, 2039320 free, 2020148 used, 403216 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 2176200 avail Mem
1. ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 22564 1860 ? Ss Sep01 0:00 /sbin/init
user1 2345 5.2 1.6 1048952 134232 ? Sl Sep15 25:36
/usr/lib/firefox/firefox
user2 3456 0.0 0.4 456789 34128 pts/0 R+ 14:30 0:01 ps aux
---------
2. ps -e or ps -A:
3. ps -u <username>:
ps -u user1
df
1. df: Displays disk space usage for all mounted file systems.
2. df -h: -Displays disk space in a human-readable format (e.g., KB, MB, GB).
-Check if the disk is running out of space.
-Check how much free space available
-----------------------------------------------
du --> Shows disk usage for files and directories (e.g., du -sh to see human-
readable sizes).
1. Shows only the total size of the specified directory in human readable format,
not the size of each subdirectory.
du -sh /path/to/directory
du -ah /path/to/directory
3. Display Disk Usage for All Files and Directories in current working directory:
du -h
--------------------------------------------------------
free
1. free - free command in Unix/Linux is used to display information about free and
used memory in the system,
including RAM and swap space.
2. -h option makes the output easier to read by using human-friendly units (KB, MB,
GB).
free -h
total used free shared
buff/cache available
Mem: 16Gi 8.1Gi 4.5Gi 1.0Gi 3.4Gi
6.5Gi
Swap: 2.0Gi 0B 2.0Gi
3. -s allows monitoring memory usage at regular intervals, which is useful for
tracking memory over time.
free -s 10
--Display Memory Usage Every 10 Seconds
---------------------------------------------------------------
uptime
Shows how long the system has been running and the system load averages.
uptime
output:
14:03:05 up 10 days, 3:12, 2 users, load average: 0.35, 0.42, 0.45
----------------------------------------------------------------
iostat
1. it provides detailed information about CPU and I/O device usage,
helping in system performance monitoring and troubleshooting.
iostat
-------------------------------
Linux 5.4.0-42-generic (hostname) 09/19/2024 _x86_64_ (4 CPU)
-x option gives more detailed device statistics, useful for in-depth analysis.
$ iostat -c
Linux 5.4.0-42-generic (hostname) 09/19/2024 _x86_64_ (4 CPU)
---------------------------------------------------
vmstat: Displays system performance including memory, CPU, and I/O usage.
$ vmstat
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
1 0 10240 10240 1024 2048 0 0 12 20 100 50 10 5 85 0
---------------------------------------------------
PROCESS MANAGEMENT
kill 1234
This sends the default SIGTERM signal to the process with PID 1234.
kill -9 1234
--------------------------------------------------------
killall firefox
This sends the default SIGTERM signal to all processes with the name
firefox.
killall -9 firefox
This sends a SIGKILL signal to all processes with the name firefox.
killall -i vim
This will prompt for confirmation before killing each vim process.
-------------------------------------------------------
nice / renice:
------------------
nohup:
The ping command is a common network utility used to test connectivity between your
computer and a remote host,
such as a server or another device on a network.
ping google.com
--------------------------------------------
ifconfig: Configures network interfaces.
-----------------------------------------------
netstat
netstat -a
netstat -t
-----------------------------------------------
-----------------------------------------------
nslookup:
who:
Displays information about who is currently logged in.
whoami:
Shows the current user.