bash / zsh commands
- Copy folder contents to another folder:
cp -R folder1/. folder2 - Copy from and to clipboard (macOS)
- Output to clipboard:
<somecommand> | pbcopy - file to clipboard:
pbcopy < file.txt - Clipboard to file:
pbpaste > file.txt - Filtered paste
pbpaste | grep sometext
- Output to clipboard:
- Find files and folders:
find <PATH> -name "<FILE/FOLDER-NAME/[WILDCARD*]>"- Process every find example:
find /path/to/file -name "*.xyz" | while read line; do; echo "do sth with $line"; done;
- Process every find example:
- Change filedate:
touch -t 201203101513 "$filename" (YYYYMMDDHHSS) - Copy file with Date/Time appended:
`now=`date +%y%m%d-%H%M;dest="<DEST-PATH>/<FILENAME>_$now.<EXT>" then cp <SRC-PATH/-FILE> $dest; - Saving output to file
ls -l > filename.txt - Read file permisson:
stat <file> - Change file permission:
chmod 644 <file>[s] - Download file to current folder:
curl -o newfilename.jpg https://remote.url/filename.jpg - Brace expansion:
echo h{e,a,u}llo> hello hallo hullo [s] - Tilde expansion: `~/ = HOME, ~+ = PWD [s]
- Command substitution:$(command) or `command` eg.: echo `date` > Thu Feb 6 10:06:20 CET 2003 [s]
- Encode image base64: encoded=$( base64 Image.jpg ) then echo $encoded or save it to output (see above)
- List all connected USB devices:
ioreg -p IOUSB[s] - line break:
\n - Find out what shell is being usesd:
echo "$SHELL" - Colorizing:
echo -e "\033[31m Hello World"[31mcontrolls the text color30–37sets foreground color,40–47sets background color- use
echo -eorprintf - For colors see: tip_colors and_formating or Wikipedia
- Example with variables and resetting color
- Link folders (alias)
ln -s target-file-folder alias-file-folder- e.g. ln -s project/theme-folder wpinstall.local/wp-content/themes
Batch proccess files in directory
whithin the file directory (in this example: convert to /done/ sub dir)
for filename in *.png
do
convert "$filename" "done/"$filename
done
Menu
#!/bin/bash
# Bash Menu Script Example
PS3='Please enter your choice: '
options=("Option 1" "Option 2" "Option 3" "Quit")
select opt in "${options[@]}"
do
case $opt in
"Option 1")
echo "you chose choice 1"
;;
"Option 2")
echo "you chose choice 2"
;;
"Option 3")
echo "you chose choice $REPLY which is $opt"
;;
"Quit")
break
;;
*) echo "invalid option $REPLY";;
esac
done
- save as XYZ
.sh, run withsh XYZ.sh - See: ask ubuntu
about_self
whoami– computer nameipconfig getifaddr en0– current network ip adresshostname– …nslookup <IP_ADDRESS>– Server/Address/Namehost <IP_ADDRESS>