loading...
Cover image for Bash commands introduction  ๐Ÿง‘๐Ÿฟโ€๐Ÿ’ป

Bash commands introduction ๐Ÿง‘๐Ÿฟโ€๐Ÿ’ป

killianfrappartdev profile image Killian Frappart Updated on ใƒป2 min read

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 ~

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

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

Make/remove file or directory

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

# Make a new directory in current directory.
mkdir directoryName

The touch command create a new file.

# Make a new file in current directory.
touch fileName

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

Where am I?

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

# Display current location in terminal.
pwd

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/

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

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

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

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

Here is my personal cheat sheet for bash commands!

Thank you for reading ๐Ÿ˜‡

Posted on by:

killianfrappartdev profile

Killian Frappart

@killianfrappartdev

Jumped into web development 1st of January 2020 and I'm completely in love. I have no experience as a blogger but practice makes perfect! ๐Ÿค–๐Ÿฆพ

Discussion

pic
Editor guide
 

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!

 

๐Ÿ‘๐Ÿ‘๐Ÿ‘

 

I can already feel the power of my terminal and I can't wait to dive deeper in ๐Ÿ˜

Thank you for sharing your experience!

 

Great post! Well done!

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

 

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.

 

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!