Linux Commands

Linux Commands

Essential Linux commands includes.

Directories

List items.

List using ~$ ls command. Flags -a -l -r -t

  • -t are used to sort files by time.

  • -a Is used to display all files, including hidden files and folders.

  • -r reverses the list of files.

  • -l lists details of all files.

Change directory

~$ cd <path/to/folder>
Go back ~$ cd ..

To check the present working directory, use ~$ pwd.

Create directory

~$ mkdir <foldername>

Delete

  • Delete file: ~$ rm <filename>

  • Delete empty folder: ~$ rm <dir>

  • Delete non-empty folder: ~$ rm -r <dir>

Print

Use ~$ cat <filename>. Example: ~$ cat index.py.

Print some lines: ~$ head -n 5 index.py (prints 5 lines from the head). For the last lines, use ~$ tail -n 5 index.py.

Copy or Move

~$ cp [OPTIONS] SOURCE DESTINATION
Flags: -r, -v.

~$ mv [OPTIONS] SOURCE DESTINATION

Flags:

  • -v Verbose: Displays detailed information about the move operation.

  • -n Prevents overwriting files in the destination.

Rename

~$ mv oldname.txt newname.txt

In Linux, a hard link refers to a physical copy of a file, while a soft link (or symbolic link) refers to a reference or pointer to the original file.

  • Soft copy:~$ ln -s path/orignal.sh copy.sh

  • Hard copy: ~$ ln path/orignal.sh copy.sh

Difference

Check the difference between two files:

~$ diff first.sh second.sh

System Level Commands

  1. ~$ uname: Displays system information.

    • -a: Shows all information.

    • -r: Displays the kernel version.

    • -n: Displays the system's network hostname.

    • -m: Shows the machine's hardware architecture (e.g., x86_64).

  2. ~$ uptime: Displays how long the system has been running.

    • -p: Displays in a human-readable way.
  3. ~$ date: Displays the current date and time.

  4. ~$ who and ~$ whoami:

    • ~$ who: Displays information about all users currently logged into the system.

    • ~$ whoami: Displays the current logged-in user.

  5. ~$ id: Displays information about a user’s identity, including user ID (UID), group ID (GID), and group memberships.

  6. ~$ shutdown and ~$ reboot: Used to shut down or reboot the system.

User and Group Management

User

  • ~$ sudo useradd -m <newuser>: Using -m will create a home directory for the user.

  • ~$ sudo passwd <newuser>: Set the password for the user.

  • ~$ sudo userdel <username>: Delete the user.

Group

  • ~$ sudo groupadd <newgroup>: Creates a new group.

  • ~$ sudo gpasswd -a <user> <newgroup>: Adds a user to the group.

  • ~$ sudo groupdel <groupname>: Deletes a group.

File Permissions

Inspect Permissions of a File

  • ~$ ls -l: Use this command to inspect the permissions of files and folders.
Symbolic RepresentationNumeric RepresentationOwner (User)GroupOthersDescription
rwxrwxrwx777read, write, executeread, write, executeread, write, executeFull permissions for everyone
rw-rw-r--664read, writeread, writereadOwner and group have read/write access; others have read-only access
rwxr-xr-x755read, write, executeread, executeread, executeOwner has full permissions; group and others have read/execute access
rw-r--r--644read, writereadreadOwner has read/write access; group and others have read-only access
rwx------700read, write, executenonenoneOnly the owner has full access
r-xr-xr-x555read, executeread, executeread, executeOwner, group, and others have read/execute access
r--r--r--444readreadreadRead-only access for everyone
---x--x--x111executeexecuteexecuteOnly execute access for everyone
  • ~$ chmod 400 <file>: Change file permissions.

  • ~$ chown <user> <file>: Change the owner of the file.

  • ~$ chgrp <groupname> <file>: Change the group of the file or folder.

Networking

  • ~$ netstat Displays active network connections, routing tables, and network interface statistics

  • ~$ ping <domain or ip>

  • ~$ ifconfig Displays or configures network interfaces

  • ~$ traceroute vs ~$ tracepath Tracks the route packets take to reach a destination

  • ~$ mtr <domain or ip> Combines the functionality of ping and traceroute for real-time network diagnostics

  • ~$ nslookup <domain or ip> Queries DNS servers to obtain domain name

  • ~$ hostname Displays or sets the hostname of the system

  • ~$ iwconfig Displays wireless network interfaces

  • ~$ arp table for mapping IP addresses to MAC addresses

  • ~$ dig Provides detailed DNS query information

  • ~$ whois Retrieves domain registration details

  • ~$ nmap <domain or ip> scans networks to discover hosts, services, and open ports

  • ~$ wget download files from the web

  • ~$ watch runs a command repeatedly at specified intervals

  • ~$ curl test API

awk, sed, grep

awk

  • ~$ awk '{print}' index.py print the file

  • ~$ awk '{print $1, $2}' index.py $<no> no means column no

  • ~$ awk '/word/ {print}' index.py /<filterword>/ filter rows concerning filter word

  • ~$ awk '/word/ {count++} END {print count}' index.py made a var count and incremented it every time a filter word was found in a row.

sed

  • ~$ sed -n '/INFOp/' index.log print rows containing INFO word

  • ~$ sed -i 's/INFO/DATA/g' index.log replace INFO with DATA -i case sensitive g globally s means substring

  • ~$ sed -n -e '/INFO=/' -e '/INFOp/' index.log print count of rows having INFO substring and print rows.

grep

  • ~$ grep INFO index.log print rows having INFO word.

  • ~$ grep -i Info index.log not case sensitive.

  • ~$ grep -i -c INFO index.log print rows including counts.

Example:

~$ ps aux | grep ubuntu