I think, therefore I am. I am, therefore I sail

Category: Linux Page 2 of 6

List size of directories in the current folder in terminal

If you would like to list the size of all the directories in your current folder run the following command:

[bash]du -a -h –max-depth=1 | sort -hr[/bash]

This will provide output similar to

[bash]
69M .
36M ./plugins
16M ./.git
14M ./themes
3.4M ./uploads
8.0K ./.DS_Store
4.0K ./upgrade
4.0K ./.revision
4.0K ./index.php
[/bash]

Remove untracked local files from Git

If you have need to get rid of a bunch of files that are in your git repo but not yet tracked it can be tedious to do the job one file at a time. Git has you covered with the git clean command.

Warning: This is a permanent loss of files. Always use –dry-run to verify the action.

Remove untracked files

Remove all untracked files with

[bash]git clean -f[/bash]

Remove untracked files and directories

Remove all untracked files and also any untracked directories

[bash]git clean -f -d[/bash]

Cleanup and remove all merged local and remote git branches

After working on a project for a while you wind up with lots of feature and hotfix branches. If you are working on a team with a remote cannonical project repo there will likely be dozens or hundreds of stale branches that need pruning. Here are a few quick commands that will help you clean up your branches.

TL;DR – Just give me the code!

Get a list of remote branches

[bash]for remote in `git branch -r `; do git branch –track $remote; done[/bash]

Remove remote branches

[bash]git branch –merged master | grep -v master | cut -d/ -f2- | xargs -n 1 git push –delete origin[/bash]

Remove local branches

[bash]git branch –merged master | grep -v master | xargs -n 1 git branch -d[/bash]

Order is important

The order that you remove branches in is important. You must remove the remote branches first otherwise git will not know how to reference the names of the remote branches and they will remain.

Get a list of branches

Chances are if you are working with a team you will not have all of the remote repo branches locally. In order to remove the remote branches your local git needs to know about them. This command will pull a list of the remote branches without fully checking them out. You do not need the contents of the branch, only the name for reference.

[bash]for remote in `git branch -r `; do git branch –track $remote; done[/bash]

Note: While writing this I realized there may be a more efficient way to do this by passing the branch name directly into git’s delete function. I will test this in the future on another cleanup.

Remove merged remote branches

Remote branches that have been merged into master should generally be safe to remove and can easily be accomplished via:

[bash]git branch –merged master | grep -v master | cut -d/ -f2- | xargs -n 1 git push –delete origin[/bash]

Per command breakdown

Get a list of all branches that have been merged into the master branch:

[bash]git branch –merged master[/bash]

Just to be safe cut out the master branch from the previous output if it exists:

[bash]grep -v master[/bash]

When you ran the command to pull a list of remote branches without checking them out is probably added ‘origin/‘ into the branch name. This will confuse the remote repo during the delete operation. The following removes origin/ from the branch name before passing it to the delete command:

[bash]cut -d/ -f2-[/bash]

Finally you can call the git command to delete the remote branch. The xargs command passes the branch names with their manipulations to the git command:

[bash]xargs -n 1 git push –delete origin[/bash]

Remove merged local branches

Local branches that have been merged into master should generally be safe to remove and can easily be accomplished via:

[bash]git branch –merged master | grep -v master | xargs -n 1 git branch -d[/bash]

Per command breakdown

Get a list of all branches that have been merged into the master branch:

[bash]git branch –merged master[/bash]

Just to be safe cut out the master branch from the previous output if it exists:

[bash]grep -v master[/bash]

Finally you can call the git command to delete the remote branch. The xargs command passes the branch names with their manipulations to the git command:

[bash]xargs -n 1 git branch -d[/bash]

Recursively count all files in a directory from the terminal

If you utilize the terminal on a regular daily basis like I do you are bound to run into times when you need to get a count of how many files exist in a directory structure. Here is some code that will display that count for you.

find . -type f | wc -l

The ‘.’ in the command above refers to the current directory. If the directory you are inspecting exists elsewhere replace ‘.’ with the path to that directory.

How to get your Archlinux back on Digital Ocean

Start-in-Linux-server-management-with-DigitalOcean

I love love love DigitalOcean! Spinning up and down boxes to play is fun and the price is awesome! I am even working on moving this site (and all the other sites I host) over to a DigitalOcean box managed by ServerPilot.io.

DigitalOcean (DO) allows you to choose from a list of Linux distributions when spinning up a new machine and one of the things that originally attracted me to DO was the ability to choose Archlinux. Sadly shortly after signing up DO dropped support for Archlinux, for very legit reasons. I have held onto the existing Archlinux machine I created for dear life…

Today Jordan Beaver posted on his website that it is not only possible to use Archlinux on DO again but he also provided a walk through guide on how to do it. It involves spinning up a FreeBSD box and replacing its internals.

For anyone else looking to run Archlinux on DigitalOcean go check out Jordan’s article titled Install Archlinux with full control on DigitalOcean VPS

Page 2 of 6

Powered by WordPress & Beards