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