Skip to content

Instantly share code, notes, and snippets.

@chintak
Last active March 26, 2016 08: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 chintak/8c53634e283654f30f94 to your computer and use it in GitHub Desktop.
Save chintak/8c53634e283654f30f94 to your computer and use it in GitHub Desktop.
Shell tricks
# Process files in a loop with arbitrary names
while IFS= read -r -d '' f; do
cp "$f" /path2/
done < <(find /path/ -wholename '*.mp3' -print0)
# Find a pattern and replace in place using sed
ls /path/*.csv -1 | grep "subj[0-9]_" | sed -n "s/\(.*subj\)\([0-9].*\)/mv \1\2 \10\2/p"
# Count the number of files in each sub directory and write to a file
find images/ -maxdepth 1 -print0 | xargs -0 -n 1 -I dir sh -c 'printf dir"\t"; find dir/ -name "*.jpg" | wc -l' &> distribution.txt
# Count the number of files in each sub directory, check against a threshold and write to a file
find images/ -maxdepth 1 -print0 | xargs -0 -n 1 -I dir sh -c 'printf dir"\t"; find dir/ -name "*.jpg" | wc -l' | \
awk '{ split($0,a,"/"); split(a[2],b,"\t"); printf ($NF < 500) ? (b[1] != "") ? b[1]"\t"b[2]"\n" : "" : "" }'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment