Skip to content

Instantly share code, notes, and snippets.

@suzmas
Last active August 18, 2019 17:53
Show Gist options
  • Save suzmas/7c2ecf7ea41658f644cd3f1bdda6e240 to your computer and use it in GitHub Desktop.
Save suzmas/7c2ecf7ea41658f644cd3f1bdda6e240 to your computer and use it in GitHub Desktop.
Fun Unix commands

Unix commands that make me happy

  • Links
  • find commands
    • Find file by filename: find ./foo -name foo.txt
    • Find file by filename starts with / contains (case insensitive): find . -iname foo* find . -iname *foo* etc.
    • Find dir: find ./foo -type d -name bar
    • Find file by modification time: find ./foo -mtime -X or +X where X = number of days. So, +1 would find files modified more than one day ago.
    • Search for text within mult files: find ./ -type f -name "*.md" -exec grep 'foo' {} \;
    • Find and replace text in a range of files: find ./ -type f -exec sed -i '' 's/find/replace/g' {} \;
    • Find files by size: find . -type f -size +100M
    • Find files by size & print size in KB (requires findutils): gfind . -type f -size +3M -printf "%p %k KB\n"
    • Find files by size & print size in bytes: find . -type f -size +3M -exec stat -f "%z %N" {} \;
    • Find files by size & print fileinfo including -lh formatted size: find . -type f -size +3M -exec ls -lh \{\} \;
  • grep commands
    • Find text by file ext.: grep 'foo' ./*.html
  • Image Processing
    • Resize all images in cwd with H / W cap: sips -Z SOME_NUMBER *.jpg
    • ImageMagick
      • Resize all images in cwd maintaining aspect ratio - add padding where image does not fit mogrify -resize 500x600 -background white -gravity center -extent 500x600 *
  • Zippin' Stuff
    • Zip entire dir: zip -r name_of_new_zip_file.zip dir_to_zip
  • Logs
    • from log path: watch tail your_log_name_here.log
  • Organizing Stuff

    • Replace any spaces in dir/file names with underscores: for f in *\ *; do mv "$f" "${f// /_}"; done
    • Replace text in file names (this example depth=1): for f in */*; do mv "$f" "${f//\.jpg\.jpg/\.jpg}"; done
    • Batch rename files: for i in *.jpg; do mv "$i" string_you_want_to_prepend"${i}"; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment