Shell
Basic loop to iterate over lines in a file
More complex loop using ‘if’ statement
Useful for control actions, cleaning up output, etc.
Checking busy log files for their contents
This does not hang your console as opposed to using tail -f
.
Alternative conditional logic in loop
Declare nodes
variable separately or prepend to loop and separate with semicolon.
‘while’ loop to iterate over lines in a file
Avoids calls to cat
as is the case with the for
loop example. Using madison
command rather than policy
seems to be slightly faster.
Match lines into an array
Grab block of text between two patterns
Octal permissions for a file or directory
Grab last character from string
Parse output of ‘grep’ into commands
Include lines before/after a ‘grep’ match
Find all unique directories in listed directories that contain files modified 10 minutes ago since the command was ran
Find all files in the current directories that were modified at least a minute ago, are larger than 500MB, and long list them
Find all files in the current directories that were modified at least a day ago, are larger than 2GB, and empty their contents
Run command against a list of directories
Step-by-step debug Bash scripts
Move ahead with Enter key.
Change timezone interactively
Search binary file while ignoring case
System call info for a directory listing
Show line number during script run
Remove duplicated lines in order
Run local script on a remote endpoint
Create new directory and change into it
From here.
Recall argument to last used command
From here.
Get SSH key fingerprint
Find broken symbolic links
Bulk fix relative symbolic links
Run remote script on remote endpoint
Create ISO from directory
The -l
doesn’t truncate long names and hyphens with underscores aren’t replaced using -iso-level 4
.
List ISO file contents without mounting
Simple colouring for log files
Works for both static and running output.
From here.
Suppress Python warnings
For situations like these.
Remove last column using delimiter
Disregard alias execution
Pretty print CSV files
From here
Pretty print TSV files
Diff two files and save output to file
Show build information for cloud image
Show top disk usage and exclude directories
Use ’:’ for an infinite loop
Execute Bash to ‘unsource’ variables
Use binary version of ‘time’
This provides access to more information.
From here.
Use ‘perf stat’ to repeat a command
Also provides additional useful measurements.
More examples here.
Change key value in array of JSON
Use indirect references for dynamic variables
Test terminal’s colors
Bulk rename files in place
Encode with Base64 on a single line
Convert PEM to single-line
Standard output to a new directory
Find and delete files older than 1 year
View permissions as a tree
-p
: permissions-u
: username/userid-f
: full path-i
: don’t print indentation lines-d
: print directories only
Use wildcard in ‘pip uninstall’
Show transaction history for a package
Show transaction’s historical info
More on ‘history’
Undo last transaction
List SystemD timers
Show execution of timer and service
Copy remote directory to local system
Overwrite existing directory
Count number of installed kernels
Increase number of installed kernels
This goes in /etc/dnf/dnf.conf
.
Pin specific kernel version
Undo ad hoc changes made to a service
For example systemd-resolved
.
Back up a file using brace expansion
Equivalent to cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
.
The same can be applied to directories:
More on brace expansion.
Restore a backed up file
Equivalent to cp /etc/ssh/sshd_config.bak /etc/ssh/sshd_config
.
Download older version of a kernel
- More information about Koji
- A list of all kernels built for Fedora
- A list of downloadable kernels built for Fedora
- Some may be removed
Fedora Discussion here.
Parallel execution of lines from a file
Verify SSL certificate against domain
Run programs with ‘systemd-run’
This will leverage SystemD’s features.
Documentation here.
Working with ‘screen’
- Start a session:
screen
- Start any long-running commands
- Detach from session:
Ctrl+A+D
- List sessions:
screen -ls
- Attach to a running session:
screen -r <session ID>
Don’t send anything identifiable over SSH
Using Ctrl keys
Ctrl + a
: move to the beginning of the lineCtrl + d
: if you’ve type something, it deletes the character under the cursor, otherwise it quits the current shellCtrl + e
: move to the end of the lineCtrl + k
: delete all text from the cursor to the end of the lineCtrl + l
: equivalent to clearCtrl + p
: same as Up arrowCtrl + n
: same as Down arrowCtrl + s
: to stop output to terminalCtrl + q
: to resume output to terminal afterCtrl + s
Ctrl + r
: begins a backward search through command history, keep pressing to continue moving backwardsCtrl + t
: transpose the character before the cursor with the one under the cursorEsc + t
: transposes the two words before the cursor
Ctrl + u
: cut the line before the cursorCtrl + y
to paste it
Ctrl + w
: cut the word before the cursorCtrl + y
to paste it
Ctrl + x + Backspace
: delete all text from the beginning of line to the cursorCtrl + x + Ctrl + e
: launch editor defined by$EDITOR
to input your command- exit the editor (e.g.
wq
) and the command will be ran automatically - useful for multi-line commands
- exit the editor (e.g.
Ctrl + z
: stop current running process and keep it in backgroundfg
to continue the process in the foregroundbg
to continue the process in the background
Ctrl + _
: undo typing
From here.
Use ampersand in ‘sed’ for efficiency
The ampersand means that the entirety of what was matched will be used in the replacement.