Skip to content

Instantly share code, notes, and snippets.

@macmladen
Last active October 7, 2016 18:19
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save macmladen/4224633 to your computer and use it in GitHub Desktop.
Save macmladen/4224633 to your computer and use it in GitHub Desktop.
BASH one-liners
## CHECKING HEADERS AND CERTIFICATE
# curl headers
curl -Iv https://example.com
# check certificate
openssl s_client -connect www.example.com:443
## FIXING FILES PERMISSIONS
# Remove Mac OS X Desktop Services Store files
find . -name ".DS_Store" -exec rm {} \;
# If you accidentally chmod -R 755 on everything revert files to 644
find . -type f -perm 755 -exec chmod 644 {} \;
# fix files and directories to default permissions
find . -type f -exec chmod 644 {} \;
find . -type d -exec chmod 755 {} \;
# If you see in OS X files in full listing with '@' that means they have extended attributes.
## OS X ONLY
# Remove extended attributes in OS X recursively
xattr -rc .
# Extract tld domains that are under dnsmasqd
scutil --dns | sed -n "/^ domain/{N;N; s/^ domain : \(.*\)\n nameserver.*/\1/gp;}"
# Which file is the oldest in archive?
# Change access/created time on arcihe [CC[YY]]MMDDhhmm[ss]
tar -tvf archive.old.tgz | sort -k 4 -r | head
touch -t 1312101322 archive.old.tgz
## ITERATION AND BATCH
# iterate directory items
for i in ./dir1/*; do ls ${i}; done
# Do some batch processing
for f in $(ls *mp4 | sed 's/prefix_to_remove\(.*\)/\1/'); do mv prefix_to_remove$f $f; done
## COMPARING DIRECTORIES AND FILES
# Compare two directories, r - recurse, q - no file diff
diff -rq dir1 dir2
# compare list of files local vs remote
diff <(find . | egrep -v '(exclude1|exclude2)' | sort) <(ssh user@site.com 'cd public_html; find . | egrep -v "(exclude3|exclude4)" | sort')
## RSYNC
# Useful rsync parameters
rsync -Pavuzh --exclude-from '/var/www/backup/rsync-exclude.txt' user@domain.com:source/ destination/
rsync -Pavuzh # interactive, visual
rsync -auzin # check what will transfer first, then remove n
rsync -auz # operational
# -P same as --partial --progress
# --partial keep partially transferred files
# --progress show progress during transfer
# -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)
# -v, --verbose increase verbosity
# -u, --update skip files that are newer on the receiver
# -z, --compress compress file data during the transfer
# -h, --human-readable output numbers in a human-readable format
# -I, --ignore-times don't skip files that match size and time
# -i, --itemize-changes output a change-summary for all updates
# -c, --checksum skip based on checksum, not mod-time & size
# -n, --dry-run show what would have been transferred
## IMAGE OPERATIONS
# Iterate all png files with convert command that is cropping to 800x600 at top/left 110px and 205
# and save resulting file in prepared new/ directory with same name
for i in *.png ; do echo $i ; convert -crop 800x600+110+205 "$i" new/"$i" ; done
# Check and convert/optimize image
identify -verbose about.jpg
convert about.jpg -quality 30 about-2.jpg
# Batch convert images on Mac OS X
for i in *.jpeg; do sips -s format png $i --out Converted/$i.png;done
# Change string in all files in directory hierarchy
find . -type f -exec sed -i 's/searc/replace/g' {} \;
# In grep file name is expanded by the shell _before_ running the command resulting in the error message.
grep -r "search" . --include='*.ext'
## SERVER OPS
# How to add ssh key to remote host
cat id_rsa.pub | ssh <remote_user>@<host> 'cat >> .ssh/authorized_keys'
# MySQL import .gz
zcat dbdump.sql.gz | mysql -u dbuser -pdbpass -h dbhost dbname
## FIND
# Finding on user or permissions
find . -user my_user -perm -u+rwx
# Find size of all files inside
find . -name "*jpg" -exec du -k {} \; | awk '{ total += $1 } END { print total/1024 " Mb total" }'
find . -type f -iname "*.jpg" -ls | awk 'BEGIN {print "0"}; {print $7, "+"}; END {print "p"}' | dc
# Find files after date ... then pipe into next oneliner ;)
find /some/start/dir -type f -mtime -7d4h52m -exec ls -al {} \;
## LISTING
# ls not including, grep -v is for invert, second is for excluding two file types
ls !(match*)
ls | grep -v 'match*'
ls | grep -v -e '\.one$' -e '\.two$'
# Sum output using awk
ps aux | grep apa | awk '{ mem += $6 } END { print mem }'
# Generate comma CSV separated folder listing
ls -l | sed '1d' | awk '{printf("%s,%s,%s,%s,%s,%s,%s,%s,%s\n",$1,$2,$3,$4,$5,$6,$7,$8,$9)}' > somefile
## PATCHING
# To create a patch for a single file, use the form:
diff -u original.php new.php > name_of.patch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment