Beersj02 Linux Bash and System Administration
Beersj02 Linux Bash and System Administration
#!/bin/env bash — the 'shebang' used to tell the operating system the for value in list-of-values ; do thing-with-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 ((counter=number ; counter<=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 documentation to specify if a parameter is
than another number
required when running script
for counter in { starting-value..ending-value}; do something done —
[parameter] — use in documentation 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
{ starting-value..ending-value..increment-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 (parameters) 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
if condition ; then do-something — if condition is met, do something ${array[-1]} — get the last value in the array
if condition ; then do-something else do-something-else — if ${array[@]} — expand all of the array elements
condition is met, do something, otherwise do something else declare -A associative-array — declare an associative array that
if condition ; then do-something elif condition2 ; then do-something- allows string indices, similar to a dictionary in Python
else else do-final-thing — if condition is met, do something; however associative-array=(["association"]="string" ) — add an association to
if a different condition is met, then do something else; otherwise do an associative array
the final thing array+=( "new" "elements") — append elements to the end of an
while condition-is-true ; do action done — perform the action as long array
as the condition is true shift — move argument $2 to $1
until condition-is-true ; do action done — opposite of while loop, function() { content-of-function } — 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-command ' — define an alias
continuing through script, usually performed within loops
type -a command — tells us if command is an alias
man 5 crontab — view manual page for crontab wget file-url — download a file
crontab -e — edit scheduled tasks in the /var/spool/cron/crontabs 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 package-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 package-name — check package name to install
0 1 * * mon find /temp -atime 3 -exec ls -l {} \; — run the command dnf install package-name — install new software packages
once a week on a Monday dnf remove package-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 information on who is logged in
every day from the 1st through the 15th, inclusive w — produce information on who is logged in
0 1 */5 * * find /temp -atime 3 -exec ls -l {} \; — run the command finger — produce information 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 background, allowing you to continue user ID
executing other commands journalctl --since "YYYY-MM-DD HH:MM" --until "YYYY-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
!command-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
ssh — gives ssh command information /etc/skel — sample startup files you can place in home directories
ssh username@ip-address — log into remote system for new users
ssh-keygen — generate public/private 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
authentication agent for implementing single sign-on with SSH and /sbin folder
ssh-keyscan — for retrieving public keys from servers /opt — contains subdirectories for optional software packages
scp file-path username@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 username@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 username@ip-address:directory — copy a directory from the administration
remote system to your own system /tmp — temporary files stored by applications
exit — terminate the shell /usr — contains applications and files used by users
~ + Ctrl-Z — suspend the remote login session /usr/bin — application/distribution binaries meant to be accessed by
locally logged in users
File Searching /usr/sbin — application/distribution 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/include — standard location of include files used in C programs
rectories (such as <stdio.h>)
grep options pattern files — searches through files for a particular /usr/src — location of sources to programs built on the system
pattern of characters, 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 recursively for pattern in dir the system administrator
locate file - locate a file /var — administrative files such as log files, used for various utilities
/var/spool — temporary storage for files being printed, sent by
Important Directories UUCP
/ — root directory
Ownership and Permissions
/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 permissions
/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 configuration 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/passwd — contains user information 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
chmod ugo file — change the user, group, and others permissions 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 permissions for user, group and/or others for file ln -S target new-name — make links between files
7 — read, write and execute permissions nano file — open file in nano text editor
6 — read and write permissions nano -v file — open file for read only in nano text editor
5 — read and execute permissions
4 — read permissions Git Commands
3 — write and execute permissions git clone /path/to/repository — create a working copy of a local
repository
2 — write permissions
git add * — add all edited files to staging
1 — execute permissions
git add filename — add specific filename to staging
0 — no permissions
git commit -am "commit message" — commit changes to head (but
chmod 644 file — standard permissions for files
not yet to the remote repository)
chmod 755 dir — standard permissions for directories
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 Miscellaneous
ls -l — list items, including their size and permissions, 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, recursively 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, recursively
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 — concatenate file and print to standard output
head file — output first 10 lines of file
tail file — output last 10 lines of file