Webdev

Linux Command Line Basics

1. ls (List)

$ ls
Documents  Downloads  Pictures

$ ls -l
total 12
drwxr-xr-x 2 user user 4096 May 15 10:00 Documents
drwxr-xr-x 2 user user 4096 May 15 10:00 Downloads
drwxr-xr-x 2 user user 4096 May 15 10:00 Pictures

2. pwd (Print Working Directory)

$ pwd
/home/user

3. cd (Change Directory)

$ pwd
/home/user
$ cd Documents
$ pwd
/home/user/Documents
$ cd ..
$ pwd
/home/user

4. mkdir (Make Directory)

$ mkdir new_folder
$ ls
Documents  Downloads  new_folder  Pictures

$ mkdir -p parent/child/grandchild
$ ls -R parent
parent:
child

parent/child:
grandchild

parent/child/grandchild:

5. mv (Move)

$ touch file.txt
$ ls
file.txt
$ mv file.txt newfile.txt
$ ls
newfile.txt
$ mv newfile.txt Documents/
$ ls Documents
newfile.txt

6. cp (Copy)

$ cp newfile.txt backup.txt
$ ls
backup.txt  newfile.txt

$ cp -r Documents Docs_backup
$ ls
backup.txt  Docs_backup  Documents  newfile.txt

7. rm (Remove)

$ rm backup.txt
$ ls
Docs_backup  Documents  newfile.txt

$ rm -r Docs_backup
$ ls
Documents  newfile.txt

8. touch

$ touch new_file.txt
$ ls -l
total 0
-rw-r--r-- 1 user user 0 May 15 11:00 new_file.txt
$ ln -s /path/to/file.txt link_to_file
$ ls -l
total 0
lrwxrwxrwx 1 user user 17 May 15 11:05 link_to_file -> /path/to/file.txt

10. clear

$ clear
# Terminal screen is now cleared

Remember to practice these commands regularly to become proficient in using the Linux command line interface!


More Linux Command Line Tools

Let’s expand our Linux command-line knowledge with these crucial tools:

1. cat (Concatenate)

$ cat file.txt
This is the content of file.txt

$ cat -n file.txt
     1  This is the content of file.txt

2. echo (Echo)

$ echo "Hello, world!"
Hello, world!

$ echo -e "Hello, \nworld!"
Hello,
world!

3. less (Less)

$ less large_file.txt
# You can now scroll through the file using the navigation keys.

4. man (Manual)

$ man ls
# Displays the manual page for the 'ls' command.

5. uname (Unix Name)

$ uname -a
Linux hostname 5.10.0-10-amd64 #21~20.04.1-Ubuntu SMP Fri Jul 1 17:06:08 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

$ uname -s
Linux

6. whoami (Who Am I)

$ whoami
user

7. tar (Tape Archive)

$ tar -czvf my_archive.tar.gz file1.txt file2.txt
# Creates a compressed archive named 'my_archive.tar.gz' containing 'file1.txt' and 'file2.txt'

$ tar -xzvf my_archive.tar.gz
# Extracts the contents of the archive.

8. grep (Global Regular Expression Print)

$ grep "hello" file.txt
# Displays lines in 'file.txt' that contain "hello".

$ grep -i "world" file.txt
# Displays lines containing "world" regardless of case.

9. head (Head)

$ head file.txt
# Displays the first 10 lines of 'file.txt'.

$ head -n 5 file.txt
# Displays the first 5 lines of 'file.txt'.

10. tail (Tail)

$ tail file.txt
# Displays the last 10 lines of 'file.txt'.

$ tail -n 5 file.txt
# Displays the last 5 lines of 'file.txt'.

Advanced Linux Command Line Tools

Let’s dive deeper into the world of Linux command-line tools with these advanced utilities:

1. diff (Difference)

$ diff file1.txt file2.txt
# Displays the differences between 'file1.txt' and 'file2.txt'.

$ diff -u file1.txt file2.txt
# Displays the differences in unified format.

2. cmp (Compare)

$ cmp file1.txt file2.txt
# Reports if 'file1.txt' and 'file2.txt' are identical.

$ cmp -s file1.txt file2.txt
# Silently reports if 'file1.txt' and 'file2.txt' differ.

3. comm (Common)

$ comm file1.txt file2.txt
# Displays lines unique to 'file1.txt', 'file2.txt', and common to both.

$ comm -1 file1.txt file2.txt
# Suppresses lines unique to 'file1.txt'.

4. sort (Sort)

$ sort file.txt
# Sorts the content of 'file.txt' alphabetically.

$ sort -n file.txt
# Sorts the content of 'file.txt' numerically.

5. export (Export)

$ export MY_VAR="Hello, world!"
# Exports the environment variable 'MY_VAR' with the value "Hello, world!".

$ echo $MY_VAR
Hello, world!

6. zip (Zip)

$ zip my_archive.zip file1.txt file2.txt
# Zips 'file1.txt' and 'file2.txt' into 'my_archive.zip'.

$ zip -r my_archive.zip directory/
# Recursively zips the contents of 'directory/' into 'my_archive.zip'.

7. unzip (Unzip)

$ unzip my_archive.zip
# Unzips the contents of 'my_archive.zip' to the current directory.

$ unzip -d /path/to/directory my_archive.zip
# Unzips the contents of 'my_archive.zip' to '/path/to/directory'.

8. ssh (Secure Shell)

$ ssh user@remote_host
# Establishes a secure connection to 'remote_host' as 'user'.

$ ssh -p 2222 user@remote_host
# Establishes a secure connection to 'remote_host' on port 2222.

9. service (Service)

$ service httpd start
# Starts the 'httpd' service.

$ service httpd stop
# Stops the 'httpd' service.

10. ps (Process Status)

$ ps -a
# Displays all active processes.

$ ps -u user
# Displays processes owned by 'user'.

System Commands

1. kill (Kill a Process)

$ kill -9 1234
# Immediately terminates the process with PID 1234.

$ kill -TERM 1234
# Gracefully stops the process with PID 1234.

$ kill -l
# Lists all available signals.

To find the PID, you can use commands like ps, pgrep, or pidof.

2. killall (Kill Processes by Name)

$ killall firefox
# Terminates all Firefox processes with the default SIGTERM signal.

$ killall -9 firefox
# Immediately terminates all Firefox processes.

This command is useful for killing multiple processes with the same name without needing to specify each PID.

3. df (Display Disk Filesystem Information)

$ df -h
# Displays disk space usage in a human-readable format.

$ df -T
# Displays the type of each file system along with usage information.

This command helps you understand how much disk space is used and available on each mounted file system.

4. mount (Mount File Systems)

$ mount /dev/sdb1 /mnt
# Mounts the device /dev/sdb1 to the /mnt directory.

$ mount -t ext4 /dev/sdb1 /mnt
# Mounts an ext4 file system from /dev/sdb1 to /mnt.

This command is essential for making file systems available for use by the operating system.

5. chmod (Change File Permissions)

$ chmod 755 file.txt
# Sets the permissions to rwxr-x (owner has full permissions, group and others have read and execute).

$ chmod u+x file.txt
# Adds execute permission for the owner.

This command is used to manage access control for files and directories.

6. chown (Change Ownership)

$ chown user file.txt
# Changes the owner of 'file.txt' to 'user'.

$ chown user:group file.txt
# Changes the owner and group of 'file.txt' to 'user' and 'group', respectively.

This command is used to manage file and directory ownership.

7. ifconfig (Display Network Interfaces)

$ ifconfig eth0
# Displays information about the eth0 network interface.

For modern systems, use the ip command instead:

$ ip addr show
# Displays detailed information about all network interfaces.

8. traceroute (Trace Network Hops)

$ traceroute example.com
# Traces the network path to example.com.

$ traceroute -n example.com
# Traces the network path to example.com without performing DNS lookups.

This command helps in diagnosing network issues by showing the path packets take to reach a destination.

9. wget (Download Files)

$ wget https://example.com/file.txt
# Downloads the file from the specified URL.

$ wget -b https://example.com/file.txt
# Downloads the file in the background.

This command is useful for downloading files from the web directly from the command line.

10. ufw (Uncomplicated Firewall)

$ ufw enable
# Enables the firewall.

$ ufw allow 80
# Allows HTTP traffic on port 80.

$ ufw deny 22
# Denies SSH traffic on port 22.

This command is used to manage firewall rules in Ubuntu and other compatible systems.

11. iptables (Base Firewall)

$ iptables -A INPUT -p tcp --dport 80 -j ACCEPT
# Allows HTTP traffic on port 80.

$ iptables -D INPUT -p tcp --dport 80 -j ACCEPT
# Deletes the rule allowing HTTP traffic on port 80.

$ iptables -L
# Lists all the current firewall rules.

This command is used to manage the underlying firewall rules and is often used by other firewall utilities like ufw.

Understanding and using these commands will help you manage various aspects of your Linux system, from process management and file system operations to network diagnostics and firewall configuration.


Package Managers

1. apt (Advanced Package Tool)

$ sudo apt update
# Updates the package list.

$ sudo apt install package_name
# Installs the specified package.

$ sudo apt remove package_name
# Removes the specified package.

2. pacman (Package Manager)

$ sudo pacman -Syu
# Synchronizes the package database and updates the system.

$ sudo pacman -S package_name
# Installs the specified package.

$ sudo pacman -R package_name
# Removes the specified package.

3. yum (Yum)

$ sudo yum update
# Updates all installed packages.

$ sudo yum install package_name
# Installs the specified package.

$ sudo yum remove package_name
# Removes the specified package.

4. rpm (RPM Package Manager)

$ sudo rpm -i package_name.rpm
# Installs the specified RPM package.

$ sudo rpm -e package_name
# Removes the specified package.

$ rpm -q package_name
# Queries the package database for information about the specified package.

System and User Management

5. sudo (Superuser Do)

$ sudo apt update
# Executes the 'apt update' command with superuser privileges.

$ sudo useradd new_user
# Adds a new user with superuser privileges.

6. cal (Calendar)

$ cal
# Displays the current month's calendar.

$ cal 2023
# Displays the calendar for the entire year 2023.

7. alias (Alias)

$ alias ll='ls -l'
# Creates an alias 'll' for the 'ls -l' command.

$ alias gs='git status'
# Creates an alias 'gs' for the 'git status' command.

8. dd (Disk Dump)

$ sudo dd if=/path/to/image.iso of=/dev/sdX bs=4M
# Creates a bootable USB stick from an ISO image.

9. whereis (Where Is)

$ whereis ls
# Displays the path to the 'ls' binary and its manual page.

10. whatis (What Is)

$ whatis ls
# Displays a brief description of the 'ls' command.

11. top (Top)

$ top
# Opens the top utility, displaying system processes and their resource usage.

12. useradd (User Add)

$ sudo useradd -m new_user
# Adds a new user with a home directory.

13. usermod (User Modify)

$ sudo usermod -aG sudo new_user
# Adds the user to the 'sudo' group.

$ sudo usermod -s /bin/bash new_user
# Changes the user's shell to bash.

14. passwd (Password)

$ sudo passwd new_user
# Sets a new password for the specified user.

$ passwd
# Changes the password for the current user.

15. systemctl (System Control)

$ sudo systemctl start httpd
# Starts the 'httpd' service.

$ sudo systemctl stop httpd
# Stops the 'httpd' service.

$ sudo systemctl enable httpd
# Enables the 'httpd' service to start automatically at boot.

16. journalctl (System Journal)

$ journalctl -f
# Displays the system journal and follows the log output.

$ journalctl -u httpd
# Displays the journal entries related to the 'httpd' service.

17. free (Free Memory)

$ free -h
# Displays memory usage in a human-readable format.

18. du (Disk Usage)

$ du -h /home
# Displays disk usage for the '/home' directory in a human-readable format.

$ du -a /home
# Displays disk usage for all files and directories within '/home'.

These commands are essential for managing packages, system resources, and user accounts on a Linux system. Practice using them to become proficient in administering your environment.


Previous Page