Linux Bash Basics

a laptop computer sitting on top of a table in the dark with green lights on it

Note: this page has been created with the use of AI. Please take caution, and note that the content of this page does not necessarily reflect the opinion of Cratecode.

Navigating the world of Linux can be a little intimidating for newcomers. Fear not, young padawan! We will guide you through the basics of Linux command line so you can feel more confident while using it.

What is Bash?

Bash stands for "Bourne Again SHell" and is the default shell for most Linux distributions. It's not just a place to run commands, but also a scripting language that can automate tasks, manipulate files, and do much more.

To begin your journey, you need to know how to move around within the file system. Here are some essential commands:

  • pwd: Print the path of the current working directory.
  • ls: List the contents of the current directory.
  • cd directory: Change the current directory to directory.

Here's an example:

$ pwd /home/username $ ls Documents Downloads Music Pictures Videos $ cd Documents $ pwd /home/username/Documents

Manipulating Files and Directories

Now that you know how to move around, let's learn to manipulate files and directories:

  • mkdir directory: Create a new directory.
  • touch file: Create a new, empty file.
  • cp source destination: Copy a file from source to destination.
  • mv source destination: Move a file from source to destination.
  • rm file: Remove a file.

Here's an example:

$ mkdir my_folder $ touch my_file.txt $ cp my_file.txt my_folder/ $ cd my_folder $ ls my_file.txt $ rm my_file.txt $ ls

Basic File Operations

Viewing, searching, and editing files are essential operations. Here are some commands to help you with that:

  • cat file: Display the contents of file.
  • grep pattern file: Search for pattern in file.
  • nano file: Edit file using the nano text editor.

Here's an example:

$ cat my_file.txt Hello, world! $ grep "world" my_file.txt Hello, world! $ nano my_file.txt

Command Chaining and Redirection

Bash allows you to chain commands together and redirect their output. This can be incredibly useful for complex operations:

  • command1 ; command2: Run command1, then run command2.
  • command1 && command2: Run command1, and if it succeeds, run command2.
  • command1 || command2: Run command1, and if it fails, run command2.
  • command > file: Redirect the output of command to file.
  • command >> file: Append the output of command to file.
  • command1 | command2: Pipe the output of command1 to command2.

Here's an example:

$ mkdir new_folder && touch new_folder/new_file.txt $ echo "Hello, world!" > new_file.txt $ cat new_file.txt | grep "world" Hello, world!

That's it! You now have a solid foundation in Linux Bash basics. With practice and exploration, you'll soon be a Linux command line master. Remember, the Force (and the man pages) are with you.


What are some basic Linux commands every beginner should know?

Here are a few essential Linux commands that every beginner should be familiar with:

  1. pwd - Print the current working directory.
  2. ls - List files and directories in the current directory.
  3. cd - Change the current working directory.
  4. mkdir - Create a new directory.
  5. touch - Create a new, empty file.

How do I navigate between directories in Linux using the command line?

You can navigate between directories using the cd command followed by the directory name or path. For example:

  • To navigate to a directory named "example": cd example
  • To move up one level to the parent directory: cd ..
  • To navigate to the home directory: cd ~

How can I combine multiple commands in a single line?

You can combine multiple commands in a single line by using a semicolon (;) between commands. For example:

mkdir new_directory; cd new_directory; touch new_file.txt

This line creates a new directory, navigates to it, and creates a new file within it.

How do I view the contents of a text file in the command line?

You can view the contents of a text file using the cat command followed by the file name. For example:

cat example.txt

This command displays the contents of the file "example.txt" in the terminal.

How can I search for a specific keyword within a file using the command line?

You can search for a specific keyword or pattern within a file using the grep command, followed by the keyword and file name. For example:

grep "search_keyword" example.txt

This command searches for the keyword "search_keyword" within the file "example.txt" and displays the lines containing the keyword.

Similar Articles