Skip to content

Instantly share code, notes, and snippets.

@andrewcrabb
Last active November 28, 2018 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewcrabb/6185361 to your computer and use it in GitHub Desktop.
Save andrewcrabb/6185361 to your computer and use it in GitHub Desktop.
Bash useful commands
# Remove duplicate photos from Aperture masters
ls 2014/12/*/*\ \([12]\)* | while read dup; do orig=`echo $dup | sed -e 's/ (1)//'`; duorig=`du $orig | cut -f1`; dudup=`du $dup | cut -f1`; if (( dudup == duorig )); then echo $dup; rm $dup; fi; done
# Concatenate with given delimiter
ls -1 | tr "\\n" ","
# Find bad symlinks
find /target/dir -type l ! -exec test -e {} \; -print
# Use full line of grep results
cat my.file | while read LINE ; do
echo "Line = $LINE"
done
# Use read to get full line:
N=0
cat my.file | while read LINE ; do
N=$((N+1))
echo "Line $N = $LINE"
done
# find within for
for f in B*; do find $f -type d | while read fname; do chmod 777 $fname; done; done
find . | while read file; do dosomething "$file"; done
# Break from loop with ctrl-C
for DIR in * ; do rsync -a $DIR example.com:somewhere/ || break; done
# Combine conditionals in bash
# http://goo.gl/Yubxz7
$ if [ ! -f *.l64 ] && [ -f *.l64.7z ]; then echo "yes"; fi
# Get directory of script
# http://bit.ly/1rRH6bi
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Must use brackets to expand glob in variable value
$ for f in *l64.7z; do new=(/mnt/scs/SCS_SCANS/*/`basename $f | sed -e 's/\.7z//'`); if [ -f "$new" ]; then du -m $f $new ; fi; done
# Search path for files in subdirectories
$ for f in */*7z; do new=`basename $f | sed -e 's/\.7z//'`; found=`find /mnt/scs/SCS_SCANS -name $new`; if [[ -n $found ]]; then du -m $f $found; fi; done
# Grep for tab
# http://goo.gl/kTds2
# Grep for tab: Perl-style grep
$ grep -P '\t' *
# Grep for tab: Extended syntax
$ grep $'\t' sample.txt
# Search for matching file, check sizes.
for f in FOO* ; do
found=`find /data/recon/ -name $f`;
if [ ! -z "$found" ]; then
thissize=`stat --printf="%s" $f`;
thatsize=`stat --printf="%s" $found`;
echo $thissize $thatsize;
if [ $thissize -eq $thatsize ] ; then
echo "yes";
fi;
fi;
done
# Find directories not containing NII files.
find -L . -mindepth 3 -type d '!' -exec sh -c 'ls -1 "{}"|egrep -i -q "nii$"' ';' -print | grep MRI >> mris_without_nii2.txt
# Date and time as formatted string.
now=`date +"%y%m%d_%H%M%S"`
# sftp upload command line
sftp foo@bar.com <<< $'put bazfile'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment