*nix notes

Most of what I do in a *nix env is from memory, but below are snippets I find myself using occasionally  - this is really is just a scrapbook for me.

Rename Multiple Files
find . -name '*.mp4' -printf "'%p' '%h/stats_%f'\n" | xargs -n2  mv

Find common entries in 2 file
comm -12 <(sort 1.txt) <(sort 2.txt)

ls - show names only
ls -l | awk '{print $1 " " $3 " " $8 }' 

A more efficient way of grepping etc on the results of find
find . -type f  -name *Test.java -print | xargs grep -vl /test/ | etc | etc

While loop that waits n seconds before repeating a task
while sleep 10
do
<command>
done

Redirect until keyword
<command> << <keyword>

INPUT HERE
<keyword>

eg, in a script called test.sh :

grep -i $* &lt;&lt; EOF
ABC=1
DEF=2
EOF

./test.sh ABC would return
ABC=1

 
Redirect stderr to same place as stdout

<command> &> onelog.log
<command> > onelog.log 2>&1

 

Wait for background tasks to finish
wait (will wait for all tasks)
wait PID 

 
SED - replace in place
sed -i 's/<searchterm>/<replacement>/g'


Bash conditionals
if command succeeds, run next command:  <command 1> && <command 2>
cd /nonexistantdir && rm *

if command fails, run next command (not otherwise):   <command 1> || <command 2>
cd /nonexistantdir || exit 1 (dont continue with the rest of the script)