Skip to content

anuraagnagar/linux-cheatsheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 

Repository files navigation

Linux Command Line Cheat Sheet

Welcome to the Command Line Cheat Sheet! This comprehensive guide is designed to assist both beginners and experienced users in navigating the command line interface efficiently. Whether you're managing files and directories, working with permissions, or diving into network and remote connections, this cheat sheet provides a quick reference for essential commands of Linux.

How to Use:

  • Commands are presented in the format used in a bash terminal.
  • Explanatory notes are provided in paragraph format to give you a clear understanding of each command's functionality.

Table of Contents

  1. Users

  2. Directory Navigation

  3. Working with Files

  4. File/Directory Permissions

  5. Search

  6. Archive

  7. Installing Programs from Packages

  8. Processes

  9. System

  10. Hardware Commands

  11. Disc Management Commands

  12. Network

  13. Remote Connection

Users

Detailed information about a user (uid, gid, and group).

id

Lists information about recent logins, including time, username, IP address, and session duration.

last

Displays authorized users.

who

Creates a group named "testgroup".

groupadd "testgroup"

Adds a user with the name "NewUser".

adduser NewUser

Deletes the user with the name "NewUser".

userdel NewUser

Modifies information about the user "NewUser".

usermod NewUser

↑ Back to Top

Directory Navigation

Navigate to the root directory.

cd /

Navigate to the home directory (using the $HOME variable).

cd ~

Navigate to the /root directory.

cd /root

Move one level up.

cd ..

Navigate to the hidden folder .ssh.

cd /root/.ssh

↑ Back to Top

Working with Files

Displays files and directories in the current folder.

ls -al

Shows the current working directory.

pwd

Creates a new directory named 'NewFolder'.

mkdir NewFolder

Deletes the file named 'NewFile'.

rm NewFile

Forcefully deletes the file named 'NewFile'.

rm -f NewFile

Recursively deletes the directory named 'NewFolder'.

rm -r NewFolder

Forcefully and recursively deletes the directory named 'NewFolder'.

rm -rf NewFolder

Copies the content of 'oldfile1' to 'newfile2'.

cp oldfile1 newfile2

Recursively copies the directory 'olddir1' to 'newdir2'. Dir2 will be created if it doesn't exist.

cp -r olddir1 newdir2

Renames 'oldfile1' to 'newfile2'.

mv oldfile1 newfile2

Creates a symbolic link to the file.

ln -s /etc/log/file logfile

Creates an empty file named 'newfile'.

touch newfile

Takes STDIN and puts it into 'newfile'.

cat > newfile

Outputs the content of 'newfile' one screen at a time.

more newfile

Outputs the first 10 lines of the file 'newfile'.

head newfile

Outputs the last 10 lines of 'newfile'.

tail newfile

Encrypts 'newfile' in gpg format using a password and saves it in the same directory.

gpg -c newfile

Decrypts the gpg file.

gpg newfile.gpg

Displays the count of bytes, words, and lines in the new file.

wc newfile

↑ Back to Top

File/Directory Permissions

Sets read, write, and execute permissions for everyone who has access to the server (owner, group, others).

chmod 777 /root/ssh

Configures permissions as rwx for the owner and r_x for the group and others.

chmod 755 /root/ssh

Sets rwx for the owner and rw for the group and others.

chmod 766 /root/ssh

Changes the owner of newfile to newuser.

chown newuser newfile

Changes both the owner and group owner of newfile to newuser and newgroup.

chown newuser:newgroup newfile

Changes both the owner and group owner of the directory newfolder to newuser and newgroup.

chown newuser:newgroup newfolder

Displays the user and group owners of newfile.

stat -c "%U %G" newfile

↑ Back to Top

Search

Searches for the searchargument in newfile.

grep searchargument newfile

Recursively searches for the searchargument in all files within the newfolder.

grep -r searchargument newfolder

Shows all locations of the newfile.

locate newfile

Finds files with names starting with searchargument in the /etc directory.

find /etc/ -name "searchargument"

Finds files larger than 50000k in size in the /etc directory.

find /etc/ -size +50000k

↑ Back to Top

Archive

Create an archive 'archive.tar' from the file 'newfile.'

tar -cf archive.tar newfile

Extract the contents of the file 'archive.tar.'

tar -xf archive.tar

Create an archive from the /var/log/ directory and compress it using gzip.

tar -zcvf archive.tar.gz /var/log/

Compress the new file (it will have the extension .gz).

gzip newfile

↑ Back to Top

Installing Programs from Packages

Installs an RPM package on CentOS, RHEL, etc.

rpm -i pkg_program.rpm

Removes an RPM package on CentOS, RHEL, etc.

rpm -e pkg_name

Installs a package using DNF from the repository on CentOS, RHEL, etc. YUM was previously used, but it has recently been replaced by DNF.

dnf install pkg_name

Installs from a DEB package on Debian, Ubuntu, Mint, etc.

dpkg -i pkg_name

Removes a DEB package on Debian, Ubuntu, Mint, etc.

dpkg -r pkg_name

Installs a package from the repository on Debian, Ubuntu, Mint, etc.

apt install pkg_name

Removes a package on Debian, Ubuntu, Mint, etc.

apt remove pkg_name

Updates the packages in the system (Debian, Ubuntu, Mint, etc.) and updates the repositories.

apt upgrade && apt update

↑ Back to Top

Processes

Displays currently running processes.

ps

Finds the process ID (PID) of 'bash'.

ps aux | grep 'bash'

Maps the process with PID 11 in process memory.

pmap -x 11

Shows all running processes.

top

Terminates a process by PID.

kill pid

Terminates all processes with the name "process".

killall process

Sends a signal to a process by name.

pkill process-name

Sends a suspended process to the background.

bg

Brings a running process to the foreground.

fg

Brings a process named "process" to the foreground.

fg process

Lists files opened by processes.

lsof

Sets the lowest priority for a process.

renice 19 PID

Finds the process ID for 'bash'.

pgrep bash

Shows a tree-like representation of processes.

pstree

↑ Back to Top

System

Displays system information.

uname

Shows information about the Linux kernel.

uname -r

Shows system uptime and average load.

uptime

Displays the host name.

hostname

Displays the host's IP address.

hostname -i

Shows the reboot history.

last reboot

Displays date and time.

date

Outputs and modifies date and time settings.

timedatectl

Displays the calendar.

cal

Shows users currently logged in.

w

Displays your username.

whoami

Shows information about the root user (requires installation with "apt-get install finger").

finger root

↑ Back to Top

Hardware Commands

Displays system messages during boot.

dmesg

Shows information about the processor.

cat /proc/cpuinfo

Displays information about the memory.

cat /proc/meminfo

Shows detailed information about devices.

lshw

Displays information about block devices.

lsblk

Frees up memory: RAM and swap (switch -m for MB).

free -m

Shows PCI device information in a tree view.

lspci -tv

Displays USB devices in a tree view.

lsusb -tv

Shows information about BIOS devices.

dmidecode

Displays information about the disk.

hdparm -i /dev/xda

Shows read and write speed of xda.

hdparm -tT /dev/xda

Performs a test for bad sectors.

badblocks -s /dev/xda

↑ Back to Top

Disc Management Commands

Shows free space on mounted partitions (in bytes).

df -h

Displays free inodes in the file system.

df -i

Provides information about the disk, partitions, and file system.

fdisk -l

Shows undistributed space on mounted partitions in MB, GB, TB.

du -sh

Displays all mount points.

findmnt

Mounts partition 1 of sdb disk to the /mnt directory.

mount /dev/sdb1 /mnt

↑ Back to Top

Network

Displays the IP addresses of all available network interfaces.

ip addr show

Assigns the address 192.168.0.1 to the eth0 interface.

ip address add 192.168.0.1/24 dev eth0

Shows the IP addresses of all available network interfaces.

ifconfig

Sends an ICMP protocol request to connect to the node at 192.168.0.1.

ping 192.168.0.1

Displays information about the

whois domain

Retrieves DNS information about the domain.

dig domain

Performs reverse DNS resolution.

dig -x 192.168.0.1

Resolves the host address.

host serverspace.us

Shows local addresses.

hostname -I

Downloads a file.

wget file_name(link to file)

Displays all ports being listened to on the host (requires "apt-get install net-tools").

netstat -pnltu

↑ Back to Top

Remote Connection

Connects to a remote host via ssh as the root user.

ssh root@host

Connects to a remote host using a non-default ssh port, specifying the user.

ssh -p port_number user@host

Utilizes the default connection using the current user.

ssh host

Uses a telnet connection (port 23).

telnet host

↑ Back to Top

Releases

No releases published

Packages

No packages published