Skip to content

Instantly share code, notes, and snippets.

@proudlygeek
Last active September 27, 2019 16:51
Show Gist options
  • Save proudlygeek/1959805 to your computer and use it in GitHub Desktop.
Save proudlygeek/1959805 to your computer and use it in GitHub Desktop.
Bash one liners
##### GIT
# Removes all local branch
for branch in $(git branch | grep "feature/MAGENTA-"); do git branch -D $branch; done
# Remove all new files
for file in $(git status | grep "new file" | sed "s/#\tnew file://"); do git rm --cached $file; done
# Delete all remote branches
for remote_branch in $(git ls-remote); do if [[ $remote_branch =~ .*(feature/MAGENTA-([0-9|^130]).+).* ]]; then git push origin :${BASH_REMATCH[1]}; fi; done
##### SSH
#Creates a local SSH tunnel
ssh -L localport:host:remoteport user@host
##### SSH + SCP
# TAR + Copy a folder over SSH from remote host to local host
ssh username@host "tar vczf - SOURCEFOLDER" | cat > jira-backup.tar.gz
# Backup a remote db
ssh booking@teruyo "mysqldump -u root -p dbname" | cat > cloud-backup.sqlh
# Pull a remote dump and import it locally
ssh tvision@margherita "mysqldump -uremote_username -premote_password remote_database_name" | mysql -ulocalusername -plocalpassword local_database_name
### MISC
# Read a GPG Crypted file on vim
cat passwd.gpg | gpg | vim -
# Read a GPG Crypted image
cat image.jpg.gpg | gpg | display
# Copy all files in a folder into another
for file in $(ls); do cp -R $file/wp-content/blogs.dir /home/booking/wordpress/shared; done
# Copy the same folder(s) into multiple hosts (given you have ssh-agent key forwarding)
for host in host1 host2 host3 host4; do scp -r blogs.dir "deploy@$host:~/public_html/cms/shared"; scp -r uploads "deploy@$host:~/public_html/cms/shared"; done
# Resize a folder of images to 940x448
for img in *.jpg; do convert $img -resize 940x448 resized/$img; done
# Rename a file with suffix
rename 's/_pre/_post/' *_pre.txt
# Count LOC (Lines Of Code)
find . -type f -name *.php | xargs cat | wc -l
# Rename files
for img in *.jpg; do mv $img `echo $img | sed -e "s/_.*/\.jpg/g"`; done
@liuggio
Copy link

liuggio commented Oct 17, 2012

I like this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment