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...
For further actions, you may consider blocking this person and/or reporting abuse
!
is pretty cool when used withhistory
. If you enterhistory
, you could use the reference number for any command from the list, and reuse it by doing:!123
, where123
is the number. Also, you couldpipe
history
(e.g,history | grep sudo
to find all commands inhistory
withsudo
).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: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!I use
noclobber
everywhere and then use>|
if I know I want to overwrite something, because we've all done that...Oooh! Definitely adding these to the list. Thank you for sharing itβs part of the reason I wanted to write this post π
Great post!
Just FYI,
Ctrl+z
is more than just a stop - it's a pause (specificallySIGTSTP
) for the currently running process; by contrast,Ctrl+c
is an abort (specificallySIGINT
), which ends and closes the process.After typing
Ctrl+z
, you should generally decide where you want to run the process by typing eitherfg
(to bring it back to the foreground and resume it) orbg
(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
wheren
is the process number in thejobs
list.Long story short, you don't want to confuse
Ctrl+z
andCtrl+c
: the latter aborts the process, but the former leaves it idling in memory.Beat me to it.
ctrl+z
,bg
, andfg
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.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 usingfc -l
(orfc -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 withsudo
!!: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>
esI 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!
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. =)
I really like to use
history | grep partofacommand
when I don't remember something I did before.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.:
ctrl+k
delete to end of linepushd
andpopd
save a lot of hassle.pushd /some/deep/path
to switch directories, thenpopd
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 workscreen -d
and logout or go home or whatever. Ssh back in andscreen -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. =)
Ahhh this is awesome!! Adding these to the list.
I totally forgot about && and & in this list, I use it all the time π―
I'll confess... I didn't know about
head
andtail
. #DevConfessions πHahaha wellll I only learned about them like a month ago when I originally wrote this so Iβm definitely with you π π
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:
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
try using three tick marks instead of two. Thanks for sharing, I'll add some of those to my bash_profile :)
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.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).
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
andll
for the command options I prefer, or shortcuts to quickly do acd
command to a directory of interest.I like that
cls
command. The CMD + k shortcut i wrote about in the article will do exactly what you said too :)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...
I totally forgot about kill and killall! Adding it to the list. Thanks for sharing those with me :)
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.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.
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.
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 pressCtrl + r
!Ahhh i forgot that I use that all the time!! Thanks for sharing/reminding me about it :)
these shortcuts are helpful too
CTRL + l - clear terminal
CTRL + SHIFT + c - copy
CTRL + SHIFT + v - paste
This is brilliant. Thank You.