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>
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
Soft and Hard link
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
~$ 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
).
~$ uptime
: Displays how long the system has been running.-p
: Displays in a human-readable way.
~$ date
: Displays the current date and time.~$ who
and~$ whoami
:~$ who
: Displays information about all users currently logged into the system.~$ whoami
: Displays the current logged-in user.
~$ id
: Displays information about a user’s identity, including user ID (UID), group ID (GID), and group memberships.~$ 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 Representation | Numeric Representation | Owner (User) | Group | Others | Description |
rwxrwxrwx | 777 | read, write, execute | read, write, execute | read, write, execute | Full permissions for everyone |
rw-rw-r-- | 664 | read, write | read, write | read | Owner and group have read/write access; others have read-only access |
rwxr-xr-x | 755 | read, write, execute | read, execute | read, execute | Owner has full permissions; group and others have read/execute access |
rw-r--r-- | 644 | read, write | read | read | Owner has read/write access; group and others have read-only access |
rwx------ | 700 | read, write, execute | none | none | Only the owner has full access |
r-xr-xr-x | 555 | read, execute | read, execute | read, execute | Owner, group, and others have read/execute access |
r--r--r-- | 444 | read | read | read | Read-only access for everyone |
---x--x--x | 111 | execute | execute | execute | Only 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 ofping
andtraceroute
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 sensitiveg
globallys
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