DEV Community

Cover image for Bash commands introduction  🧑🏿‍💻
Killian Frappart
Killian Frappart

Posted on • Updated on

Bash commands introduction 🧑🏿‍💻

I just learned about some basic terminal commands and it was a great experience!

It might not be as instinctive as a graphical user interface, still there are some good reasons to start using your terminal.

🔷 High performance

🔷 Real control of what is happening under the hood

🔷 Task automation

Alt Text

Note: We will only walk through the basic commands of Linux/macOS terminal.

Get started

If you are using Linux or macOS as your operating system, you are very likely to use bash shell which is the most widely used shell.

Once you opened up your terminal, should have something that look like this:

Alt Text

Navigation

The cd (change directory) command lets you navigate through your computer's file system.

# Go to target folder
cd /target

# Go to parent folder
cd ..

# Go to home folder
cd ~
Enter fullscreen mode Exit fullscreen mode

List

The ls (list) command print files and directories.

# Print files located in the current directory.
ls

# Print files located in the target directory
ls targetparent/target
Enter fullscreen mode Exit fullscreen mode

Options

You can add some options to most of existing commands by simply appending your command with -[option]

For example:

# Print files located in the current directory with more details.
ls -l

# Print every files located in the current directory (hidden files as well).
ls -a
Enter fullscreen mode Exit fullscreen mode

Make/remove file or directory

The mkdir (make directory) command create a new directory.

# Make a new directory in current directory.
mkdir directoryName

Enter fullscreen mode Exit fullscreen mode

The touch command create a new file.

# Make a new file in current directory.
touch fileName

Enter fullscreen mode Exit fullscreen mode

The rm/rmdir (remove) commands delete file/directory.

# Delete an empty directory.
rmdir myDirectory

# Delete a file.
rm myFile

# Delete a directory recursively.
rm -r myDirectory

Enter fullscreen mode Exit fullscreen mode

Where am I?

The pwd (print working directory) command display your current location.

# Display current location in terminal.
pwd

Enter fullscreen mode Exit fullscreen mode

File management

The cp (copy) command lets you copy and paste your files.

# Create a copy of a file to target location.
cp myFile target/

Enter fullscreen mode Exit fullscreen mode

The mv (move) command moves a file to another location OR rename it if you stay in current directory.

# move file to target location.
mv myFile target/

# rename file.
mv myFile newName
Enter fullscreen mode Exit fullscreen mode

Read/Edit file from the terminal

The head/tail commands print a part of the file.

# Print 10 first lines of a file.
head myFile

# Print 10 last lines of a file.
tail myFile

# Print 30 first lines of a file.
head -n 30 myfile

Enter fullscreen mode Exit fullscreen mode

The cat (concatenate) command can print content of a file, concatenate multiple files together, create and edit files, ...

# Print content of a file in terminal.
cat myFile

# Print content of a file with numbers.
cat myFile -n

# Combine multiple files ogether.
cat fileOne fileTwo > target

Enter fullscreen mode Exit fullscreen mode

Search

The grep (get regular expression) search for a given expression.

# Look for a word in a file.
grep word fileName

# Look for a word in a file without case sensitivity.
grep -i word fileName

Enter fullscreen mode Exit fullscreen mode

Here is my personal cheat sheet for bash commands!

Thank you for reading 😇

Top comments (6)

Collapse
 
jrbrtsn profile image
John Robertson • Edited

Great start Killian! I began using the Bourne shell 30 years ago, then transitioned to Bash about 24 years ago when Linux became widely available. I still learn new things all the time. Shells like Bash provide an easy way to leverage all the other commands installed on your system. Once you learn how to loop, branch, redirect I/O and manage subprocesses, you can put together mashup applications very quickly!

Collapse
 
steminstructor profile image
STEMInstructor

👏👏👏

Collapse
 
killianfrappartdev profile image
Killian Frappart

I can already feel the power of my terminal and I can't wait to dive deeper in 😁

Thank you for sharing your experience!

Collapse
 
bobbyiliev profile image
Bobby Iliev

Great post! Well done!

I recently wrote a Getting started with Bash scripting guide which might be a good next step!

Collapse
 
baukereg profile image
Bauke Regnerus

Thanks for sharing Bobby. I started learning bash just a week ago and I'm writing a script atm to execute git commands on multiple repositories at once. It's so different from any other language I've used before. I already found some new "tricks" in your guide.

Collapse
 
bobbyiliev profile image
Bobby Iliev

That's awesome! Super happy to hear that you've found some useful information in the guide!

Yea, even though there are a lot of more powerful languages out there, you can still do some pretty cool things with Bash!