ZSH Daily Tips
79 subscribers
2 links
A clean logical shell with powerful globbing and completion and much more
Download Telegram
# Matching all files which do not have a dot in filename
$ ls *~*.*(.)
# explanation
ls * # all files
~ # except
*.* # files with a dot
(.) # files only , exclude possible directory with a dot
# history (fc)
fc -ln -4 > /tmp/hist # no numbers
fc -ln 1 | grep rsync | gvim -
fc -l -5 # 5 most recent
fc -l 1 5 # 5 oldest
fc -l -10 -5 # 10th newest to 5 newest
fc -l 1 | more # step through all
fc -lir # with datestamps (reverse)
alias hg='fc -l 1 | grep -i '
# your own metalanguage
# the power of global aliases
alias -g ND{1,}='*(/om[1])' # newest dir
alias -g ND{2,D}='*(/om[2])' # 2nd newest dir
alias -g NF{1,}='*(.om[1])' # newest file
alias -g NF{2,F}='*(.om[2])' # 2nd newest file
cp NF ND
vi NF2
# the = operator locates any file in any directory in $PATH
# tab completion works as well
vi =week
cd =week(:h)
/usr/local/bin
# create your super-shell with global aliases.
alias -g TOD=' *(.m0)' # today's files
alias -g F=' | fmt -50 ' # format
alias -g G=' | grep -i'
alias -g GV=' | grep -iv' # negative grep
alias -g H=' | head '
alias -g M=' | less '
alias -g ERR=' >& /dev/null & '
gvim -p TOD
# find 5 largest files in hierarchy includes: with filter by file type php,inc exclude: directories libs and tmp
~ means exclude
ls -lS **/*.(php|inc)~(libs|tmp)/*(.OL[1,5])
# useful/essential setopts
setopt interactivecomments # allows end of command line comments
setopt HIST_IGNORE_DUPS
setopt HIST_IGNORE_SPACE # dont put commands beginning with space in history
setopt # to see your current settings
# Open newest image in downloads directory
alias imgd='cd ~/Downloads/ && xnview *.(jpg|jpeg|gif|png)(.om[1])  >& /dev/null &'
# using = 0 to n subdirectories
ls /fred.jpg # standard
setopt GLOBSTARSHORT # simplify glob
ls fred.jpg # better
ls /fred.jpg # slow search from root
ls /home/**fred.jpg # search from home
ls ../**fred.jpg # search up and down . you want this
cp ../**/template.jpg .
# single liners no 4 (a template)
# find directories that contain both "index.php" and "index.html"
ls **/*(D/e:'[[ -e $REPLY/index.php && -e $REPLY/index.html ]]':)
# zsh Special variables
echo EUID:$EUID GID:$GID EGID:$EGID USERNAME:$USERNAME fignore:$fignore mailpath:$mailpath cdpath:$cdpath
echo LINENO:$LINENO RANDOM:$RANDOM SECONDS:$SECONDS COLUMNS:$COLUMNS HISTCHARS:$HISTCHARS UID:$UID
# Append /etc/services at the end of file foo' and bar'

$ cat /etc/services >> foo >> bar


# Append exit 1' at the end of all *.sh - files
``
$ echo "exit 1" >> *.sh`
# identify non-binary files
notbinary() { file --mime $REPLY | grep -iqv binary }


ls **/*(.+notbinary)
# z can also cope with sub-directories
z vac spa 18

~/Pictures/vacation/spain/2018
# but what if i haven't visited 2017 yet?
z vac spain ; cd 2017

# or
z vac spain ;  2017 # with autocd

~/Pictures/vacation/spain/2017
# locate completion function for a given command eg. apt-get
print $^fpath/$_comps[apt-get](N)
/home/phy1729/zsh/Completion/Debian/Command/_apt
# the power of bindkey
bindkey # which zsh widgets are already bound
bindkey -v # set vi as cli editor. 'covers up' many default
bindkey | grep -i push-line # find out which key a widget is on
# now find what the default bindings are
zsh -f # zsh with zero config
bindkey
# evolution/trends of tools (via RedHat post)
fgrep -> grep -f
egrep -> grep -e
nslookup OR dig
ifconfig / route --> ip
netstat -> ss
ping 8.8.8.8 # never changes LOL
ls *.^c(.) # all files excluding c files
ls -l *.(png|jpg|gif)
# images only
ls *(*)
# executables only
ls /etc/**/zsh
# which directories contain 'zsh'?
ls **/*(/^F)
# list empty directories
ls /etc/*(@)
# symlinks only
ls **/*(-@)
# list dangling symlinks
# visualize directory structure
tree -d
# or
ls -d **/
# or
ls -d **(/)