Linux Shell Cheatsheet Flashcards
- General: Unix file layout
the tree-like logical system layout that organizes and stores all files under directories. at the top of the system is the root, represented with /.
/bin/cp
/bin/is
/bin/pwd
/home/daniel/bin
/home/daniel/profile
etc
https://media.geeksforgeeks.org/wp-content/uploads/20221206120522/noname.png
tree
- General: * UTF
Unicode Transformation Format.
UTF-8: 9bit unicode conversion format.
UTF 65 = A
UTF 66 = B
etc
- Install: sudo
super user do
gives admin/root access rights, applies root access to commands
- Install: apt-get
command line tool for interacting with Advanced Package Tool (APT) Library. (package management system for Linux distributions)
allows you to search, install, manage, update and remove software.
- Install: install
copies files and sets file permissions.
install text.txt /target_directory
install -m 700 text.txt new_directory
(copies to new directory with rwx— permissions)
- Processes: jobs
process managed by shell
- Processes: ^Z/^C
^C kills current process,
^Z pauses the process.
- Processes: bg/fg
fg continues process from the background,
bg continues process in background
- Processes: &
sends the command to the background
- Processes: kill
kills a process. can also kill [ID] to target which process to kill
- Processes: killall
kills all processes in the background
killall sleep // kills all sleeps in the backgground
- Processes: wait
wait() system call suspends execution of the calling thread until one of its children terminates.
or, waits for the completion of any running processes. if multiple processes are running, only uses the last known command’s process id.
wait PID // waits until PID process finishes
3.1: Processes: %n
brings the job at position n to the foreground
3.1: Processes: ps
abbreviates ‘process status’.
ises to list currently running processes and their PIDs along with other info
lists PIDs and
3.1: Processes: nice
lowers a process’s priority
3.1. Processes: time
checks how long a process takes to complete
3.1: Processes: htop
nicer view compared to top. shows active processes, the PIDs, and other info
3.1: Processes: top
shows active processes, their PIDs and other info. but not as pretty as htop.
3.1: Processes: watch
reruns commands in a loop until stopped manually, or runs into a condition that stops the loop.
helps monitor something until it changes.
3.2: Processes: /proc
pseudo-filesystem, provides an interface to kernel data structures. mostly read-only.
displays kernel/system state, but read-only
3.2: Processes: suspend
uh, CTRL+Z?
can be continued with bg or fg
3.2: Processes: Notify-Send
sends notifications, alternative to windows pop-ups.
3.2: Processes: * batch
telling the system to execute commands from a list or queue.
can be made to run at late night or early morning to free the system for interactive.
can also set batch process priority to when CPU is not busy. nice.
use queuedefs fo;e tp describe queues managed by cron daemon. letters between a and y define queue name. tasks started by the at command placed in default queue for a. batch-started commands are placed in b queue. c is for tasks from the crontab file.
batch command will execute commands from standard input or a specified fille when the system load levels drop to a specific point.
3.2: Processes: * at
creates a list of commands to be excecuted at a specified time
3.2: Processes: * atq
displays listed jobs to be executed
3.2: Processes: * crontab
chron table. lists jobs under cron command.
cron meaning the jobs will be executed automatically according to a set schedule
3.2: Processes: * ^Q/^S
^S - stops all output to the terminal(program will keep running) which can be restarted with ^q
^q - releases (displays) terminal output which has been paused with q.
- History: history n
outputs previous n commands to terminal
- !!
inputs the previous terminal input
s/old/new
replaces found instance of old with new
- !-2
displays the 2nd previous command, and inputs it to the terminal
- !!:p
displays the previous terminal input
- Wild Cards & WS: *
- represents any number of characters (even 0 characters).
h*t = hat, hot, hoot, hlnjasjdnaisjnt
- Wild Cards & WS: ?
? represents any single character.
h?t = hat, hot, hit, but not hoot.
- Wild Cards & WS: |chroot
creates a new root directory
chroot [options] /path/to/new/root /path/to/server
mkdir $HOME/jail //creates jail
mkdir -p $HOME/jail/{bin, lib64} //create directories in jailus
cd $HOME/jail
cp -v /bin/{bash, ls} $HOME/jail/bin //copy /bash and /ls to fake home directory
ldd /bin/bash //print shared libraries
cp -v [whatever the displayed libraries are] $HOME/jail/lib64
sudo chroot $HOME/jail /bin/bash// user now treats /jail as root directory.
- Wild Cards & WS: “ “
preserves literal values except for $, `, “, \, and !.
\ only has speacial meaning whewn followed by $, `, “ \ or a newline character.
also guves * and @ meaning if introduces as $@ (passing specified parameters seperately)) or $* (passing all parameters as a single parameter).
- Wild Cards & WS: ‘ ‘
preserves the literal value of each character within the quotes.
echo !!
echo ‘!!’
- Wild Cards & WS: [A-Z]
specifies a range.
m[a,o,u]m becomes man, mom and mum.
m[a-d]m will include mam, mbm, mcm and mdm.
- Wild Cards & WS: {A..Z}
specifies a range seperated by commas.
m{a,o,u}m becomes man, mom and mum.
{*.doc, *.pdf} will include bob.doc, sam.doc, word.pdf, what.pdf
- FS/*: mount
mount -t TYPE DEVICE DIR
attaches the file system found on some device to the file tree/file hierarchy of the unix system
- FS/*: umount
umount {directory|device}
removes the file system found on some device form the file tree/hierarchy of the unix system
- FS/*: {un}compress
sudo install ncompress
- FS/*: df
reports file system disk usage
- FS/*: du
estimates file space usage
- FS/*: sync; sync
synchronizes cached writes to persistent storage
sync;sync
- FS/*: swapon
specifies devices on which paging and swapping are to take place.
swapon [options] [specialfile]
mount/unmount devices as swap files
- FS/*: swapoff
disables swapping on the specified devices and files. -a flag disables swapping on all known swap devices and files (form /proc/swaps, or /etc/fstab)
inode, table
each file has an inode containing metadata about the file. apps can retrieve the metadata using stat
inode table (df -i) contains all the inodes and is created when file system is created
- FS/*: linux file types
Linux supports 7 different file types:
Regular file, various data; text, script, image, video, etc.
Directory file, name and address of other files
Link file, point or mirror other files
Character special file, represents device files, like hard drives annd monitors
Block special file, represents device files, like hard drives annd monitors
Socket file, provides inter-process communication
and Named pip file. allows processes to send data to other processes or recieve data from other processes
- Compression/*: tar
tar cfz bigfile.tgz bigfile compress
tar xf bigfile.tgz decompress
tar balls. generally used to pull files together for easy transport into another system or backup as a group. compression is a bonus feature by adding z to cfz
- Compression/*: dd
boot into rescue mode,
create mount point for external drive: mkdir /mnt/external
mount /dev/sdb1 /mnt/external
create compressed disk image: dd if=/dev/sda | gzip -9 > /mnt/external/filename.img.gz
copies data block by block
future recovery:
zcat /mnt/externak/filename.img.gz | dd if=/dev/sda
- Compression/*: g{un}zip
replace original file with encrypted file
gzip bigfile
gunzip bigfile.gz
- Compression/*: {un}zip
creates compressed file and leaves original intact.
zip ./bigfile.zip
unzip bigfile.zip
- Compression/*: zcat
identical to gunzip -c; standard output, keepigng original file unchanged.
- Compression/*: zgrep
invokes grep on compressed or gzipped files.
zgreo [grep_options] [-e] pattern filename
- Compression/*: zless
filter for crt viewing of compressed text.
crt; certificate and private key files. public-key certificate following x. 509 standard, containind info in the server identy, name, geolocation and public key.
deep dives into the files
- Compression/*: zmore
file perusal filter for crt viewing of compressed text.
- Data Streams: cat
cat [OPTION] [FILE]
concatenate files and print them on the standard output
cat bill.txt
- Data Streams: cmp
compares two files, byte by byte
cmp bill.txt bob.txt
differe on byte 171, line 1
- Data Streams: cut
remove selections from each line of files
cut -c 1-7 bill.txt // outputs first to 7th character,
1- //first character to end,
-40 up to 40th character
- Data Streams: diff
compare files line by line
diff bob.txt bill.txt
- Data Streams: diff3
compare 3 files line by line
- Data Streams: more
more [options] file
paging through the text, one screenful at a time
- Data Streams: less
opposite of more. paging through a text, all at once.
8.1. Data Streams: sort
sort lines of text files.
sort [OPTION]… [FILE]…
write sorted concatenation of all files to standard outpit
8.1. Data Streams: head
outputs the first part of files (1st 10 lines of each file)
8.1. Data Streams: tail
outputs the last part of files (last 10 lines)
8.1. Data Streams: tr
translate or delete characters.
works like substituting one patter for another
8.1. Data Streams: uniq
report or omit repeated lines
uniq [OPTION]… [INPUT [OUTPUT]]
8.1. Data Streams: wc
print newline, word and byte counts for each file
8.1. Data Streams: grep
print lines that match patterns
grep o bob.txt
8.2. Data Streams: find
search for files in a directory hierarchy
8.2. Data Streams: hd
hexdump. displays file contents in hexadecimal, decimal octal or ascii.
8.2. Data Streams: rev
rev bob.txt
‘reverse line characterwise’. outputs the file but reverses all the characters.
8.2. Data Streams: locate
doesn’t search the entire file system like file. looks through regularly update file database (index) in the system, ergo faster.
sudo apt update
sudo apt install mlocate
locate bob.txt
gcp
advanced command line copier
gcp [OPTIONS] FILE DEST
acts like cp (copy) but with high level functionality
(shows cp progress, skips problem files, logs which files copy succesfuly, remove incompatible chars like /. or * on VFAT),) and more
- Shell: man
interface for displaying reference manuals
- Shell: man -k
searches the short descriptions and manual page names for the given keyword as regular expresson. prints out matches. works like apropos
- Shell: history
GNU history library keeps track of lines input by the user.
try history for giggles
- Shell: <tab></tab>
CTRL+v+tab
tab autocompletes input
- Shell: bash
GNU Bourne-Again SHell
sh-compatible language interpreter, executes commands read from an input .sh filew
- Shell: env
runs a program in a modified environment
env [OPTIONS]… - [NAME=VALUE]… [COMMAND [ARG]…]
- Shell: echo
echo xyz
displays a line of text
9.1 Shell: alias/unalias
alias rt~=’rm -r *~’
unalias rt~=’rm -r *~’
9.1 Shell: true/false
true does nothing, succesfuly.
false does nothing, unsuccessfuly.
9.1 Shell: sleep
delays for a specified amount of time.
sleep 100 // sleep for 100 seconds
9.1 Shell: which
locates a command (which file it belongs to)
9.1 Shell: #!/bin/bash
“shabang”.
inscructs the program loader to use the /bin/sh program instead of any other, passing the oath of the script as the first argument.
9.1 Shell: if [] then else fi
if [COMMAND]
then: RUN IF COMMAND IS TRUE
else: RUN IF COMMAND IS FALSE
fi: finish
9.1 Shell: do while
while [COMMAND]
do: WHILE COMMAND IS TRUE
done: IF COMMAND IS FALSE.
9.2 Shell: ulimit
get and set user limits. obsolite routine.
but in shell: displays or sets shell resource limits.
ulimit // reports the soft limit on file size.
ulimit -H // reports hard limit on file size.
ulimit -u 8000 // set soft limit on number of user processes to 8000
9.2 Shell: /dev/tty
controlling terminal for the current process.
ps -a // to find out which ttys are attatched to which process.
9.2 Shell: set
allows you to change the values of shell options and set the positional parameters, or display the names and values of shell variables.
https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html
9.2 Shell: $?
ls duck
echo $?
$? is the return value of the last executed command. 0 if succesful, 2 if not. or anything other than 0, depending on the progra,.
last process’s exit status
9.2 Shell: for i in [] do done
for VARIABLE In 1 2 3 4 5 .. N
do
command1
command2
command 3
done
for VARIABLE in file1 file2 file3
do
command1
command2
command3
done
for OUTPUT in $(cmd)
do
command1 on $OUTPUT
command2 on $OUTPUT
commandN
done
9.2 Shell: apropos
searches the short descriptions and manual page names for the given keyword as regular expresson. prints out matches. works man -k.
search manual’s short descriptions
9.2 Shell: * bind -p
bind command is for viewing and modifying readline keybindings.
-p will display both the keybindings and the corresponding function names.
- Login & System: su
run command iwth substitute user and group ID
run
su [options] [-] [user [arguments…]]
- Login & System: reboot
reboots the machine. returns 0 on success, non-zero failiure otherwise.
- Login & System: shutdown
shutdown [OPTIONS] [TIME] [WALL…]
-H halts the machine
-P poweroffs
-r reboot
- Login & System: users
print the user names of users currently logged in to the current host
- Login & System: clear
clears the terminals screen
- Login & System: hostname
show or set the system’sdisplays the system’s DNS name host name
- Login & System: ifconfig
configure a network interface
with no additional commands, displays the status of the currently active interfaces.
10.1 Login & System: exit
causes normal process termination
10.1 Login & System: date
prints or sets the system date and time
10.1 Login & System: w
show who is logged on and what they are doing
gives users, with more info (users)
10.1 Login & System: whoami
prints effective userid
10.1 Login & System: lshw
lists hardware. run with sudo, something might be missing.
10.1 Login & System: last reboot
shows a listing of last logged in users
10.1 Login & System: ssh -X/rsh/telnet
opens remote login client.
-X enables X11 forwarding. enable with caution: users bypassing remote host file permissions can perform attacks like keystroke monitoring.
use -x to disable x11 forwarding.
10.2 Login & System: ~^Z
stops the current process and sends it to the background
10.2 Login & System: * xterm
terminal emulator for the X window system.
10.2 Login & System: * scp
openSSH secure file copy.
copies files between hosts on a network
10.2 Login & System: * uptime
tells how long the system has been running
10.2 Login & System: * xload
displays a constantly updating histogram of the system load average
10.2 Login & System: * wall
writes a message to all users
10.2 Login & System: * write
send a message to another user
write daniel tty
10.2 Login & System: * netstat
prints network connections, routing tables, interface statistics, masquerade connections, and multicast memberships
10.2 Login & System: * free
display amount of free and used memory in the system
- Files: pwd
print name of current working directory
- Files: ls
list directory contents
- Files: lsof
list open files
- Files: cp
copy files and directories
- Files: mv
move (rename) files
- Files: rm
remove files or directories
- Files: mkdir
make directories
11.1 Files: rmdir
remove empty directories
11.1 Files: cd
change current directory
11.1 Files: ln
make links between files.
lan [OPTIONS]… TARGET… DIRECTORY…
-P physical, creates a hard link.
(hard link is its own file, soft link just points to the name of the original file)
11.1 Files: touch
creates the file if it doesn’t exist,
updates the access and mod times of each FILE to the current time,
-c flag will not create any file,
11.1 Files: ftp
ftp host [port]
functions like open host [port]
11.1 Files: ~
shortcut for user’s home directory
11.1 Files: .
current directory
11.2 Files: ..
directory immediately above the current one
11.2 Files: .bashrc
script file that’s executed when a user logs in
11.2 Files: .profile
present in your $HOME directory, overrides /etc/profile file to customize individual working environment.
11.2 Files: pushd
pushd [OPTIONS] [DIRECTORY]
pushd /home // push the current directory to the top of the directory stack and change to /home
popd will pop the top directory from the stack and move ‘down’ the stack.
11.2 Files: popd
pushd [OPTIONS] [DIRECTORY]
pushd /home // push the current directory to the top of the directory stack and change to /home
popd will pop the top directory from the stack and move ‘down’ the stack.
11.2 Files: source
reads and executes the file content in the current shell.
(can type set of commands into a text.txt and use source to run all the commands)
source cmdlist.txt
11.2 Files: * readlink
print resolved symbolic links or canonical file names, path links and buffer
11.2 Files: * tree
list contents of directories in a tree-like format
- I/O Redirection: stdin
extern FILE *stdin;
standard input
- I/O Redirection: stdout
extern FILE *stdout;
standard output
1
- I/O Redirection: sterr
extern FILE *stderr;
standard error
2
- I/O Redirection: <
read the command’s input from a file instead of from the keyboard
sort < data.txt
overwrite stdin
- I/O Redirection: >
sends input (from terminal?)to output file
cat bill.txt > bob.txt
overwrite stdout
- I/O Redirection: 2>
2 denotes standard error,
> output,
therefore 2> outputs the standard error
overwrite stderr
- I/O Redirection: &>
redirect standard output
write over stdout AND stderr
- I/O Redirection: «
append stdin
- I/O Redirection:»_space;
append stdout
- I/O Redirection: 2>&1
stdout and stderr
- I/O Redirection: /dev/null
vacuuuuuuum go vuuuuum
- I/O Redirection: tee
named after and functions like a T-pipe.
wc -l text1.txt|tee -a text2.txt
outputs the bit count of text1.txt to console, but also appends it to text2.txt (-a flag appends. removing the -a flag will overwrite the contents of text2.txt)
- I/O Redirection: script
makes typescript of terminal session.
script [options] [file]
- I/O Redirection: cmd - command substitution
allows the output of a command to replace the command itself.
$(command)command
for example: $(cat file)
whatever was written in that file becomes the command
- I/O Redirection: ^D
logs you out of any terminal and closes it, or gets to back to the original user before using su or sudo.
- Compilation/*: {g}cc/g++
g++: GNU C and C++ compiler. compiles .c and .cpp files.
gcc: GNU C compiler. compiles .c files
g++ is an alias for including c++ files
- Compilation/*: gcc/g++ -g
g++: GNU C and C++ compiler. compiles .c and .cpp files.
gcc: GNU C compiler. compiles .c files
-g flag generates debug info to be used by GDB debugger.
tells gcc to generate source-level debugging/symbol info within the executeable itself. IF the program crashes and produces a core file, is killed, or a program calls a function that dumps its core
- Compilation/*: ld
GNU linker.
ld [OPTION] objfile.
combines object/archive files, relocates their data and ties up symbol differences
last step in compiling a program is to run ld, usually
converts multiple linked files into a single .a/out
- Compilation/*: nm
list symbols from object files
- Compilation/*: ar
create, modify and extract from archives.
archive: single file holding collection of other files ina structure that that makes it possible to find/retrieve the original files/’members’
runes compiler and finds the issues
- Compilation/*: gdb
GNU debugger.
interface for seeing what happens ‘inside’ a program as it executes, or what another program was doing as it crashed.
- Compilation/*: make
GNU make [OPTION]… [TARGET]
GNU make utility to maintain groups of programs
auto-detects what pieces of a large program need to be recompiled, and issues commands to recompile them.
- Compilation/*: valgrind
tool for debugging, finding memory leaks, detection and profiling
- Compilation/*: splint
Secure Program Lint
programming tool for statically checking C programs for security vulnerabilities and coding mistakes.
automatically finds and points out vulnerabilities and mistakes in C programs
- Compilation/*: core file
generated when a program terminates unexpectedly, helps determine what caused the termination.
- Compilation/*: gprof
display call graph profile data
- Permissions: chmod
change file mode bits
changes file moded bits of each given file,
- Permissions: chown
change file owner and group
chown [OPTION] [OWNER] FILE
chown [OPTION] [OWNER][:[GROUP]] FILE
- Permissions: chgrp
change group ownership
chgrp [OPTION]… GROUP FILE
- Permissions: mask
manipulate nmh message sequences
change spe
- Permissions: unmask
user file creation mode mask
set file mode creation mask
set default
- Permissions: setuid
set user identity
- Information tools: espdiff
apply the appropriate transformation to a set of patches
- Information tools: fortune
displays a fortune
cosway
cow goes moo
- User Montor: xeyes
googly eyes