Essential Linux Commands: A Comprehensive Guide with Examples

  1. pwd: Print working directory

                    pwd

Example output:

                     /home/user/documents

ls (List Directory Contents):

·         Displays the files and directories in the current directory.

·         Example output: 

Example output:

                      file1.txt  file2.txt  directory1

  cd: Change directory

                      cd directory1

  mkdir: Make directory

    Creates a new directory.

                      mkdir new_directory

  rmdir: Remove directory

                      rmdir directory_to_remove

  rm: Remove

                      rm file_to_remove.txt

  cp: Copy

 Copies files or directories from one location to another.

                     cp file_to_copy.txt destination_directory/

  mv: Move

 Moves files or directories from one location to another.

  mv file_to_move.txt new_location/

  touch: Create an empty file

                      touch new_file.txt

  cat: Concatenate and display files

    Displays the content of a file

                      cat file.txt

  grep: Search

    Searches for a specific pattern in files

                  grep "pattern" file.txt

  chmod: Change mode

        Changes the permissions of a file or directory.

                      chmod 755 file.txt

  chown: Change owner

        Changes the owner of a file or directory

                     chown user:group file.txt

  tar: Tape Archive

                Used to create and manipulate tar archives

                      tar -czvf archive.tar.gz directory_to_compress/

  sudo: Superuser do

                Allows a permitted user to execute a command as the superuser or another user.

                                 sudo apt-get update

  df: Disk Free

Displays information about available disk space on the file system.

$ df -h
Filesystem    Size  Used Avail Use% Mounted on
devtmpfs       863M     0  863M   0% /dev
tmpfs          893M  168K  893M   1% /dev/shm
tmpfs          893M  9.5M  883M   2% /run
tmpfs          893M     0  893M   0% /sys/fs/cgroup
/dev/map[...]   17G  6.9G   11G  41% /
/dev/sda1     1014M  255M  760M  26% /boot
tmpfs          179M  120K  179M   1% /run/user/1000

  du: Disk Usage

The du -sh command is used to display the disk usage of files and directories in a human-readable format. Here's an example of its output:

                  # du -sh *

172M    20230828_0700

176M    20230828_0701

175M    20230828_0702

  top: Display Linux processes

                      top

A screen shot of a computer

Description automatically generated

  ps: The ps aux command is used to display information about currently running processes. Here's an example of its output:

$ ps aux
USER       PID  %CPU %MEM   VSZ   RSS   TTY  STAT START   TIME COMMAND
root         1   0.0  0.0 163692  6448   ?    Ss    Jan01   0:01 /sbin/init
root         2   0.0  0.0      0     0   ?    S     Jan01   0:00 [kthreadd]
root         4   0.0  0.0      0     0   ?    I<    Jan01   0:00 [kworker/0:0H
 

Explanation of columns:

  • USER: The username of the process owner.
  • PID: The process ID (unique identifier) of the process.
  • %CPU: The percentage of CPU time used by the process.
  • %MEM: The percentage of memory used by the process.
  • VSZ: The virtual memory size of the process in kilobytes.
  • RSS: The resident set size (actual memory usage) of the process in kilobytes.
  • TTY: The terminal associated with the process (or ? if not applicable).
  • STAT: The status of the process (e.g., S for sleeping, R for running).
  • START: The start time of the process.
  • TIME: The total CPU time consumed by the process.
  • COMMAND: The command or executable associated with the process.

This output provides detailed information about each running process on the system, including the user who owns the process, resource usage, status, and command being executed.

 

  kill: Terminate processes

The kill command is used to send signals to processes, allowing you to terminate or manipulate their behavior. Here are some common usage examples:

Terminate a Process by PID:

kill PID

Terminate a Process by Name:

pkill process_name

 

  ssh: Secure Shell

SSH (Secure Shell) is a protocol used for securely accessing and managing remote servers. Here are some common SSH commands and their usage examples:

                Connect to a Remote Server

                      ssh username@hostname

Specifying a Port:

                    ssh -p 22 user@example.com

   scp: Secure Copy to remote server

                SCP (Secure Copy Protocol) is a command-line utility for securely transferring files between hosts using SSH. Here are some common SCP commands and their usage examples:

 
Copy a file from the local system to a remote server.
 
scp /path/to/local/file username@hostname:/path/to/destination/directory

Example: Copy a file named "file.txt" from the local system to the remote server.

scp /path/to/file.txt user@example.com:/home/user/documents/

wget: Web Get

                      wget http://example.com/file.txt

  curl: Client URL

                                curl -O http://example.com/file.txt

  tail: Display the last part of a file

The tail command is used to display the last part of a file, typically used for viewing log files or monitoring real-time changes in files. Here are some common tail commands and their usage examples:

Display Last 10 Lines of a File:

                    tail logfile.txt

Display Last N Lines of a File:

                    tail -n 20 logfile.txt

Output Continuously (Follow Mode):

  • Output the contents of a file continuously as new lines are appended (similar to tail -f).
tail -f access.log
 

  head: Display the first part of a file

The head command is used to display the first part of a file, typically used for viewing the beginning of files. Here are some common head commands and their usage examples:

Display First 10 Lines of a File:

                    head example.txt

Example: Display the first 20 lines of a file named "example.txt".

                    head -n 20 example.txt

 find: Search for files and directories

                The find command in Linux is used to search for files and directories within a specified directory hierarchy based on various criteria. Here are some common find commands and their usage examples:

                              find /path/to/search -name "filename"

  awk: Text processing tool

                     awk '/pattern/ {print}' file.txt

  sed: Stream editor

  sed 's/old_string/new_string/' file.txt

 

ip a : The ip a command is used to display network interface information, including IP addresses assigned to each interface. Here's how to use it and an example of its output:

  Example output:

$ ip a
 
: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
     inet 127.0.0.1/8 scope host lo
        valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
     inet 192.168.1.100/24 brd 192.168.1.255 scope global dynamic eth0
        valid_lft 86363sec preferred_lft 86363sec
     inet6 fe80::dcba:1234:5678:abcd/64 scope link 
        valid_lft forever preferred_lft forever
3: wlan0: <BROADCAST,MULTICAST> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
     link/ether 12:34:56:78:90:ab brd ff:ff:ff:ff:ff:ff

 

ping: The ping command is used to test the reachability of a host on a network

                     ping google.com

traceroute: Trace Route - traces the route taken by packets from the source to the destination.

                     traceroute google.com

netstat: Network Statistics - displays network connections, routing tables, interface statistics, masquerade connections, and multicast memberships.

  netstat -a

 route: Display or manipulate the IP routing table.

                     route -n

  hostname: Show or set the system's host name.

                      hostname

  date: Display or set the system date and time.

                      date

  cal: Display a calendar.

                      cal

  history: Display the command history list.

                      history

  man: Display the manual page of a command.

       man ls

  file: Determine file type.

                     file myfile.txt

  chroot: Run a command with a different root directory.

                      chroot /newroot /bin/

  crontab: Schedule periodic background work to be executed.

                     crontab -e

  at: Execute commands at a specified time.

                      echo "ls -l" | at now + 1 hour

  systemctl: Control the systemd system and service manager.

                      systemctl start sshd

  journalctl: Query and display messages from the journal.

                      journalctl -u sshd

  useradd: Create a new user or update default new user information.

                     useradd newuser

  usermod: Modify a user account.

                      usermod -aG groupname username

  userdel: Delete a user account and related files.

                      userdel username

  groupadd: Create a new group.

                     groupadd newgroup

  groupmod: Modify a group definition on the system.

                      groupmod -n newgroup oldgroup

  groupdel: Delete a group from the system.

                     groupdel oldgroup

  visudo: Edit the sudoers file safely.

                      visudo

  lsof: List open files and the processes that opened them.

                     lsof -i :port_number

 uptime: Show how long the system has been running.

                      uptime

  watch: Execute a program periodically, showing output fullscreen.

                      watch -n 1 uptime

  chattr: Change file attributes on a Linux file system.

                     chattr +i myfile.txt

  lsattr: List file attributes on a Linux second extended file system.

                      lsattr myfile.txt

  watchdog: Manage software or hardware watchdog timers.

                      watchdog -t 60 /dev/watchdog

  hostid: Print the numeric identifier for the current host.

                       hostid

  setuid: Run a program with the permissions of the owner.

                     chmod u+s /path/to/program

  setgid: Run a program with the permissions of the group.

                      chmod g+s /path/to/program

  dmesg: Print or control the kernel message buffer.

                      dmesg | grep -i error

  last: Show listing of last logged in users.

                      last -n 5

  ldconfig: Configure dynamic linker runtime bindings.

                    ldconfig -v

 

 

أحدث أقدم