DEV Community

Cover image for 101 Bash Commands and Tips for Beginners to Experts

101 Bash Commands and Tips for Beginners to Experts

Andrew (he/him) on January 13, 2019

Update 25 Sep 2019: This article is now available in Japanese, thanks to the hard work of ラナ・クアール. Please check out their work by following the li...
Collapse
 
ben profile image
Ben Halpern

Holy cow!

Collapse
 
awwsmm profile image
Andrew (he/him)

My Markdown editor started choking near the end of writing this. RIP Simplenote.

Collapse
 
joehobot profile image
Joe Hobot • Edited

That's what s..... ah nevermind 😛

Collapse
 
webreflection profile image
Andrea Giammarchi

this is an awesome post but I've no idea how could anyone use a shell that looks like a pipe operator all the time ... some command with pipe reads very badly, why not removing all the starting parts of your snippets, adding a classic $ like any other shell, instead of 17:40 | andrew@pc01 ~ | ?

Collapse
 
phlash profile image
Phil Ashby

Fabulous readable reference, thanks Andrew!

Minor niggle - almost all of these are not bash commands, but command line tools that work the same from any shell - it would be nice to know what are bash built-ins that might go pop in another shell, and what aren't :)

Long-running advanced voodoo:

  • disown (bash built-in): disconnects background processes from your terminal such that they stay running after you log off.
  • screen (cli tool): opens a new terminal (tty/pty), such that anything you run in that terminal stays running when you disconnect from it / log off. You can also reconnect (screen -r) when you log back in.

I prefer screen over using disown, because of the reconnection ability.

Collapse
 
david_j_eddy profile image
David J Eddy

I'd like to add tmux to this list of helpful tools. Does similar thing as screen.

Collapse
 
awwsmm profile image
Andrew (he/him)

Thanks, Phil! I'll have to do some more research.

Watch out for the "unabridged" version of this in the future.

Collapse
 
xhiena profile image
xhiena

Yes, I was missing those on the SSH section, @andrew really awesome post

Collapse
 
sollyucko profile image
Solomon Ucko

FWIW, most of these aren't actually bash commands (only the builtins are); they're Unix/Posix/whatever commands.
Also, the prompt you use confuses me: it looks like there's an extra command (such as the ex command) at the start of the pipeline.

Collapse
 
bilus profile image
Bilus

Great reference, I would suggest changing the format of your prompt. Having the | symbol dividing the time and username is confusing on first look.

Collapse
 
rvprasad profile image
Venkatesh-Prasad Ranganath

Great list!! I would have added "type" and "tmux" to the list :)

For folks interested in useful Unix tricks, Unix Power Tools book is a great source.

Collapse
 
kylerconway profile image
Kyle R. Conway

Impressive post! I remember when I found ncdu it was an amazing day. It's by far my favorite tool to find what's taking up my disk space! I really hope more people use it as a result of your excellent post!

Collapse
 
awwsmm profile image
Andrew (he/him)

ncdu is great, it's what du should have been. Writing this post, I found a few other commands and flags that I forgot about / never learned about. One of my favourite ones is the -p flag to mkdir, which will create all missing intermediate directories! Small things like that can save you lots of time.

Collapse
 
darksmile92 profile image
Robin Kretzschmar

I think it's really awesome that you tought of the reader and added a "Back to TOC" after each topic for easy navigation!
And of course the content is very well explained with just enough words but much information in it :)

Collapse
 
jikuja profile image
Janne Kujanpää

Cool article but there are few things I need to disagree.

Environment variable creation

Using format foo=bar stores environment variable only current command. To keep environment variable loaded in your running bash process you need to export it with export foo=bar. This behaviour can be checked with commands env, printenv or by echoing environment variable.

source vs sh

Results of source and sh commands might look identical but they are not. Source command executes script in running bash and all exported envirenment variables are part of the running bash shell. sh invokes a new shell and exported variables are not usable in the calling shell. Also working directory of calling bash shell might be changed when using source command.

Please note that sh usually is linked to posh or some other lightweight shell which does not have all bash features.

Collapse
 
jikuja profile image
Janne Kujanpää

More about source

In some systems source and . are interchangeable command. (POSIX compatibility is swamp)

Anyway source command is good if you need (temporary) setup your environment: e.g. load environment variables, aliases and even request passwords from user with read -s <variable_name> or to load functions from external libraries.

Usually source is used in scripts(1) but I'm sure that users can find use cases when to use source to setup environment for running shell.

Collapse
 
brynsmith5 profile image
Bryn Smith

Good post! Do not run kill -9, but rather use just plain kill, or kill -15 if that doesn't work.
Kill and kill -15 end a process somewhat gracefully, so that the os is at least notified that the process is killed. -9 just causes it to vanish with no notice. It's akin to just hitting the power button, on a process level, without doing any sort of shutdown routine.
Also, ag (Silversurfer) is my team's grep replacement, give it a look.

Collapse
 
michaeltharrington profile image
Michael Tharrington

Exactly what I need right now...

Actually, more than I need, but in the best way, haha. Thx! 😄

Collapse
 
mrxavier profile image
Marcelo Xavier

Great article!! Thank you so much for the contribution!👏👏

Btw, the exercise about nohup yes just almost broke my PC by creating a file of 40GB in less than a min!!🫣😱
This is because nohup created a log file called nohup.out appending all output values spit by the command. So in very little time I had a HUGE file with 'y' on each line.

I advise you to change this exercise before brake others computer storage : )

Collapse
 
thomaswdmelville profile image
Thomas Melville

A great resource, thanks.

If the file you're looking for is not found with locate, you can run updatedb to update the list of files for locate.

Collapse
 
letsfindcourse profile image
letsfindcourse

This tutorial means that this is a brief introductory guide to SED that will help give beginners a solid foundation about concrete tasks.
letsfindcourse.com/tutorials/sed-t...

Collapse
 
ekynos profile image
Heiko Müller

Thank you for your impressive post!
And for mac users: If you want to use the very convenient ncdu command, there's a version available for Homebrew:


brew install ncdu

Collapse
 
superhawk610 profile image
Aaron Ross

rm -rf is commonly referred to as "rimraf" or "rimraf-ing".

Looking back it seems intutuitive but I didn't make that connection for the longest time.

Great article :)

Collapse
 
katiekodes profile image
Katie • Edited

Great post!

BTW, A similarly "for beginners" tutorial that I enjoyed, in book form, is M.G. Venkateshmurthy's "Introduction to Unix and Shell Programming," if anyone is still looking for additional material. (It gives fun "why" context to all of its "how" -- for example, it points out that the commands/flags of command-line interfaces are short and hard to remember because back when the only way to interact with your computer was a command line interface and only specialists used computers, the last thing programmers wanted to do was type a lot, so they just made up a bunch of cryptic short commands.)

Collapse
 
prennix profile image
Paul Rennix • Edited

WELL DONE!

set -x is a favorite of mine for debugging.

#!/bin/bash
set -x

this turns on line-by-line logic/command flow output and is great for debugging. It can be used inside a script or on the command line. If using on the command line, you'll want to turn it off at the end of the line. When used in a script, it's activity ends when the script does, or when unset with set -x

~ prennix$ set -x; for number in $(seq 1 3); \
do echo "Number ${number}"; done; \
if [ "${number}" == "3" ]; then echo "it's three"; \
else echo "this text is never printed"; fi; set +x

++ seq 1 3
+ for number in '$(seq 1 3)'
+ echo 'Number 1'
Number 1
+ for number in '$(seq 1 3)'
+ echo 'Number 2'
Number 2
+ for number in '$(seq 1 3)'
+ echo 'Number 3'
Number 3
+ '[' 3 == 3 ']'
+ echo 'it'\''s three'
it's three
+ set +x
Collapse
 
rishabh52510351 profile image
Rishabh Raj

Thank You for sharing such an informative post. I like the way you present the things and make the post more presentable. At PegaLogics, You will get the best industry-leading experience in the field of Mobile App Development & Web Designing. Visit our website, Regards- pegalogics.com/services/web-design...

Collapse
 
karuna24s profile image
Karuna Sehgal

Thank you for writing this post!

Collapse
 
awwsmm profile image
Andrew (he/him)

Hi everyone!

Thanks for the amazing response to this article! I've updated the command prompt so it's a bit less confusing. I hope to post an updated / revised / expanded version of this in the future, where I'll take everyone's suggestions below into consideration.

Thanks again!

Collapse
 
amr3k profile image
Amr

the picture associated with the xdg-open command appears to be from a Windows machine! how would it be possible?

Collapse
 
awwsmm profile image
Andrew (he/him)

Hi Amr,

Because I'm sshing into an Ubuntu machine from Windows using MobaXTerm. xdg-open (at least in this case) is using the default window styling from Windows.

Collapse
 
readyready15728 profile image
readyready15728

Good article. Should have mentioned ipython and trash-cli though. (Also probably in most contexts, for the Python 3 version to run, one has to enter in "python3", "ipython3", "pip3" etc.)

Collapse
 
udayvunnam profile image
Uday Vunnam

This is so good. I can use many of these.
Thanks for sharing!!

Collapse
 
atiqulhaque profile image
Md.Atiqul Haque

Very useful article. Thanks Andrew for this article.

Collapse
 
kaxi1993 profile image
Lasha Kakhidze

Awesome post!!! Thanks Andrew.

Collapse
 
djangotricks profile image
Aidas Bendoraitis • Edited

Really remarkable article!

But it seems that top uses the capital -U for the username. At least on Mac OS and FreeBSD.

Collapse
 
moopet profile image
Ben Sinclair

df takes a --max-depth=N

You meant du there, I think.

Collapse
 
awwsmm profile image
Andrew (he/him)

I did, thanks! It's fixed now.

Collapse
 
jikuja profile image
Janne Kujanpää

Bonus tip for date command: date +%s prints unix time (in seconds)

Collapse
 
ozzyogkush profile image
Derek Rosenzweig

Nice reference, thanks!

Collapse
 
gilangaramadan profile image
Gilang A. Ramadan

Wohoo awesome, thanks!

Collapse
 
captainawesomedi profile image
Di Wu

sick post! need these

Collapse
 
vignesh0025 profile image
Vignesh D

Great. But that Pipe operator as prompt in the command snippet seems very confusing.

Collapse
 
jochemstoel profile image
Jochem Stoel

Epic.

Collapse
 
dance2die profile image
Sung M. Kim

Thank you, Andrew.

A great article written at the perfect time (I started using Ubuntu recently) 👍

Collapse
 
njkevlani profile image
Nilesh Kevlani

Amazing article, got to learn many new thing :)

Collapse
 
johndemian profile image
John Demian

Sweet Jesus, this is awesome!
I took note of some of these, can't wait to show off in front of my peeps back at the office.

Collapse
 
vintharas profile image
Jaime González García

WoW

Collapse
 
namraj profile image
Nava

This post is a great effort to cover most cli commands.It can help many to get started with command line.

Collapse
 
alexandlazaris profile image
Alexander Lazaris

Awesome stuff Andrew! Funnily enough I missed the title (and the URL ...) and was pleasantly suprised by how many different commands you listed. Nice work

Collapse
 
prathaprathod profile image
Prathap Rathod

Thanks Andrew

Collapse
 
wolfhoundjesse profile image
Jesse M. Holmes

I will use this, thanks! Now, if posts a similar article for PowerShell, I'll be done. lol

Collapse
 
chris_bertrand profile image
Chris Bertrand

That's a monster post!

Collapse
 
elliotd123 profile image
elliotd123

This is an awesome list - but it should be noted that most of these are not bash commands. They are just common programs. Many of them are therefore usable in any shell, not just bash.

Collapse
 
olsard profile image
olsard

Great list! Thanks for sharing.

Collapse
 
lorenzoblog profile image
Collapse
 
alexgeorgiev17 profile image
Alex Georgiev

This is really good post! Everyone that is interested in Linux should save it and revisit it once in a while! Great content!

Collapse
 
jay97 profile image
Jamal Al

This is very long 😐

Collapse
 
teqhow profile image
David Smith

Your article is valuable to me and to others. Thank you for sharing your information for my new blogging website teqhow.com!

Collapse
 
slidenerd profile image
slidenerd

hard links vs soft links is best answered here askubuntu.com/questions/108771/wha... nice post!

Collapse
 
muja profile image
Muja Siyam

awesome, keep it up

Collapse
 
bobbyiliev profile image
Bobby Iliev

Great article! 🙌

You should check out this Open-Source Introduction to Bash Scripting Ebook on GitHub as well!