853 words
4 minutes
Linux Command
2025-05-01
2025-04-25

1. Displaying Disk Usage in Megabytes and Gigabytes#

Linux’s df (disk free) command shows mounted filesystem disk space usage. By default it prints in 1K blocks; with -h it uses human-readable units (KB, MB, GB):

# Show sizes with automatic units (e.g. 1K, 234M, 2G)
df -h

# Show sizes in raw 1K blocks
df

# Force output in megabytes (M) or gigabytes (G)
df -BM     # outputs sizes in megabytes
df -BG     # outputs sizes in gigabytes

Alternatively, use du (disk usage) to check a specific directory’s size:

# Recursively summarize sizes, human-readable
du -h /path/to/dir

# Show only the total for that directory
du -sh /path/to/dir

2. Managing File Permissions with chmod#

Unix file permissions control read, write, and execute access. To make a script executable for its owner:

chmod u+x example.sh
SymbolMeaning
ufile owner (user)
gowning group
oothers
+add permission
-remove permission
rread
wwrite
xexecute

E.g., chmod ug=rw,o=r file.txt → owner & group read/write; others read only.


3. Top 50 Linux Commands You Should Know#

File & Directory Operations#

  1. ls – list files & directories

  2. pwd – print working directory

  3. cd – change directory

  4. mkdir – create directory

  5. rm – remove files/directories

  6. cp – copy files/directories

  7. mv – move or rename files

  8. touch – create empty file

  9. ln – create (symbolic) links

  10. find – search for files by name, size, date, etc.

Viewing & Editing Files#

  1. cat – concatenate & display file content

  2. less – view file content page by page

  3. head – show first N lines

  4. tail – show last N lines

  5. grep – search text via patterns

  6. diff – compare file differences

  7. cmp – byte-by-byte file comparison

  8. sort – sort file lines

  9. wc – count lines/words/bytes

  10. sed – stream editor for text transformation

System Information & Process Management#

  1. uname -a – kernel & system info

  2. whoami – current username

  3. top – real-time process & resource viewer

  4. ps aux – snapshot of processes

  5. kill, killall – terminate processes

  6. df – disk filesystem usage

  7. du – directory usage (see above)

  8. free -h – memory usage

  9. uptime – system uptime & load

  10. htop – interactive process viewer (if installed)

Networking & Remote Access#

  1. ifconfig (or ip addr) – network interfaces

  2. ping – test network connectivity

  3. traceroute – trace packet route

  4. ssh – secure shell access

  5. scp – secure copy over SSH

  6. wget, curl – download files over HTTP/FTP

  7. netstat (or ss) – sockets & network stats

  8. iptables, ufw – firewall rules

  9. route (or ip route) – view/manipulate routing

  10. nslookup, dig – DNS queries

Package Management & Updates#

  1. apt, yum, dnf, pacman – install/remove packages

  2. sudo – execute a command with superuser privileges

  3. systemctl – manage systemd services

  4. service – start/stop SysV services

  5. journalctl – view systemd logs

  6. cron, at – schedule tasks

  7. alias – create command shortcuts

  8. env, export – set environment variables

  9. dd – low-level data copying (e.g. USB images)

  10. whereis, which, whatis – locate commands


4. Terminal Multiplexing with Screen#

GNU Screen lets you run multiple shells inside one terminal, detach, and resume:

# Install on Debian/Ubuntu
sudo apt-get install screen

# Start a new session
screen

# Detach (leave running in background)
Ctrl-a d

# List sessions
screen -ls

# Reattach to a session
screen -r <session_name>

# Create a new window inside screen
Ctrl-a c

# Next/previous window
Ctrl-a n   Ctrl-a p

# List windows
Ctrl-a "

# Split windows (horizontal)
Ctrl-a S

# Close current window
Ctrl-a k

# Rename window
Ctrl-a A

# Kill all screen sessions
killall screen

5. Resolving Default Gateway Conflicts#

Having more than one default gateway (e.g. both eth0 and wlan0) causes routing errors. Choose one interface for your internet traffic; remove the other’s gateway:

# Remove the default route via eth0
sudo ip route del default via 192.168.1.1 dev eth0

Verify with:

ip route show

6. Mounting USB Drives Permanently#

One-off Mount#

# /etc/fstab entry example:
/dev/sda1   /home/pi/usb   ext4   defaults   0   0
# Temporarily mount:
sudo mount /dev/sda1 /home/pi/usb

Bind-Mount (e.g. Nextcloud data folder)#

sudo mount --bind /home/pi/usb /portainer/Files/AppData/Config/Nextcloud/Data/pi/files/usb

Permanent Mount via UUID#

  1. Find the UUID:

    sudo blkid /dev/sda1
  2. Add to /etc/fstab:

    UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx  /mnt/external  ext4  defaults,uid=pi,gid=pi,nofail  0  0
  3. Apply:

    sudo mount -a

Adjust ownership for easy access:

sudo chown -R pi:pi /mnt/external

7. Refreshing Arch Linux Mirrors#

On Arch Linux, ensure you have the fastest mirrors:

# Update mirror list with the 5 fastest servers
sudo pacman-mirrors -f 5

# Then update your system
sudo pacman -Syu

8. Starting Docker Service#

If Docker isn’t already running, use:

# Start Docker daemon
sudo systemctl start docker

# Enable it at boot
sudo systemctl enable docker

# Check status
sudo systemctl status docker

9. Working with External Hard Drives#

9.1. Connect, Partition & Format#

  1. Detect drives

    sudo lsblk
  2. Partition (if needed)

    sudo fdisk /dev/sda
    # Inside fdisk: ‘n’ new, ‘d’ delete, ‘w’ write
  3. Format partition

    # ext4 for Linux
    sudo mkfs.ext4 /dev/sda1
    
    # exFAT for cross-platform
    sudo mkfs.exfat /dev/sda1
  4. Create mount point & mount

    sudo mkdir -p /mnt/external
    sudo mount /dev/sda1 /mnt/external
    sudo chown -R pi:pi /mnt/external

9.2. Permanent Mount#

See section 6 above.


10. Configuring HDD Spin-Down with hd-idle#

Prolong hardware life and save power by spinning down idle drives:

  1. Install

    sudo apt-get install hd-idle
  2. Configure
    Edit /etc/default/hd-idle:

    # Spin down after 180 seconds idle
    HD_IDLE_OPTS="-i 180 -l /var/log/hd-idle.log"
  3. Restart service

    sudo systemctl restart hd-idle
    # or for SysV:
    sudo service hd-idle restart

11. Monitoring Disk Activity & Background Services#

  1. List open files on a mount

    sudo lsof | grep /home/pi/USB
  2. I/O top

    sudo apt-get install iotop
    sudo iotop
  3. Running services

    systemctl list-units --type=service --state=running
  4. Process snapshot

    ps aux
    # or top for live view
    top
  5. System logs

    dmesg | less
    journalctl -xe

12. Optimizing Samba (SMB) Shares#

If Samba’s smbd keeps accessing your USB mount, disable kernel change notifications:

  1. Edit share config in /etc/samba/smb.conf:
    [USB]
    path = /home/pi/USB
    browseable = yes
    read only = no
    kernel change notify = no
  1. Restart or stop Samba:
sudo systemctl restart smbd
# If you no longer need sharing:
sudo systemctl stop smbd
Linux Command
https://jamshidzadeh.ir/posts/study/linux-command/
Author
Ali Jamshidzadeh
Published at
2025-05-01