PentOpsVault @syztem4our666

PentOpsVault
Linux

Basics of Linux

CheatSheet

Linux Cheat Sheet

Table of Contents

File Listing

CommandDescription
lsList the names of files and subfolders in the current directory.
ls -lAlso show details of each item displayed, such as user permissions and the time/date when the item was last modified.
ls -aAlso display hidden files/folders. May be combined with ls -l to form ls -al.
ls -tSort the files/folders according to the last modified time/date, starting with the most recently modified item.
ls List the files matching pattern.

Special instances:

  • . — current directory
  • .. — parent directory

Changing Directories

CommandDescription
cd directory-nameChange directory to directory-name.
cdTo the $HOME directory.
cd ..Up one level to enclosing folder or parent directory.
cd /etcTo the /etc directory.

File Operations

CommandDescription
cmpCompare two files for sameness. No output if they are identical, outputs character and line number otherwise.
diffCompare two files for differences. Outputs the difference.
pwdDisplay the path of the current working directory.
mkdirMake a new directory named X inside the current directory.
mvMove a file from one path to another. Also used for renaming files.
cpCopy a file from one path to another. Usage similar to mv.
cpRecursively copy a directory and its contents to another location.
rmRemove (delete) a file permanently.
rm -r Recursively delete a directory and its contents.
rm -f Forcibly remove a file without prompts or confirmation.
rm -rf Forcibly remove a directory and its contents recursively.
rmdir Remove a directory permanently, provided it is empty.

File Viewing and Editing

CommandDescription
open -eOpens a file in the default text editor.
touchCreate an empty file or update the access and modification times.
catView contents of a file.
cat -bAlso display line numbers.
wcDisplay word count of a file.
headDisplay the first 10 lines of a file. If multiple files are specified, each file is preceded by a header consisting of "==> [Filename] <==".
head -n 4Show the first 4 lines of a file.
ls *.c | head -n 5Display the first 5 items of a list of .c files in the current directory.
tailDisplay the last 10 lines of a file.
tail -n +1Display entire contents of the specified file(s), with header of respective file names.
tail -fDisplay the last 10 lines of the specified file(s), and track changes appended to them at the end.
CommandDescription
ln -sCreate symbolic link of path.

Input/Output Redirection

CommandDescription
echo TEXTDisplay a line of TEXT or the contents of a variable.
echo -e TEXTAlso interprets escape characters in TEXT, e.g., \n → new line, \b → backslash, \t → tab.
cmd1 | cmd2Pipe the output of cmd1 to cmd2.
cmd > fileRedirect output of a command cmd to a file file. Overwrites pre-existing content of file.
cmd >& fileRedirect output of cmd to file. Overwrites pre-existing content of file. Suppresses the output of cmd.
cmd > /dev/nullSuppress the output of cmd.
cmd >> fileAppend output of cmd to file.
cmd < fileRead input of cmd from file.
cmd << delimRead input of cmd from the standard input with the delimiter character delim to terminate the input.

Search and Filter

CommandDescription
grepSearch for a text pattern in specified file(s) or standard input.
grep -rSearch recursively for a text pattern.
grep -vReturn lines not matching the specified pattern.
grep -lWrite to standard output the names of files containing the pattern.
grep -iPerform case-insensitive matching.

Archives

CommandDescription
tarManipulate archives with .tar extension.
gzipManipulate archives with .gz extension.
bzip2Manipulate archives with .bz2 extension.
zipCreate ZIP archives.
unzipUnzip ZIP archives.

File Transfer

CommandDescription
ssh user@accessConnect to access as user.
ssh accessConnect to access as your local username.
ssh -p port user@accessConnect to access as user using port.
scp [user1@]host1:[path1] [user2@]host2:[path2]
scp -P port [user1@]host1:[path1] [user2@]host2:[path2]Connect to hostN as userN using port for N=1,2.
scp -r [user1@]host1:[path1] [user2@]host2:[path2]Recursively copy all files and directories from path1 to path2.
sftp [user@]accessLogin to access as user via secure file transfer protocol. If user is not specified, your local username will be used.
sftp accessConnect to access as your local username.
sftp -P port user@accessConnect to access as user using port.
rsync -a [path1] [path2]Synchronize [path1] to [path2], preserving symbolic links, attributes, permissions, ownerships, and other settings.
rsync -avz host1:[path1] [path2]Synchronize [path1] on the remote host host1 to the local path [path2], preserving symbolic links, attributes, permissions, ownerships, and other settings. It also compresses the data involved during the transfer.

File Permissions

CommandDescription
chmod permission fileChange permissions of a file or directory. Permissions may be of the form [u/g/o/a][+/-/=][r/w/x] (see examples below) or a three-digit octal number.
chown user2 fileChange the owner of a file to user2.
chgrp group2 fileChange the group of a file to group2.

Usage examples:

CommandDescription
chmod +x testfileAllow all users to execute the file
chmod u-w testfileForbid the current user from writing or changing the file
chmod u+wx,g-x,o=rx testfileSimultaneously add write and execute permissions to user, remove execute permission from group, and set the permissions of other users to only read and write.

System Information

General

CommandDescription
unameShow the Linux system information.
uname -aDetailed Linux system information
uname -rKernel release information, such as kernel version
uptimeShow how long the system is running and load information.
suSwitch user to root
sudoSuperuser; use this before a command that requires root access e.g., su shutdown
calShow calendar where the current date is highlighted.
dateShow the current date and time of the machine.
haltStop the system immediately.
shutdownShut down the system.
rebootRestart the system.
last rebootShow reboot history.
man COMMANDShows the manual for a given COMMAND. To exit the manual, press “q”.
hostnameShow system host name
hostname -IDisplay IP address of host
cat /etc/*-releaseShow the version of the Linux distribution installed. For example, if you’re using Red Hat Linux, you may replace * with redhat.

Disk Usage

CommandDescription
dfDisplay free disk space.
duShow file/folder sizes on disk.
du -ahDisk usage in human readable format (KB, MB etc.)
du -shTotal disk usage of the current directory
du -hFree and used space on mounted filesystems
du -iFree and used inodes on mounted filesystems
fdisk -lList disk partitions, sizes, and types
free -hDisplay free and used memory in human readable units.
free -mDisplay free and used memory in MB.
free -gDisplay free and used memory in GB.

Process Management and Performance Monitoring

CommandDescription
&Add this character to the end of a command/process to run it in the background
psShow process status. Often used with grep e.g., ps aux | grep python3 displays information on processes involving python3.
ps -efPrint detailed overview
ps -U root -u rootDisplay all processes running under the account root.
ps -eo pid,user,commandDisplay only the columns pid, user and command in ps output
topDisplay sorted information about processes
htopDisplay sorted information about processes with visual highlights. It allows you to scroll vertically and horizontally, so you can see every process running on your system and entire commands.
atopDisplay detailed information about processes and hardware
kill PIDKill a process specified by its process ID PID, which you obtain using the ps command
killall proc1Kill all processes containing proc1 in their names
lsofList all open files on the system. (This command helps you pinpoint what files and processes are preventing you from successfully ejecting an external drive.)
lsof -u rootList all files on the system opened by the root user. As the output can be long, you may use lsof -u root | less to keep this list from taking up space in the terminal output.
mpstat 1Display processor-related statistics, updated every second (hence the 1, whereas mpstat 2 refreshes the output every 2 seconds)
vmstat 1Display virtual memory statistics (information about memory, system processes, paging, interrupts, block I/O, disk, and CPU scheduling), updated every (1) second
iostat 1Display system input/output statistics for devices and partitions. virtual memory statistics, updated every (1) second
tail -n 100 /var/log/messagesDisplay the last 100 lines in the system logs. Replace /var/log/messages with /var/log/syslog for Debian-based systems.
tcpdump -i eth0Capture and display all packets on interface eth0
tcpdump -i eth0 port 80Monitor all traffic on interface eth0 port 80 (HTTP)
watch df -hExecute df -h and show periodic updates. To exit, press Ctrl+C.

User Management

CommandDescription
whoDisplay who is logged in
wDisplay what users are online and what they are doing
usersList current users
whoamiDisplay what user you are logged in as
idDisplay the user ID and group IDs of your current user
lastDisplay the last users who have logged onto the system
groupadd gp1Create a group named gp1
useradd -c "Alice Bob" -m ab1Create an account named ab1, with a comment of "Alice Bob" and create the new user’s home directory
userdel ab1Delete the account named ab1
usermod -aG gp1 ab1Add the account ab1 to the group gp1

Networking

CommandDescription
ifconfigDisplay all network interfaces with IP addresses
ifconfig -aDisplay all network interfaces, even if any of them is down, with IP addresses
ifconfig eth0Display IP addresses and details of the eth0 interface
ip aAnother way to display all network interfaces with IP addresses
ethtool eth0Query or control network driver and hardware settings of the interface eth0
netstatPrint open sockets of network connections, routing tables, interface statistics, masquerade connections, and multicast memberships. Pipe with the less command: e.g., netstat -a | less
netstat -aShow both listening and non-listening sockets
netstat -lShow only listening sockets
netstat -nutlpShow listening TCP and UDP ports and corresponding programs
ping hostSend ICMP echo request to host, which may be a symbolic name, domain name or IP address
whois domainDisplay whois information for domain
dig domainDisplay DNS information for domain
dig -x addrDo a reverse lookup on an IPv4 or IPv6 address addr
host domainDisplay DNS IP address for domain
wget LINKDownload from location LINK
curl LINKDisplay the HTML source of LINK. Check out our curl Cheat Sheet for details.

On this page

Edit on GitHub