loading...
Cover image for Helpful Terminal Commands for Beginners!

Helpful Terminal Commands for Beginners!

milkstarz profile image malik Updated on ・1 min read

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 started working with more terminal-oriented applications, there are several commands that I use that I'd love to share with beginners and terminal lovers alike.

Note: This article assumes that you're using some type of UNIX shell, with a preference towards Mac. Some of these commands may not work on Windows CMD/Powershell.

Basic Commands

Commands in a shell can be used in a ton of different ways - but I'd say that the important ones to learn fall under three main categories:

  1. Navigating and Working with Files & Directories
  2. Manipulating Output for Input
  3. Finding Things

Navigating and Working with Files & Directories

On a computer, files and directories (otherwise known as folders) are responsible for managing information. Here are some commands to make your life easier when working with files:

  • cd - navigate to different directories
  • pwd - see the name of the current directory
  • ls - lists all files in the current directory
  • mkdir - make a new directory
  • touch - make a new file
  • cp - copy a file
  • mv - move a file or directory
  • rm - removes a file or directory
  • zip - compresses files into a zip archive
  • unzip extracts files from a zip archive
  • chmod - Allows you to make a file executable and change permissions granted to it by your machine.
    • In order to make a file executable, you can type chmod +x [name of file]

  • tar - Allows you to work with tarballs in a Linux command line. It has a long list of uses, including package management for systems.
    • tar -cvf allows you to create a .tar archive
    • tar -xvf allows you to "untar" a .tar archive
    • tar -tvf list the contents of a .tar archive

Honorary Mention: sudo

sudo is a very widely used command in bash interfaces that allows you to run a command with administrative or root privileges.

For example, if you edit any files in the root directory (also known with the path /) of your machine without sudo privileges, you will be denied permissions to edit the file.

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.

Manipulating Output for Input

Often times when scripting the goal you are trying to achieve is to run filters through text, check output logs from a server, or run batch commands on multiple files.

Here are some commands that can be beneficial for manipulating file output:

  • cat - show a file's contents
  • head - displays the first few lines of a file
  • tail - displays the last few lines of a file
    • Using the -f flag with tail will allow you to see updates to the file as they are coming in. This is extremely helpful for tracking log output from servers.

  • | - The pipe character take a command's output and uses it for the input of another command

  • * - matches zero or more characters in a filename
    • There is actually a whole way of matching multiple files and names via globs!

  • ? - matches any single character in a filename

  • > - stores a command's output to a file, and creates a new file if one doesn't already exist
    • WARNING: This will override and replace all contents that were in the original file.

  • >> - concatenates a command's output to a file, creates a new file if none is found

Finding things

  • echo - show text or the compiled text of other commands. Great for testing commands out.
  • grep - allows you to find results line by line using some sort of pattern or regular expression.
  • ack - similar to grep, but has a better code search because it knows to search in places you'd expect it to search. (ignores version control directories, etc.)

Other helpful commands I've found

  • pbcopy - Copy selected text into your clipboard buffer
  • history - Prints the command history for your session
    • This is really useful with grep and the pipe symbol, which will allow you to search through your history: history | grep [part of command]

  • pbpaste - Paste the selected text in the terminal from your clipboard buffer

  • curl - Allows you to make HTTP calls RESTful endpoint

  • kill - Kills a running process given a process id (pid)

  • killall - Kills all running processes of a certain type

  • lsof -i tcp:[port number here] - list all running processes on a specific port
    • This one is particularly useful for when you get an error like:
    Error: listen EADDRINUSE :::3005
        at Object.exports._errnoException (util.js:1023:11)
        at exports._exceptionWithHostPort (util.js:1046:20)
        at Server._listen2 (net.js:1261:14)
        at listen (net.js:1297:10)
        at Server.listen (net.js:1375:9)
        at Object.<anonymous> (/path/to/node/server/server.js:15:34)
        at Module._compile (module.js:571:32)
        at loader (/path/to/node/modules/node_modules/babel-register/lib/node.js:144:5)
        at Object.require.extensions.(anonymous function) [as .js] (/var/www/html/gcsbpo/rocc/node_modules/babel-register/lib/node.js:154:7)
        at Module.load (module.js:488:32)

It provides an easy way to find out the pid to kill the running process.

Shortcuts/Tips & Tricks

  • !$ expands the final argument of the previous command. Useful for chaining commands on the same file
  • !! - repeats the last command executed
    • These are thanks to Thomas
    • ![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 at the end of the command.
    • [extra stuff]!!: Re-execute last command, tacking on [extra stuff] at the beginning of the command. Very handy for executing something as a regular user that needs to be ran 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

  • clear - clears all output from the terminal, old output - can still be accessed by scrolling up

  • CMD + k - clears all output from the session, only previously called commands are retained

  • CTRL + c - Aborts the current running process and closes it.

  • CTRL + z - Pauses (SIGSTP) any the current running process
    • NOTE: CTRL + C aborts the process, but CTRL + z leaves it idling in memory.

  • CTRL + a - Takes you to the beginning of the bash input

  • CTRL + e - Takes you to the end of the bash input

  • CTRL + u - Clears all input before the cursor

  • CTRL + r - Opens a prompt that will search the previous commands from your session

Conclusion

Learning how to navigate the terminal is definitely daunting at first, but after spending some time scripting you'll find that these commands can save you tons of time on a day to day tasks that you do.

Personally, I'm starting to reach the point where I navigate faster via Terminal than when I use Finder.

If you have any commands, shortcuts, or tips I should add to this post, please leave me a comment! Would love to add more.

Posted on Sep 5 '18 by:

milkstarz profile

malik

@milkstarz

your everyday full stack engineer, foodie, and reader of things.

Discussion

markdown guide
 

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!

 

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 πŸ˜„

 

! 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).

 

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.

 

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.

 

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

 

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!

 

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.:

./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. =)

 

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

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

 

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 :)

 

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.

 

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

 

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

 

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.

 

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.

 

I'll confess... I didn't know about head and tail. #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:

  • 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

 

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.

 

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.

 

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

 

This is brilliant. Thank You.

 

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!

 

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