DEV Community

Cover image for Helpful Terminal Commands for Beginners!

Helpful Terminal Commands for Beginners!

malik on September 05, 2018

This cheat sheet was originally posted on malikbrowne.com. I love learning new commands and tools to optimize my workflow on my Mac. Since I have...
Collapse
 
zeddotes profile image
zeddotes

! is pretty cool when used with history. If you enter history, you could use the reference number for any command from the list, and reuse it by doing: !123, where 123 is the number. Also, you could pipe history (e.g, history | grep sudo to find all commands in history with sudo).

Collapse
 
hoelzro profile image
Rob Hoelz

Great writeup, Malik - thanks for sharing! Since you asked for some tips, let me share two of my favorites:

First is !$ - it's similar to !!, but instead of expanding to the entire previous command, it just expands to the final argument of the previous command. It's really useful for chaining commands that operate on the same file; here's one example that I find myself using a lot:

$ mkdir foo
$ cd !$

The second is the noclobber shell option, which is present in both bash and zsh. You mentioned above that > will destroy a file if you redirect to a file that already exists - set -o noclobber prevents that! I can't tell you how many times this has prevented me from destroying work!

Collapse
 
moopet profile image
Ben Sinclair

I use noclobber everywhere and then use >| if I know I want to overwrite something, because we've all done that...

Collapse
 
milkstarz profile image
malik

Oooh! Definitely adding these to the list. Thank you for sharing it’s part of the reason I wanted to write this post πŸ˜„

Collapse
 
codemouse92 profile image
Jason C. McDonald

Great post!

Just FYI, Ctrl+z is more than just a stop - it's a pause (specifically SIGTSTP) for the currently running process; by contrast, Ctrl+c is an abort (specifically SIGINT), which ends and closes the process.

After typing Ctrl+z, you should generally decide where you want to run the process by typing either fg (to bring it back to the foreground and resume it) or bg (run it in the background); if you don't do either, then it'll just hang out in memory waiting for permission to continue.

You can see the complete list of jobs running on that shell by typing jobs; their status is listed there as well. To resume a job, typing %n where n is the process number in the jobs list.

Long story short, you don't want to confuse Ctrl+z and Ctrl+c: the latter aborts the process, but the former leaves it idling in memory.

Collapse
 
jeikabu profile image
jeikabu

Beat me to it. ctrl+z, bg, and fg were indispensable to me when I started.

Maybe less so now that everyone starts out in xwindows, but I still forget a trailing & every now and then.

Collapse
 
ferricoxide profile image
Thomas H Jones II • Edited

When it comes to shell history, you're doing yourself a grave injustice if all you're using is !!. Other fun ones are:

!<number>: Repeat command from your history (you can see the available command-history using fc -l (or fc -l 1 to see all the contents of your history)
!! <extra stuff>: Re-execute last command, tacking on <extra stuff> at the end of it
<extra stuff>!!: Re-execute last command, tacking on <extra stuff> at the beginning of it ...which is great if you executed something as a regular users that turns out to have needed to be run with sudo
!!:s/<SEARCH>/<REPLACE>: Re-execute last command, replacing first substring <SEARCH> with <REPLACE>
!!:s/<SEARCH>/<REPLACE>: Re-execute last command, replacing all substring <SEARCH>es with <REPLACE>es

Collapse
 
milkstarz profile image
malik

I didn’t know about the search and replace functionality of !!

That is going to be extreeeemely useful for me personally haha, thank you for sharing!

Collapse
 
ferricoxide profile image
Thomas H Jones II

Yeah. BASH is pretty much "take the best from KSH93 and TCSH ...plus, in recent years, a few others". So, it has a lot of features in it. Used to be, when I was tutoring junior systems administrators, all they really new about was filec. Most were astounded when I'd show them all of the wonders of BASH ...even though my preferred shell was still KSH.

Bash is kind of to shells what English is to language. =)

Collapse
 
ginniecodes profile image
Jhinel Arcaya

I really like to use history | grep partofacommand when I don't remember something I did before.

Collapse
 
jeikabu profile image
jeikabu • Edited

I love the shell. When I learned OSX had a "proper" shell I never went back to linux.

open is like double-clicking a file in the UI. open . to get a Finder window in cwd.

esc+. repeat last part of previous command

&& is used in an alias below, but is handy if you've got a few time consuming commands that you want to run while you go get coffee. E.g.:

./configure && make -j4

ctrl+k delete to end of line

pushd and popd save a lot of hassle. pushd /some/deep/path to switch directories, then popd to go back to where you started.

If you work with remote machines a lot:

ssh. Particularly using "key-based authentication" (google it) and X11 forwarding (-X but I believe enabled by default now)

screen will change your life. Ssh into remote machine, screen. Do a bunch of work screen -d and logout or go home or whatever. Ssh back in and screen -r to continue where you left off.

There's tons more. I've been using bash for years and still learn new things from time to time. =)

Collapse
 
milkstarz profile image
malik

Ahhh this is awesome!! Adding these to the list.

I totally forgot about && and & in this list, I use it all the time πŸ’―

Collapse
 
nickytonline profile image
Nick Taylor

I'll confess... I didn't know about head and tail. #DevConfessions πŸ˜‚

Collapse
 
milkstarz profile image
malik

Hahaha wellll I only learned about them like a month ago when I originally wrote this so I’m definitely with you πŸ˜…πŸ˜‚

Collapse
 
banminkyoz profile image
Kyoz

Really cool post, Malik. :D

I do love to use terminal too. I'd like to share you my dotfiles. I think i'll update some commands in this post to it :)

You know, I love to create alias for most my most used command. Like:

  • c instead of cd
  • mkd to make a forler and jump in to it right away
  • gh to jump to my github folder, pj to jump into my project folder
  • ni, ns for npm install, npm start
  • gs, ga, gc "message", gp, gpush... instead of git status, git add ., git commit "message", git pull, git push...etc

And a lot of them. When i have to use a command so much, i'm always want to make an alias for it, i found it's very useful. :) So what's your opinion, do you use alias too ?

Maybe i'll do a post to share how i boosted productivity with terminal tomorrow :D

P/s: i don't know why `` not work so above command is a little to see :D

Collapse
 
milkstarz profile image
malik

try using three tick marks instead of two. Thanks for sharing, I'll add some of those to my bash_profile :)

Collapse
 
robertcoopercode profile image
Robert Cooper

Great list of bash commands. This encourages me to try and use them more often. I can especially see how tail -f can help during debugging.

Collapse
 
enguerran profile image
enguerran πŸπŸ’¨ • Edited

I think it is the good time to share this wallpaper (Like itsfoss.com I did not succeed to find the source of the file, sorry for the author).

bash cheat sheet wallpaper

Collapse
 
eljayadobe profile image
Eljay-Adobe

In the terminal I use, the clear command does not erase the scroll-back lines.

So in my ~/.bash_profile (since I use the bash shell), I add this alias to clear the screen and erase the scroll-back lines:

alias cls="clear && printf '\e[3J'"

Alas, since there is no tput to erase the scroll-back lines, the escape sequence may vary by terminal. This sequence works on all the terminals I use.

Most of the rest of my aliases relate to how I do software development and interact with my editor, compiler, and source control.

And a few deal with ls and ll for the command options I prefer, or shortcuts to quickly do a cd command to a directory of interest.

Collapse
 
milkstarz profile image
malik

I like that cls command. The CMD + k shortcut i wrote about in the article will do exactly what you said too :)

Collapse
 
jshamg profile image
jshamg

I would add some things.

"top" or "htop" is something like a taskmanager
"kill" to kill a prozess
"killall" to kill all recources of a program

"| less" , u already explained the pipe but the less outputs everything with moving around with arrowkeys. If you dont have a graphical environment thats essential.

essential are also the package manager commands but they depend on the system...

But anyways, a really great guide for beginners, i can imagine that helps a lot...

Collapse
 
milkstarz profile image
malik

I totally forgot about kill and killall! Adding it to the list. Thanks for sharing those with me :)

Collapse
 
moopet profile image
Ben Sinclair

For your safety, there are a couple of things your computer will not allow you to do as a sudo'd command, i.e. running bash scripts.

I was re-reading this post and just noticed this bit about sudo. What did you mean? Because I can definitely run sudo ./my-cool-script.sh with no problems.

Collapse
 
milkstarz profile image
malik

I meant this for devices that you don't have admin access to by default! Many work machines have this in place to follow The Principle of Least Privilege.

In my experience this has really only been an issue at big corporate companies.

Collapse
 
scorp13 profile image
scorp13

Here is my cheat sheet for some Linux terminal commands which I use most often. I think I "borrow" some from your guide to complement it, if you don't mind.

Collapse
 
jmtiong profile image
Jia Ming

Thanks for sharing this cheat sheet!

To add on for Ctrl + r, after typing something, you can also cycle through all the previous related commands just by repeating press Ctrl + r!

Collapse
 
milkstarz profile image
malik

Ahhh i forgot that I use that all the time!! Thanks for sharing/reminding me about it :)

Collapse
 
bjhaid_93 profile image
/[Abejide Femi Jr]\s/ • Edited

these shortcuts are helpful too
CTRL + l - clear terminal
CTRL + SHIFT + c - copy
CTRL + SHIFT + v - paste

Collapse
 
adeelhuss94 profile image
A.

This is brilliant. Thank You.