DEV Community

10 Terminal Commandments for Coding Newbies

Taylor Overturf on December 05, 2021

The terminal can be an intimidating place for anyone, like me, who has spent most of their life living in a โœจGraphical User Interfaceโœจ paradise. ...
Collapse
 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ

touch is actually for updating the file access or modification time. The fact that it happens to create the file if it doesn't exist is merely a convenient side effect

Collapse
 
projaymmer profile image
Jay Nguyen

Oh shoot. My course did not share that fact. Thanks, Jon!

Collapse
 
stackoverturf profile image
Taylor Overturf

Thank you so much, Jon Randy! I appreciate that you took the time to comment.

Collapse
 
aschwin profile image
Aschwin Wesselius

I like that you use the word "commandments" instead of commands. At least these are not written in stone tablets....

It might help to mention that this goes only for POSIX compliant operating systems. Not for Powershell or DOS prompts.

But I agree that it can be daunting to start working with the terminal on UNIX/Linux/OSX/FreeBSD etc.

However, the power lies in the fact that these commands can be executed from scripts and thus you can program any flow of execution. This can't be done in a GUI without bothering setting up some advanced GUI interpretation tool. Which doesn't scale properly is mundane and error prone when the GUI changes.

The terminal hardly changes. These tools have been around for decades and decades.

To me, the productivity rises when I can automate stuff from the terminal using scripts. Heck, this is where most DevOps etc. finds its own roots.

Nice to see that even newbies dare to look into it, picking it up and even writing posts about it!

Collapse
 
napcoder profile image
Marco Travaglini

You know the sensation when you need to re-execute that very complicated command you run few days or week ago and you cannot remember anything but a word? history | grep that-word saved my life a lot of time

Collapse
 
rbgrouleff profile image
Rasmus Bang Grouleff

In Bash you are also able to search in your history with CTRL+r :)

Collapse
 
napcoder profile image
Marco Travaglini

I have to remember this one, thanks

Collapse
 
aschwin profile image
Aschwin Wesselius • Edited

Some more very useful things on the terminal. Be ready, since these might shock you!

The history command has been mentioned before. It will indicate also a number in front of the commands entered. If you take this number after the exclamation mark it will execute that command again. You can even pipe these.

Let's say from Taylor's history command we take entry 14 and 15, to create the folder and to list the content inside the current folder:

$ !14 && !15
Enter fullscreen mode Exit fullscreen mode

It should execute both commands (mkdir myFolder and ls) consecutively. The && is used to make sure the command ends properly first before trying to execute the next command. This is somewhat advanced Bash, but still very helpful to understand and use.

Ok, another useful feature in Bash, is the up arrow. This way you can "browse" a bit in your history. Probably you already know this, but if not this is a God send! This way you can find the command that was working, edit it a bit and hit ENTER again to execute it. If you have to do some commands over and over again a couple of times with some slight changes, this helps out a lot of the repetitive and boring things. Boring means loss of concentration, loss of concentration means less fun. We don't want boring, we want productivity and results. We can do this!

And never forget to use the TAB autocompletion functionality!

For advance use of the terminal, the Z-shell (zsh) is quite powerful where most commands have autocompletion, syntax hightlighting and better display of common elements etc. This way, using Git will be a breeze because the autocompletion gives even suggestions to the possible arguments you might want to enter.

Have fun and hack the planet!

Collapse
 
agredalex profile image
Alex รgreda

Another neat trick you can use is !! to get the last command you executed before, this is pretty convenient whenever you want to execute it but with another command, example:

you just used
git status

and now you want to add everything to stage and see the status you can type
git add . && !!

and the shell will "translate" it to
git add . && git status

Collapse
 
goodevilgenius profile image
Dan Jones

This is very unclear on how to use ... It's not a command itself. If you try it, you'll just get a "command not found" error.

You need cd ..

Also, cd by itself will send you back to your home folder. Normally, you need to put the directory name in there: cd subfolder

Collapse
 
m1keym profile image
Mikey

I have recently started using the command line and this made me chuckle especially there 'where am i' and was extremely informative as well.

I've come across a problem I've yet to be able to fix, I wonder if you might have a solution.

When you're working with git and you're about to commit and type out a message but you forget one side of the "" around the message - my terminal goes wild and I haven't been able to figure out what to do except close and start again which is annoying.

Let's say I type git commit -m "initial commit

and press enter without closing the quotation mark I get just get

and nothing I press works, is there are cmd to fix it?

Collapse
 
aschwin profile image
Aschwin Wesselius

Most of the time you can just add the closing quotation mark. It might include the tailing carriage return/linefeed (\n) into the commit message, but that's not that much damage done right? You can also update the commit message, which I hardly do so I don't know how to do it without looking it up for you.

Collapse
 
agredalex profile image
Alex รgreda

You can edit your last commit message with git commit --amend

Collapse
 
kamilzag profile image
KamilZag

Have you tried
Ctrl + c
?
It should help i think

Collapse
 
stackoverturf profile image
Taylor Overturf • Edited

Hi Mikey, I know exactly what you're talking about and I have made the mistake of forgetting my ending quotation mark many times!

I agree with KamilZag, CRTL + C will allow you to return to the normal command line and get out of that weirdness.

I also just saw Aschwin's great explanation, yes, if you notice your error soon enough, I have found that I can add the ending " even if I've pressed enter. However, I did not know I could update my commit message, so thank you for that!

Thread Thread
 
m1keym profile image
Mikey

Thank you all so much for your help.

I've done it a few times and then I just end up staring blankly at the screen like 'WTF do I do' hit a few keys then close the terminal, but then I have to navigate all the way back to where I was because I don't know any other way. I contemplated Googling it but couldn't think of what to ask without writing an essay.

I appreciate the help! :)

Collapse
 
nueeardente profile image
Edgar Carlos

Thank you for this article.
For me, the most indispensible commands (besides some of the ones in this article) are:

  1. cat combined with grep -i (when you are quickly looking for something or some pattern in a file)
  2. grep (many regex options)
  3. tail (when going through logs in linux)
  4. more
  5. which and last but not least
  6. man (for looking up how to use the commands mentioned here)
Collapse
 
dozykeys profile image
Duru Chidozie

it's worth it...thumbs up

Collapse
 
goodevilgenius profile image
Dan Jones

I think you've confused iTerm or Terminal.app with "terminal". Cmd-t is an iTerm (and other terminal emulator) shortcut for opening a new tab. It's not universal, and has nothing to do with the terminal itself, but rather, with the terminal emulator you're using.

Collapse
 
projaymmer profile image
Jay Nguyen

These are some dope commandments, bruh. Unlike the Almighty, I shall follow you. Thanks for sharing.

Collapse
 
xiaoeyun profile image
Xiao_e_yun

No whereami,but you have whoami

Collapse
 
mrwensveen profile image
Matthijs Wensveen

alias whereami=pwd

Collapse
 
astorrer profile image
Aaron Storrer

How about "Commands newbies should never type." Or, most dangerous powerful commands.

rm (or anything with -r or -R)
chmod
chown
mv (You did mention it's use but not why to fear it.)

Collapse
 
sturlabragason profile image
sturlabragason

Nice post! My favorite is CTRL+R ๐Ÿ”ฅ

Collapse
 
dozykeys profile image
Duru Chidozie

Nice read...it was worth it

Collapse
 
drvy profile image
Dragomir Yordanov

Note: CTRL + C does not "clear" the terminal. It sends an "Interrupt" (SIGINT) signal to the current program. Also, the program can chose to ignore it.

Collapse
 
moscow0201 profile image
UCan

thank you so much

Collapse
 
thiagomg profile image
Thiago Massari Guedes

A nice shortcut for exit is Ctrl+D - It is also satisfying for lazy people such as myself

Collapse
 
whobeu profile image
Robert G. Schaffrath

I usually set IGNOREEOF=4 (or higher) due to having gotten logged out too many times accidentally. Type CTRL+D to exit an application and it seems unresponsive so I hit it again. Then it actually wakes up and I have entered two CTRL+D's with the net result being the shell getting the second CTRL+D LOL.