DEV Community

Cover image for [1/4] Beginners BASH basics - becoming one with the shell
Hayden Rouille
Hayden Rouille

Posted on • Edited on

[1/4] Beginners BASH basics - becoming one with the shell

Part 1 of a 4 part series

View part 2 here and part 3 here

When I first started using the command line it seemed quite daunting to me. I didn't know what I should know and I didn't know what benefits investing time into learning more would give me. My job required me to learn more Ruby, so why learn bash?

bash who?

I have since learnt that having knowledge of just some basic commands improves your workflow tenfold, and for me in particular increased my confidence when sshing to a server or even just having to navigate around my own environment.

By the end of this series I hope you'll feel at home when navigating the command line and won't feel a need to use the file manager GUI again!

Here are some of my most commonly used commands:

man

man provides you with the manual documentation for a utility. Use man the same way you would when using programming documentation - it will tell you all the available flags for a command and what it can be used for. The main source of truth when questioning how to use a tool!

Example usage: man ls (use q to exit)

Equally, you could also use info ls to view this information too.

ls

ls will list the files in your cwd (current working directory).

If you use man ls, you can see that ls offers many flags. The most common usage however, are (in my opinion) lah. Executing ls -lah will give you a list of all files in your CWD (current working directory) including hidden (.files), with their file-sizes, permissions and dates when they were updated.

Example usage: ls -lah

You can then use the file command on a file to view the details of it!

cat

cat will spit the contents of a file out to the command line.

Example usage: cat myfile

mkdir

mkdir creates a directory at the specified location. As with ls, mkdir has many useful flags that you can see in the manual. The most notable being -p, which will recursively create the directories as specified.

Example usage: mkdir -p ~/my_directory/my_subdirectory

rm

rm is used to remove a file or directory.
Warning: paired with sudo, or even on it's own, this can be a very dangerous command if used incorrectly.

You may want to use the following flags:
-r for recursive
-v for verbose (it will give feedback to what is happening)
-f for force

Example usage: rm -rvf ~/asd

grep

grep will allow you to search for specific content from a file or result list.

Example usage: grep 'my_search' test.md, you will see all the lines containing my_search within test.md.

You can view lines around them by using the -C 3, or get all lines not containing the match with -v.

sudo

sudo will execute the following command as a superuser. You'll often need to use sudo to run certain commands, for instance commands that will affect the system.

Example usage: sudo systemctl status mysql

conclusion

And that's it for part 1 of my beginners bash series! If all of the above is below you... stay tuned for some more advanced tips!

Dive into curl, chmod and more in Part 2 - here

Feel free to ask any questions or if you're interested in how I've customised my environment, head over to my repository below.

GitHub logo haydenrou / dotfiles

My Personalised Development Environment

Top comments (10)

Collapse
 
jcolombo profile image
Juan Colombo

Great!,now I'm Hackerman.

Collapse
 
webreflection profile image
Andrea Giammarchi

(use q to exit)

if there's something worth a meme, is people trapped inside less, vim, etc ... where q is for quit, or at least that's how I used to remember how to exit.

(use q to quit/exit) is what I'd write, as it's the most common key to quit many programs ;-)

Collapse
 
hayden profile image
Hayden Rouille

Very true, a missed meme opportunity!

Collapse
 
hayden profile image
Hayden Rouille

Absolutely, they'll be coming up soon. Thanks!

Collapse
 
fennecdjay profile image
Jรฉrรฉmie Astor

Thank you. Can I suggest you add info?
Things like info gcc we're a game changer when I found this command.

Collapse
 
hayden profile image
Hayden Rouille

Sure! I chose man because it's more widely known, but I'll add info below.

Collapse
 
bohyun611kim profile image
bohyun611kim

Absolutely Coooooool~

Collapse
 
maxart2501 profile image
Massimo Artizzu

I'm a simple person.
I see a Kung Fury reference, I click on โค๏ธ.

Collapse
 
jpalala profile image
jose palala

For beginners, tldr.sh/ is also really useful :) It's like man pages but more succinct!

Collapse
 
hayden profile image
Hayden Rouille

That's a cool package, I'll have to try it out myself!