0% found this document useful (0 votes)
68 views4 pages

Beersj02 Linux Bash and System Administration

This document provides a cheat sheet for Linux, Bash scripting, and system administration. It includes summaries of Bash scripting basics like shebangs and parameters; Bash loops and conditions like for, while, and if/then statements; automated commands using cron; downloading and unpacking files; package management with dnf; and viewing system logs with commands like journalctl, who, and finger. The cheat sheet is organized into sections covering these topics to serve as a quick reference guide.

Uploaded by

Sergeo Armani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views4 pages

Beersj02 Linux Bash and System Administration

This document provides a cheat sheet for Linux, Bash scripting, and system administration. It includes summaries of Bash scripting basics like shebangs and parameters; Bash loops and conditions like for, while, and if/then statements; automated commands using cron; downloading and unpacking files; package management with dnf; and viewing system logs with commands like journalctl, who, and finger. The cheat sheet is organized into sections covering these topics to serve as a quick reference guide.

Uploaded by

Sergeo Armani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Linux, Bash, and System Administration Cheat Sheet

by beersj02 via cheatography.com/89657/cs/20407/

Bash Scripting Basics Bash Loops and Conditions (cont)

#!/bin/env bash — the 'shebang' used to tell the operating system the for value in list-o​f-v​alues ; do thing-​wit​h-value done — iterate over a
path it should use to interpret the file list of values
bash file-name.sh — run the bash script in terminal for ((counte​r=n​umber ; counte​r<=​number ; counter++ )); do

./ file-name.sh — run the bash script in terminal if set to executable something done — start at counter is equal to a number, then do
something and increment the counter by 1 until the counter is greater
<parameter> — use in docume​ntation to specify if a parameter is
than another number
required when running script
for counter in { starti​ng-​value..ending​-value}; do something done —
[parameter] — use in docume​ntation to specify is a parameter is
brace expansion that iterates over a number range or character
optional when running script
range from starting value to the ending value
# — used to make comments throughout script
{ starti​ng-​value..ending​-value..increm​ent​-value} — specify the
|| — logical OR
increment value in a for loop, otherwise the default is 1
&& — logical AND
for (( ; ; )); do something done — infinite loop
$# — resolved to the number of arguments that have been passed to
break — can add to while or for loops to exit from the loop but
the script
continue the rest of the script
$0 — refer back to the script name
continue — used to skip current iteration of a loop and continue to
$1, $2, etc. — refer to user input (param​eters) that user can add the next iteration of the loop
when running script, separated by a space
cut — cut different parts of a string
exit [0-255] — exit script and return number from 0 to 255. 0 means
basename path — get the filename from a given path
everything worked as intended, but other values can be used to
denote errors that the script ran into
Bash Arrays and Functions

array=("​ele​men​ts" "​of" "​arr​ay") — create an array of strings


Bash Loops and Conditions
${array[0]} — get the first element of the array
if fi — basic structure of all if-the​n-exit, if-the​n-else, or if-eli​f-else
statements ${array[*]} — get all values in the array

if condition ; then do-som​ething — if condition is met, do something ${array[-1]} — get the last value in the array

if condition ; then do-som​ething else do-som​eth​ing​-else — if ${array[@]} — expand all of the array elements
condition is met, do something, otherwise do something else declare -A associ​ati​ve-​array — declare an associ​ative array that
if condition ; then do-som​ething elif condition2 ; then do-som​eth​ing​- allows string indices, similar to a dictionary in Python
else else do-fin​al-​thing — if condition is met, do something; however associ​ati​ve-​array=(["​ass​oci​ati​on"]="​str​ing​" ) — add an associ​ation to
if a different condition is met, then do something else; otherwise do an associ​ative array
the final thing array+=( "​new​" "​ele​men​ts") — append elements to the end of an
while condit​ion​-is​-true ; do action done — perform the action as long array
as the condition is true shift — move argument $2 to $1
until condit​ion​-is​-true ; do action done — opposite of while loop, function() { conten​t-o​f-f​unction } — define a function
perform the action until the condition becomes true
alias — list all aliases defined in the current session
sleep time — sleep or wait for a specified number of second before
alias alias =' bash-c​ommand ' — define an alias
continuing through script, usually performed within loops
type -a command — tells us if command is an alias

By beersj02 Published 5th December, 2019. Sponsored by ApolloPad.com


cheatography.com/beersj02/ Last updated 5th December, 2019. Everyone has a novel in them. Finish
Page 1 of 4. Yours!
https://apollopad.com
Linux, Bash, and System Administration Cheat Sheet
by beersj02 via cheatography.com/89657/cs/20407/

Automated Commands Download and Unpack

man 5 crontab — view manual page for crontab wget file-url — download a file
crontab -e — edit scheduled tasks in the /var/s​poo​l/c​ron​/cr​ontabs file tar -xzf tar-file — extract a tar file
crontab -l — list scheduled tasks
Package Management
* * * * * find directory -exec ls -l {} \; — find files on directory
dnf upgrade — update the system and all of its packages
* * * * * - cron format (0-59 minutes, 0-23 hours, 1-31 day of month,
1-12 month, 0-7 day of week) dnf search packag​e-name — search for new software called packag​‐
e-name
0 1 1 * * find /temp -atime 3 -exec ls -l {} \; — run the command just
on the first day of each month dnf provides packag​e-name — check package name to install

0 1 * * mon find /temp -atime 3 -exec ls -l {} \; — run the command dnf install packag​e-name — install new software packages
once a week on a Monday dnf remove packag​e-name — remove a package from the system
0 1 1,15 * * find /temp -atime 3 -exec ls -l {} \; — run the command on
the 1st and 15th day of each month System Logs

0 1 1-15 * * find /temp -atime 3 -exec ls -l {} \; — run the command who — produce inform​ation on who is logged in
every day from the 1st through the 15th, inclusive w — produce inform​ation on who is logged in
0 1 */5 * * find /temp -atime 3 -exec ls -l {} \; — run the command finger — produce inform​ation on who is logged in
every fifth dat (1st, 6th, 11th, etc.)
id -u username — get the user ID for a specific user
at — reads commands to be executed from a file or from standard
journalctl — view the log of the entire system
input
Q — quit from journalctl log
atq — show which commands you have in the at queue, displays job
journalctl -f — follow the logs in real time
number, date of planned execution and job owner
journalctl -u sshd — view only log entries for ssh unit
atrm job-num — delete a job from the queue by specifying job-num
journalctl -u httpd -n 3 — vie a specific number of log entries (i.e. 3)
System journalctl _UID=1000 — view log entries for a specific user by giving
& — puts command into the backgr​ound, allowing you to continue user ID
executing other commands journalctl --since "​YYY​Y-MM-DD HH:MM" --until "​YYY​Y-MM-DD
du — display disk usage statistics HH:MM" — filter and display log entries for a certain time period

df — display free disk space dmesg — view all kernel messages from the last boot of the machine

free — display amount of free and used memory in the system last — display last user logins

kill — get rid of a command in the background history — list previous commands used

man command — show manual for command history | grep keyword — search for a command by keyword in
history
shutdown now — shutdown machine
!comman​d-num — repeat a command from history and run the
command
script — record all output for the session in a file
exit — exit from scripting session

By beersj02 Published 5th December, 2019. Sponsored by ApolloPad.com


cheatography.com/beersj02/ Last updated 5th December, 2019. Everyone has a novel in them. Finish
Page 2 of 4. Yours!
https://apollopad.com
Linux, Bash, and System Administration Cheat Sheet
by beersj02 via cheatography.com/89657/cs/20407/

Secure Shell Important Direct​ories (cont)

ssh — gives ssh command inform​ation /etc/skel — sample startup files you can place in home direct​ories
ssh username@ip-address — log into remote system for new users

ssh-keygen — generate public​/pr​ivate key pair /home — contains a home folder for each user

ssh-add — command for adding SSH private keys into the SSH /lib — contains libraries needed by the essential binaries in the /bin
authen​tic​ation agent for implem​enting single sign-on with SSH and /sbin folder

ssh-ke​yscan — for retrieving public keys from servers /opt — contains subdir​ect​ories for optional software packages

scp file-path userna​me@​ip-​address: — copy a file from your local /proc — the interface between the filesystem and the running
system to remote system processes, the CPU and memory

scp userna​me@​ip-​address:file-path — copy a file from the remote /root — the home directory of the root user
system to your own system /sbin — very common commands used by the superuser for system
scp -r userna​me@​ip-​address:directory — copy a directory from the admini​str​ation
remote system to your own system /tmp — temporary files stored by applic​ations
exit — terminate the shell /usr — contains applic​ations and files used by users
~ + Ctrl-Z — suspend the remote login session /usr/bin — applic​ati​on/​dis​tri​bution binaries meant to be accessed by
locally logged in users
File Searching /usr/sbin — applic​ati​on/​dis​tri​bution binaries that support or configure
find — search for a file or directory on your file system stuff in /sbin

find /home -name *.jpg — find all .jpg files in the /home and sub-di​‐ /usr/i​nclude — standard location of include files used in C programs
rec​tories (such as <st​dio.h>)

grep options pattern files — searches through files for a particular /usr/src — location of sources to programs built on the system
pattern of charac​ters, and displays all lines that contain that pattern /usr/local — programs and data files that have been added locally by
grep -r pattern dir — search recurs​ively for pattern in dir the system admini​strator

locate file - locate a file /var — admini​str​ative files such as log files, used for various utilities
/var/spool — temporary storage for files being printed, sent by
Important Direct​ories UUCP
/ — root directory
Ownership and Permis​sions
/bin — the most essential Unix commands (such as ls)
sudo — log in or run program as root user
/boot — location where the kernel and other files used during booting
are sometimes stored ls -l — display ownership and permis​sions

/dev — contains device files, the interface between the filesystem adduser — create a user account (as root)
and the hardware passwd account — set password for account (as root)
/etc — contains config​uration files, which can generally be edited by userdel -r account — delete an account and account's home
hand in a text editor directory (as root)
/etc/p​asswd — contains user inform​ation in a certain format chown — change owner of a file
( username:password:uid :gid :gecos:homedir :shell )
chown userid /home/ userid/ — make user account owner of home
directory (as root)
chgrp — change group

By beersj02 Published 5th December, 2019. Sponsored by ApolloPad.com


cheatography.com/beersj02/ Last updated 5th December, 2019. Everyone has a novel in them. Finish
Page 3 of 4. Yours!
https://apollopad.com
Linux, Bash, and System Administration Cheat Sheet
by beersj02 via cheatography.com/89657/cs/20407/

Ownership and Permis​sions (cont) File Management (cont)

chmod ugo file — change the user, group, and others permis​sions for less file — view file instead of opening in an editor, allowing page
file (ugo given in base 8, where u is the user, g is the group, and o is navigation
others) sort file — used to sort a file, arranging the records in a particular
chmod [ugo][​+-=​][rwx] file — give, take away, or set the read, write, order
and/or execute permis​sions for user, group and/or others for file ln -S target new-name — make links between files
7 — read, write and execute permis​sions nano file — open file in nano text editor
6 — read and write permis​sions nano -v file — open file for read only in nano text editor
5 — read and execute permis​sions
4 — read permis​sions Git Commands

3 — write and execute permis​sions git clone /path/​to/​rep​ository — create a working copy of a local
repository
2 — write permis​sions
git add * — add all edited files to staging
1 — execute permis​sions
git add filename — add specific filename to staging
0 — no permis​sions
git commit -am "commit message" — commit changes to head (but
chmod 644 file — standard permis​sions for files
not yet to the remote reposi​tory)
chmod 755 dir — standard permis​sions for direct​ories
git push — send changes to the master branch of your remote
find / -user username -ls — find files associated with a user
repository
git status — list the files you've changed and those you still need to
File Management
add or commit
ls — list items in your current directory
ls -a — list all items and hidden files in your current directory Miscel​laneous
ls -l — list items, including their size and permis​sions, in your current yes "string" — echo string in infinite loop
directory
cal — prints an ASCII calendar of the given month or year
pwd — prints path of current working directory
date — display current system time
cd — change directory to home directory
true — does nothing and finishes with zero exit code, indicating
cd dir — change directory to dir success
cd .. — go up one directory false — does nothing and finishes with non-zero exit code (often 1),
cp file1 file2 — copy file1 to file2 indicating failure
cp -r dir1 dir2 — copy dir1 to dir2, recurs​ively clear — clears the screen of the terminal
mv file1 file2 — move file1 to file2, or just change file name
rm file — remove file
rm -r dir — remove directory dir, recurs​ively
echo text — outputs text to standard output
echo "text" > file — redirect text to file
touch file — create file, such as an empty txt or zip
cat file — concat​enate file and print to standard output
head file — output first 10 lines of file
tail file — output last 10 lines of file

By beersj02 Published 5th December, 2019. Sponsored by ApolloPad.com


cheatography.com/beersj02/ Last updated 5th December, 2019. Everyone has a novel in them. Finish
Page 4 of 4. Yours!
https://apollopad.com

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy