Useful tips:
- history – list history of all recorded shell history
- history n – list last n statements
- !n – execute nth statement from history
- !! execute last statement
Articles, notes and random thoughts on Software Development and Technology
Useful tips:
Some random shell scripting notes for future reference:
On OS X: find -E . -regex ‘pattern’
On Linux flavors: find . -regextype posix-regex -regex ‘pattern’
Posix vs basic vs extended regex character class differences.
Pipe result to newfile > : eg grep ‘pattern’ file > output.txt
Pipe result appending to file > : eg grep ‘pattern’ file >> output.txt
Capture output as String? : $(some expression)
Iterate files:
for f in some-file-pattern or something producing a list of files
do ... done
Use find . -name ‘pattern’ to recurse matching files down subdirs
Find with a regex for multiple patterns:
find -E . -regex ".*ext1|.*ext2|.*ext3"
first line of file:
head -n 1 filename
grep -o : only display match
Match patterns in file and output matches or matched groups:
Match files, patterns in files, and pipe matches to file:
Quick note to remember this syntax as every few months I find a need to do a grep within a number of files:
find -name "pattern" -exec grep "pattern" {} ;
Grep options:
-H print filename in results
-n print line number where match was found
-l limit match to first found match in file (useful if you want to find files containing a match but don’t care how many matches are in each file)
Pipe to wc -l to count file occurrences, eg:
find -name "pattern" -exec grep -l "pattern" {} ; | wc -l
Use egrep if you need to use regex in the pattern.
Using Cygwin and git on windows, I ran into an issue where git was working fine in a cygwin shell, but I was trying to setup a Jenkins build accessing a git repo, and git from Windows was giving errors like this:
$ git push myremote master
error: cannot spawn c:cygwinbinssh: No such file or directory
fatal: unable to fork
Running shell commands on Windows in Cygwin this was somewhat misleading, as I wasn’t expecting it to be looking for ‘ssh.exe’.
Turns out my GIT_SSH env var was causing the issue, had to replace this:
GIT_SSH=c:cygwinbinssh
with
GIT_SSH=c:cygwinbinssh.exe
and then all was good.