Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Judith
Judith

Posted on

     

Command Line Snippets do you keep handy?

Here are a few commands I keep handy:

Bash
// root login ssh
ssh root@my-ip-here

// find PID
sudo lsof -i :

// kill port
sudo kill -9

// To remove a directory that contains other files or directories
rm -r mydir

// don't receive a prompt for each file being removed
rm -rf mydir

Brew
brew update && brew upgrade && brew cleanup && brew doctor

Git
// remove a repo
find . -type f | grep -i ".git" | xargs rm
cd ..
rm -rf

//sets your name and email for commit messages
git config --global user.name 'John Doe'
git config --global user.emailjohndoe@example.com

// helps recover from mess ups! a log of your last few actions

git reflog

Top comments(20)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
ben profile image
Ben Halpern
A Canadian software developer who thinks he’s funny.
  • Email
  • Location
    NY
  • Education
    Mount Allison University
  • Pronouns
    He/him
  • Work
    Co-founder at Forem
  • Joined

Remove all local branches except master...

git branch |grep-v"master" | xargs git branch-D
CollapseExpand
 
skkeeper profile image
Fábio André Damas
  • Joined

Inspired by yours but adapted for my use case: delete all branches that are already merged into the current branch safely (doesn't delete branches that aren't fully merged and shows warning):

git branch--merged |grep-v"$(git rev-parse--abbrev-ref HEAD)" | xargs git branch-d

I use this on develop to delete old feature and bugfix branches.

CollapseExpand
 
jrohatiner profile image
Judith
frontend software engineer. mental adventurist
  • Email
  • Location
    miami beach/new york
  • Education
    Masters Degree Systems
  • Work
    Senior Frontend Software Engineer at AChickAndAClick
  • Joined

OMG yes! very important to have handy :)

CollapseExpand
 
waylonwalker profile image
Waylon Walker
👋 Hey there, I am Waylon WalkerI am a Husband, Father of two beautiful children, Senior Python Developer currently working in the Data Engineering platform space. I am a continuous learner, and sha
  • Location
    Peoria, Illinois
  • Work
    Python Dev
  • Joined

Nice one!

CollapseExpand
 
nickytonline profile image
Nick Taylor
I'm a fan of Open Source and have a growing interest in serverless and edge computing. I'm not a big fan of spiders, but they're doing good work eating bugs. I also stream on Twitch.
  • Email
  • Location
    Montréal, Québec, Canada
  • Education
    University of New Brunswick
  • Pronouns
    He/Him
  • Work
    Developer Advocate at Pomerium
  • Joined

I rely heavily on git aliases. I find it really speeds up my workflow. For those interested, here’s my git aliases.

CollapseExpand
 
jrohatiner profile image
Judith
frontend software engineer. mental adventurist
  • Email
  • Location
    miami beach/new york
  • Education
    Masters Degree Systems
  • Work
    Senior Frontend Software Engineer at AChickAndAClick
  • Joined
• Edited on• Edited

Great list! You reminded me of how tweaks to my config have helped a lot too! Here's how to add colors to your console by adding this to your config:

Add colors to your ~/.gitconfig file:

[color]
ui = auto
[color "branch"]
current = yellow reverse
local = yellow
remote = green
[color "diff"]
meta = yellow bold
frag = magenta bold
old = red bold
new = green bold
[color "status"]
added = yellow
changed = green
untracked = cyan

And add this too:
Highlight whitespace in diffs

[color]
ui = true
[color "diff"]
whitespace = red reverse
[core]
whitespace=fix,-indent-with-non-tab,trailing-space,cr-at-eol

CollapseExpand
 
pavelloz profile image
Paweł Kowalski
Performance, JavaScript, Serverless, and Testing enthusiast.
  • Location
    Earth, Milky Way
  • Work
    Senior Performance Engineer at platformOS
  • Joined
• Edited on• Edited
gitbranch-r|awk'{print $1}'|egrep-v-f/dev/fd/0<(gitbranch-vv|greporigin)|awk'{print $1}'|xargsgitbranch-d

Removes branches that are marked as remote on your machine, but dont exist on origin.

If it complains, replace-d with-D at the end.

Used aftergit fetch prune.

It keeps branch list tidy and real.

CollapseExpand
 
jrohatiner profile image
Judith
frontend software engineer. mental adventurist
  • Email
  • Location
    miami beach/new york
  • Education
    Masters Degree Systems
  • Work
    Senior Frontend Software Engineer at AChickAndAClick
  • Joined

excellent!

CollapseExpand
 
fboaventura profile image
Frederico Freire Boaventura
  • Joined

Hi!

Just as an advice, take real care with this:

// remove a repofind . -type f | grep -i ".git" | xargs rmcd ..rm -rf

I've made a quick setup just to illustrate, but let me explain the inner workings of these commands, specially the find and grep combination, that is the dangerous part here.

The tree I have here is:

tree

Where the blue items are folders and the gray are files.

find . -type f will search and print all the files, starting on the actual folder up to all levels downward. And thegrep -i ".git" will match to anything that has<any_ character>[gG][iI][tT], at any part of the name.

So, on my special and quick setup, I'll get this result::

» find . -type f | grep -i '.git'./.git/configure./.git/HEAD./folder1/agitos./.gitignore./.gitattributes./agitar/aaa./agitar/a/1./agitar/a/2./agitar/a/3./agitar/a/4./agitar/a/5./agitar/a/6./agitar/b/1./agitar/b/2./agitar/b/3./agitar/b/4./agitar/b/5./agitar/b/6./agitar/c/1./agitar/c/2./agitar/c/3./agitar/c/4./agitar/c/5./agitar/c/6

This is definitely not the expected behavior, and is quite easy to be achieved (believe me :) ). If you pass this result into thexargs rm you may end up removing a lot of undesired files. And you have to take extra care wit this, because asking file by file if you want it to be deleted isn't the default behavior of therm command. On RedHat, CentOS and Fedora there is an aliasrm=rm -i, to make the confirmation mandatory by default.

cd .. is going up one level on the folders, and therm -rf without any other arguments is doing absolutely nothing, but leaves space for a disaster.

My advice, when you want to remove a repository from a folder, is to make sure you are removing the right folder, so pass the complete path to therm command, like so:

rm -rf /home/user/dev/my_program/.git

Always, when usingrm -rf pass the complete path to the file/folder you want to remove.

Also, while I'm around, thefind command is a real swiss knife. To find and remove the.git folder and remove it you can use (but is hardly not advised to):

find /home/user/dev/my_program -type d -name '.git' -delete

You may want to, for example, find all the repositories inside you home folder:

find /home/user -type d -name '.git'

You can also search for folders/files older than, newer than, ..., ..., ...

Have fun and take care! :)

CollapseExpand
 
jrohatiner profile image
Judith
frontend software engineer. mental adventurist
  • Email
  • Location
    miami beach/new york
  • Education
    Masters Degree Systems
  • Work
    Senior Frontend Software Engineer at AChickAndAClick
  • Joined

Thank you! That is very very helpful.
Namaste!

CollapseExpand
 
brycehipp profile image
Bryce Hipp
  • Location
    Oakland, TN
  • Work
    Senior Software Engineer at Quantum Edge, LLC
  • Joined
• Edited on• Edited

A few handy snippets that I've made into aliases

# Get OS X Software Updates, and update installed Ruby gems, Homebrew, npm, and their installed packagesaliasupdate='sudo softwareupdate -i -a; brew update; brew upgrade; brew cleanup; npm install npm -g; npm update -g; sudo gem update --system; sudo gem update'# Clear out node_modules and re-install everythingaliasyarn.please="printf 'Removing node_modules folder...' && rm -rf node_modules && yarn"aliasnpm.please="printf 'Removing node_modules folder...' && rm -rf node_modules && npm i"
CollapseExpand
 
jrohatiner profile image
Judith
frontend software engineer. mental adventurist
  • Email
  • Location
    miami beach/new york
  • Education
    Masters Degree Systems
  • Work
    Senior Frontend Software Engineer at AChickAndAClick
  • Joined

great suggestions!

CollapseExpand
 
quasipickle profile image
Dylan Anderson
  • Location
    Camrose, AB
  • Work
    Web Applications Specialist at University of Alberta
  • Joined

Snippets I've made aliases for

List flag aliases

aliasll='ls -l'alias ls='ls --color'aliaslh='ll -h'aliasla='ls -a'aliaslla='la -l'

cd alias because I don't always get that space in time

aliascd..='cd ..'
CollapseExpand
 
jrohatiner profile image
Judith
frontend software engineer. mental adventurist
  • Email
  • Location
    miami beach/new york
  • Education
    Masters Degree Systems
  • Work
    Senior Frontend Software Engineer at AChickAndAClick
  • Joined

awesome!

CollapseExpand
 
waylonwalker profile image
Waylon Walker
👋 Hey there, I am Waylon WalkerI am a Husband, Father of two beautiful children, Senior Python Developer currently working in the Data Engineering platform space. I am a continuous learner, and sha
  • Location
    Peoria, Illinois
  • Work
    Python Dev
  • Joined

I love those little ones, where I don't really need this, but my fingers automatically do it in a bit of haste so why not.

CollapseExpand
 
brandinchiu profile image
Brandin Chiu
Current CTO exploring entrepreneurship on the side; coach; mentor; instructor.Dedicated to promoting digital literacy and ideological diversity in tech.

When I want to transfer a file to a remote server without going through the security headaches of installing ftp servers:

cat file.txt | ssh me@ip_address "cat > newfile.txt"

CollapseExpand
 
fboaventura profile image
Frederico Freire Boaventura
  • Joined

Hi Brandin!

You may also use:

scp file.txt me@ip_address:newfile.txtscp file.txt me@ip_address:/var/www/newfile.txtscp -r folder me@ip_address:

if you don't add anything after the: the file/folder being transfered will be stored on the user's home folder.

CollapseExpand
 
waylonwalker profile image
Waylon Walker
👋 Hey there, I am Waylon WalkerI am a Husband, Father of two beautiful children, Senior Python Developer currently working in the Data Engineering platform space. I am a continuous learner, and sha
  • Location
    Peoria, Illinois
  • Work
    Python Dev
  • Joined

I am really bad at storing aliases, and heavily rely on my command line history hand 🤭. I heavily usefzf and its Ctrl-R functionality to fuzzy search my past commands. I can typically get to a previous command within a few keystrokes.

CollapseExpand
 
javaarchive profile image
Raymond
I like web applications a lot
  • Location
    US
  • Joined

zip -r . Backup.zip
Don't rember if that's the correct one, but I do backup my code a lot to download onto my local machine.

CollapseExpand
 
tesla3690 profile image
tesla3690
  • Joined

test

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

frontend software engineer. mental adventurist
  • Location
    miami beach/new york
  • Education
    Masters Degree Systems
  • Work
    Senior Frontend Software Engineer at AChickAndAClick
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp