A Basic Guide to Bash Commands and Concatenation

March 27, 2023

Bash (Bourne-Again SHell) is a popular command-line interface for interacting with Unix-like operating systems, such as Linux and macOS. It allows users to execute various commands to manage files, directories, processes, and more. This guide provides an overview of basic Bash commands and illustrates how to concatenate commands using the |, &, <, and > operators.

Basic Bash Commands

Navigating Directories:

  • pwd: Print the current working directory
  • cd: Change the current working directory (e.g., cd /path/to/directory)
  • ls: List the contents of a directory (e.g., ls /path/to/directory)

File and Directory Management:

  • mkdir: Create a new directory (e.g., mkdir new_directory)
  • rmdir: Remove an empty directory (e.g., rmdir directory_name)
  • rm: Remove files or directories (e.g., rm file_name, rm -r directory_name)
  • cp: Copy files or directories (e.g., cp source_file destination)
  • mv: Move or rename files or directories (e.g., mv source destination)

File Viewing and Manipulation:

  • cat: Display the contents of a file (e.g., cat file_name)
  • less: Display the contents of a file with scrolling and searching (e.g., less file_name)
  • head: Display the first lines of a file (e.g., head -n 10 file_name)
  • tail: Display the last lines of a file (e.g., tail -n 10 file_name)
  • grep: Search for text patterns in files (e.g., grep "pattern" file_name)
  • sed: Stream editor for text manipulation (e.g., sed 's/original/new/g' input_file > output_file)
  • awk: Text processing tool for column-based data (e.g., awk '{print $1}' file_name)

Process and System Management:

  • ps: Display information about running processes
  • top: Display a real-time overview of system processes
  • kill: Terminate a process (e.g., kill process_id)
  • man: Display the manual page for a command (e.g., man command_name)
  • uname: Display system information (e.g., uname -a)

Concatenation of Commands

Pipe (|):

The pipe operator (|) is used to send the output of one command as input to another command. This is known as "piping" and allows you to chain commands together. Example:

cat file.txt | grep "pattern": This command will display the lines containing the "pattern" in the file.txt.

Background Execution (&):

The ampersand (&) is used to run a command in the background, allowing you to continue using the terminal for other tasks. Example:

sleep 10 & : This command will run the 'sleep' command in the background for 10 seconds.

Input Redirection (<):

The less-than operator (<) is used to redirect the contents of a file as input to a command. Example:

sort < file.txt: This command will sort the lines in file.txt and display the sorted output.

Output Redirection (>, >>):

The greater-than operator (>) is used to redirect the output of a command to a file. If the file already exists, its contents will be overwritten. If the file does not exist, it will be created. Example:

ls -l > output.txt: This command will save the long listing of the current directory to output.txt, overwriting the file if it already exists.

The double greater-than operator (>>) is used to redirect the output of a command to a file, appending the output to the existing contents of the file. If the file does not exist, it will be created. Example:

echo "New line" >> file.txt: This command will append "New line" to the end of file.txt.

Combining Redirection and Pipes:

You can also combine redirection and pipes to create more complex command sequences. Example:

cat file1.txt file2.txt | grep "pattern" > output.txt: This command concatenates file1.txt and file2.txt, searches for lines containing "pattern," and writes the matching lines to output.txt.

Additional Tips:

Command Substitution:

Command substitution allows you to use the output of one command as an argument for another command. Command substitution can be done using either $(command) or command. Example:

cp $(ls -1t | head -1) /path/to/destination/: This command copies the most recently modified file in the current directory to the specified destination.

Using 'tee' for Simultaneous Output:

The 'tee' command can be used to display the output of a command on the terminal and write it to a file simultaneously. Example:

ls -l | tee output.txt: This command will display the long listing of the current directory on the terminal and also write the output to output.txt.

Running Multiple Commands Sequentially:

You can run multiple commands sequentially using the semicolon (;). The next command will only be executed after the current command has completed. Example:

mkdir new_directory; cd new_directory: This command will create a new directory called "new_directory" and then change the current working directory to "new_directory".


Mastering basic Bash commands and understanding how to concatenate them using |, &, <, and > operators can significantly improve your efficiency when working with Unix-like operating systems. Practicing these commands and techniques will help you navigate, manage files, and perform various tasks in the command-line environment with ease.