Essential Linux Commands
-
Creating and Reading a Variable
- Create a variable: To create a variable in Linux, you simply assign a value to it:
MY_VAR="Hello, World!" - Read the variable: To read or print the value of the variable, use the
echocommand and prefix the variable name with a$sign:echo $MY_VAR
- Create a variable: To create a variable in Linux, you simply assign a value to it:
-
File and Directory Management
-
Listing Files and Directories
-
ls: List directory contents. -
ls -la: Shows all files, including hidden files, with detailed information. -
file -s file.txt: Displays the file type offile.txt. -
tar: is used for compressing (with gzip or bzip2), and extracting archive files.tar -cvf backup.tar /home/user/mydata/: Creates a tar archive named backup.tar of the directory /home/user/mydata.tar -xvf archive_name.tar: Extracts the contents of archive_name.tar.tar -czvf archive_name.tar.gz /path/to/directory/: Creates a gzip-compressed tar archive.tar -tvf archive_name.tar: Lists the contents of archive_name.tar without extracting.
Note:
c: Create a new archive,z: Indicates that the file is gzip compressed.x: Extract files from the archive.v: Verbose mode.f: Specifies the file name.
-
-
Changing Directories
cd: Change the current directory.cd /path/to/directory: Moves into the specified directory.
-
Creating and Removing Directories
mkdir: Create a new directory.mkdir new_directory: Creates a directory namednew_directory.rmdir: Remove an empty directory.rmdir empty_directory: Removes the specified empty directory.
-
Removing Files and Directories
rm: Remove files or directories.rm file.txt: Removesfile.txt.rm -r: Recursively remove a directory and its contents.rm -r directory_name: Deletesdirectory_nameand all its contents.
-
Copying and Moving Files
cp: Copy files or directories. Ex-cp source.txt destination.txt>> Copiessource.txttodestination.txt.mv: Move or rename files or directories. Ex-mv oldname.txt newname.txt>> Renamesoldname.txttonewname.txt.sed: Stands forStream Editoris designed for parsing and transforming text in a non-interactive way.sed 's/old_text/new_text/g' file.txt: Substitute text in a filesed -i 's/old_text/new_text/g' file.txt: In-place substitutionsed '/pattern/d' file.txt: Delete lines that match a patternsed 's/oldname.txt/newname.txt/' file.txt: Rename a file
-
Viewing and Editing Files
cat: Display the contents of a file. Ex-cat file.txt>> Shows the content offile.txt.nano: Open a file in the Nano text editor. Ex-nano file.txt>> Allows editing offile.txt.vim: Open a file in the Vim text editor.vimstands for Vi IMproved. It is an enhanced version of vi editor. Ex -vim file.txt>> Allows editing offile.txt.
-
File Permissions and Ownership
chmod: Change file permissions. Ex-chmod 755 script.sh>> Sets permissions forscript.sh.chown: Change file owner. Ex-chown user file.txt: Changes the owner offile.txttouser.chgrp: Change group ownership. Ex -chgrp groupname file.txt>> Changes the group offile.txttogroupname.
-
Mounting Filesystems
mount: Attach a filesystem to a mount points (directory/folders).mount /dev/xvdf /mnt/mydrive: Mounts the device/dev/xvdfat/mnt/mydrive.umount: Detach a filesystem from a directory.umount /mnt/mydrive: Unmounts the filesystem mounted at/mnt/mydrive.
Note : Every storage device or partition connected to a Linux system is represented as a file in the
/dev/directory. For example: If we plug in an SSD, Linux creates something like/dev/sdafor that SSD./dev/xvdfspecifically represents a storage device in Linux, typically used for virtual drives in cloud environments, such as AWS, where it signifies a virtual disk that can be mounted and accessed like a physical hard drive.
-
-
System Information and Management
-
Checking System Info
uname: Display system information.uname -a: Shows detailed information about the kernel and OS.
-
Monitoring Disk
df: df stands for "disk free". This command provides information about the disk space usage of all mounted file systems.df -k: Provides information about the disk space usage of all mounted file systems explicitly in kilobytes (1K blocks).df -h: Provides information about the disk space usage of all mounted file systems in a human-readable format, with the output shown in KB, MB, GB, or TB, depending on the size of the disk space.du: du stands for "disk usage". This command provides information about the space usage of files and directories.du -sh: Shows the size of each file and directory in the current directory.lsblk: Displays a detailed list of block devices, including their size, type, mount points, and more. Note` : lsblk and df -k are often used together: lsblk to understand disk structure, and df -k to check disk space usage.
-
Checking Memory Usage
free: Display memory usage.free -m: Shows memory usage in megabytes.
-
System Load and Processes
top: Provides real-time system information including memory usagehtop: Enhanced version of top (requires installation).htop: Interactive process viewer with an improved interface.
-
Process Management
ps: Display current processes.ps aux: Shows all running processes with details.kill: Terminate a process by PID.kill 1234: Terminates the process with PID 1234.pkill: Kill processes by name.pkill firefox: Terminates all processes namedfirefox.
-
-
Networking Commands
-
Checking Network Configuration
ifconfig** or **ip a: Display network interface information.ifconfig: Shows the configuration of network interfaces.ping: Check connectivity to another host.ping example.com: Sends packets toexample.comto test connectivity.
-
Transferring Files
scp: Securely copy files between hosts.scp file.txt user@remote\_host:/path/to/destination: Copiesfile.txtto a remote host.rsync: Efficiently sync files and directories between two locations.rsync -avz source/ user@remote\_host:/path/to/destination: Synchronizes files fromsource/to a remote host.
-
Downloading Files
curl: Transfer data from or to a server.curl -O http://example.com/file.txt: Downloadsfile.txtfrom the specified URL.wget: Retrieve files from the web.wget http://example.com/file.txt: Downloadsfile.txtfrom the specified URL.
-
-
User Management
-
Viewing Current User
whoami: Display the current logged-in user.whoami: Shows the username of the current user.
-
User and Group Management
adduser: Create a new user.sudo adduser newuser: Adds a new user callednewuser.usermod: Modify a user’s properties.sudo usermod -aG groupname username: Addsusernametogroupname.deluser: Remove a user.sudo deluser username: Deletes the user namedusername.
-
-
File Compression and Archiving
-
Compressing Files
tar: Create or extract compressed archives.tar -czvf archive.tar.gz directory/: Creates a compressed archive ofdirectory.unzip: Extract compressed ZIP files.unzip file.zip: Extracts the contents offile.zip.gzip: Compress a file.gzip file.txt: Compressesfile.txttofile.txt.gz.
-
-
File Searching and Finding
-
`searching for Files
find: Search for files and directories.find /path/to/search -name "\*.txt": Finds all.txtfiles in the specified path.
-
`searching for Text in Files
grep: Search for specific text within files.- **
grep "search\_term" file.txt``** : Searches forsearch_terminfile.txt`. grep -r: Recursively search for text in a directory.grep -r "search\_term" /path/to/directory: Searches forsearch_termin all files within the specified directory.
-
-
Disk and Partition Management
-
artitioning Devices
sudo fdisk /dev/sda: Opens the partitioning tool to create, modify, or delete partitions on the device.sudo parted /dev/sda: Launches the partitioning tool for larger disks and modern partitioning methods.
-
Creating File Systems
mkfs: Create a file system on a partition or device.sudo mkfs.ext4 /dev/sda1: Creates an ext4 file system on the partition /dev/sda1.sudo mkfs.xfs /dev/sda1: Creates an xfs file system on the partition /dev/sda1.sudo mkfs.vfat /dev/sda1: Creates a FAT32 file system on the partition /dev/sda1.
-
Mounting File Systems
sudo mount /dev/sda1 /mnt/mydrive: Mounts the /dev/sda1 partition at the /mnt/mydrive directory, making it accessible.sudo umount /mnt/mydrive: Unmounts the file system at /mnt/mydrive.
-
Checking and Managing File Systems
lsblk: Displays a tree of all block devices, their partitions, and their mount points.sudo blkid: Displays file system type and other attributes of block devices.
-
Managing Swap Space
sudo mkswap /dev/sda2: Configures the /dev/sda2 partition as swap space.sudo swapon /dev/sda2: Enables the swap space on /dev/sda2, allowing the system to use it for memory paging.
-
-
Package Management
dnf: is a package manager used by certain Linux distributions (e.g., Fedora, CentOS, RHEL).DNFstands for Dandified YUM. Usednfto install packages directly from the configured software repositories without needing to download the package manually first.dnf install package_name: Installs the specified package from the repositories.dnf update: Updates all installed packages to their latest versions.dnf remove package_name: Removes the specified package from the system.dnf search keyword: Searches for packages matching the specified keyword.dnf list installed: Displays a list of all installed packages on the system.
wget: is a command-line utility for manually downloading files from the web.wgetstands for World Wide Web Get. Usewgetwhen need to download a file directly, and we know the specific URL.wget http://example.com/file.zip: This command will download the file file.zip to your current working directory.
-
Service Management
systemctlCommand is used to examine and control the system (systemd) processes.- Basic Commands
systemctl start service_name: Starts the specified service immediately.systemctl stop service_name: Stops the specified service.systemctl restart service_name: Restarts the specified service.systemctl status service_name: Shows the current status of the specified service.systemctl enable service_name: Enables the specified service to start automatically at boot time.systemctl disable service_name: Disables the specified service from starting at boot time.systemctl list-units --type=service: Lists all loaded services and their current status.
-
Miscellaneous Commands
-
Viewing Command History
history: Display the command history.history: Shows a list of previously executed commands.
-
Rebooting and Shutting Down
reboot: Restart the system.sudo reboot: Reboots the machine.shutdown: Shut down the system.sudo shutdown now: Shuts down the machine immediately.
-