CoderCo

Share this post

Bash tips and tricks every developer should know!

blog.coderco.io

Discover more from CoderCo

A newsletter for Software, Systems and DevOps
Over 3,000 subscribers
Continue reading
Sign in

Bash tips and tricks every developer should know!

Coder Co
Mar 29, 2023
3
Share this post

Bash tips and tricks every developer should know!

blog.coderco.io
Share

For any developer or DevOps engineer, learning the command line is essential. The command line can help you execute tasks faster and more efficiently, and Bash is a popular shell to use in Unix-like systems such as Linux and Mac. In this article, we'll cover ten Bash command line tips that can help you level up your skills.

Thanks for reading CoderCo! Subscribe for free to receive new posts and support my work.


1. Use `awk` to extract data from text files

The `awk` command is used to manipulate and extract data from text files. For example, if you have a text file with the following contents:

Name Age Gender
John 30  Male
Mary 25  Female

And if you want to extract only the names and ages, you can use awk '{print $1,$2}' file.txt This will output:

Name Age
John 30
Mary 25

2. Use `sort` to sort the contents of a file

The sort command sorts the contents of a file. For example, if you have a file with the following contents:

dog
cat
bird

And if you want to sort it alphabetically, use sort file.txt. The output will be:

bird
cat
dog

3. Use head and tail to display the beginning and end of a file

The head and tail commands are used to display the beginning and end of a file, respectively. For example, if you want to display the first 10 lines of a file, use head -n 10 file.txt. If you want to display the last 10 lines, use tail -n 10 file.txt

4. Use the cd - command to go back to the previous directory

$ cd /usr/bin
$ cd - 

5. Use the Ctrl+R keyboard shortcut to search your command history.

If you want to search your command history for a specific command, you can use CTRL+R shortcut:

(reverse-i-search)`ku': kubectl run nginx --image=nginx

As you can see, using CTRL+R, I only needed to type ku and it found me the kubectl command I was looking for

6. The !! command to repeat the previous command

Have you ever typed a long command only to realize you forgot sudo?

Instead of typing the whole command again, you can use !! to repeat the previous command with sudo.

$ apt-get install nginx
E: Could not open..

$ sudo !!
Installing..

In the above case the !!after the sudo basically translates to apt-get install nginx

7. Some CTRL+<letter> shortcut

CTRL+A to go to the beginning of the command

CTRL+E to go to the end of the command

CTRL+U to delete the whole line/command

CTRL+C to stop the current running process

CTRL+D to exit the terminal

8. Use the ping command to test network connectivity

If you want to test network connectivity to a specific server or IP address, you can use the ping command.

ping google.com

9. Brace expressions to create files in bulk

When working on a project, you might find yourself needing to create multiple files or directories at once. This can be a tedious and time-consuming process if you do it manually.

However, with brace expansion, you can create files and directories in bulk with just a few commands.

Creating files in bulk

To create multiple files with similar names, you can use brace expansion. For example, to create 100 files with names file1.txt, file2.txt, file3.txt, and so on, you can use the following command:

$ touch file{1..100}.txt

Similarly, if you need to create three files for your project named app.html, app.css, and app.js, you can use brace expansion to create all of them in one go:

$ touch app.{html,css,js}
$ ls
app.html app.css app.js

Creating directories in bulk

To create multiple directories at once, you can also use brace expansion. For example, if you need to create five directories named images, css, src, templates, and scripts, you can use the following command:


$ mkdir {images,css,src,templates,scripts}
$ ls
images css src templates scripts

It's important to note that there should be no spaces between the words inside the braces, as this can cause errors.

These are just a few of the many Bash command line tips and tricks that you can use to make your life easier. By learning these shortcuts and commands, you can become more efficient and productive in your work.

Thanks for reading CoderCo! Subscribe for free to receive new posts and support my work.

3
Share this post

Bash tips and tricks every developer should know!

blog.coderco.io
Share
Comments
Top
New
Community

No posts

Ready for more?

© 2023 CoderCo
Privacy ∙ Terms ∙ Collection notice
Start WritingGet the app
Substack is the home for great writing