Skip to content

Instantly share code, notes, and snippets.

@Telematica
Last active September 7, 2022 23:16
Show Gist options
  • Save Telematica/e867c6e254169b73ebb95f3b52c2ecd8 to your computer and use it in GitHub Desktop.
Save Telematica/e867c6e254169b73ebb95f3b52c2ecd8 to your computer and use it in GitHub Desktop.
Bash Tricks (Linux, MacOS, Unix-like)

Linux / Bash Tricks and Tips for the Evil Genius

  • UsingTerminalAsCalculator.sh - Because sometimes Terminal is the only way to go.
  • ChangeTerminalShell.sh - To update your account to use zsh, please run chsh -s /bin/zsh
#The default interactive shell is now zsh.
#To update your account to use zsh, please run `chsh -s /bin/zsh`.
#For more details, please visit https://support.apple.com/kb/HT208050.
chsh -s /bin/zsh
# Single Line Comment
# https://stackoverflow.com/questions/43158140/way-to-create-multiline-comments-in-bash
: '
This is a
very neat comment
in bash
(Multiline comment)
'
# env - run a program in a modified environment
env
#https://stackoverflow.com/questions/18444194/cutting-the-videos-based-on-start-and-end-time-using-ffmpeg
ffmpeg -i movie.mp4 -ss 00:00:03 -t 00:00:08 -async 1 cut.mp4
#https://stackoverflow.com/questions/23171937/ffmpeg-video-editing-command-in-milliseconds-timestamp
ffmpeg -i a.ogg -ss 00:01:02.500 -t 00:01:03.250 -c copy x2.ogg
#https://stackoverflow.com/questions/7715485/how-to-only-find-files-in-a-given-directory-and-ignore-subdirectories-using-bas
find /dev -maxdepth 1 -name 'abc-*'
# ldd - print shared library dependencies
ldd /dat/wqweb/apps/apache_latest/modules/libphp5.so
# Library Linker
# $LD_LIBRARY_PATH
# Linux Libraries
# /- .so Dynamic Library
# ELF < .a Static Library
# \- .exe Executable
export LD_LIBRARY_PATH=$PATH:/my-stuff/
#https://bash.cyberciti.biz/guide/Getting_User_Input_Via_Keyboard
#https://www.cyberciti.biz/tips/linux-unix-pause-command.html
read -p "Press [Enter] key to start backup..."
read -p "Press any key to resume ..."
## Bash add pause prompt for 5 seconds ##
read -t 5 -p "I am going to wait for 5 seconds only ..."
#https://stackoverflow.com/questions/965053/extract-filename-and-extension-in-bash
#http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
#First, get file name without the path:
filename=$(basename -- "$fullfile")
extension="${filename##*.}"
filename="${filename%.*}"
#Alternatively, you can focus on the last '/' of the path instead of the '.' which should work even if you have unpredictable file extensions:
filename="${fullfile##*/}"
#Fix for dealing with file names without extension:
#Note that if an extension is present, it will be returned including the initial ., e.g., .txt.
extension=$([[ "$filename" = *.* ]] && echo ".${filename##*.}" || echo '').
: '
1)
Reference : https://stackoverflow.com/questions/14842195/how-to-get-file-creation-date-time-in-bash-debian
Reference : https://stackoverflow.com/questions/34123076/osx-how-to-get-the-creation-modification-time-of-a-file-from-the-command-lin
Target OS : [Mac OSX]
Sample Output : 2022-07-25 13:32:30
'
stat -f "%SB" -t "%Y-%m-%d %H:%M:%S" "$filename"
: '
2)
Reference : https://apple.stackexchange.com/questions/238572/how-to-get-file-sizes-of-large-files-in-os-x-terminal
Target OS : [Mac OSX]
Sample Output : 748641265
'
stat -f '%z' filename # Normal file
#https://stackoverflow.com/questions/16355229/how-to-add-an-integer-number-and-a-float-number-in-a-bash-shell-script
echo 1 + 3.5 | bc
awk "BEGIN {print 1+3.5; exit}"
python -c "print 1+3.5"
perl -e "print 1+3.5"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment