If you’re new to Linux, understanding Bash (the Bourne Again SHell) is one of the most important skills you can learn. Bash lets you communicate with your system through text-based commands, helping you manage files, directories, and processes efficiently.

In this guide, we’ll walk through the most useful Bash commands — ls, cat, rmdir, rm, mkdir, cp, w, nano, and ps — along with Input/Output (I/O) operators that let you control how data flows in and out of commands.

1. ls — List Directory Contents

The ls command lists files and directories in your current working location.

Syntax:

ls [options] [directory]

Examples:

  • ls → Lists files and folders in the current directory.
  • ls -l → Displays a detailed list with file permissions, owner, and size.
  • ls -a → Shows all files, including hidden ones (those starting with .).
  • ls /home → Lists contents of the /home directory.

Tip: Combine options, e.g., ls -la for a detailed list including hidden files.

2. cat — View or Combine File Contents

The cat command (short for concatenate) lets you read, create, or merge files.

Syntax:

cat [options] [file]

Examples:

  • cat file.txt → Displays the content of a file.
  • cat file1.txt file2.txt > combined.txt → Merges two files into one.
  • cat -n file.txt → Displays file content with line numbers.

Tip: Use less file.txt for easier scrolling in long files.

3. mkdir — Create a Directory

Use mkdir to create new directories.

Syntax:

mkdir [options] [directory_name]

Examples:

  • mkdir projects → Creates a folder named projects.
  • mkdir -p /home/user/docs/new → Creates nested directories if they don’t exist.

Tip: The -p option prevents errors when creating multiple levels at once.

4. rmdir — Remove Empty Directories

This command removes empty directories.

Syntax:

rmdir [directory_name]

Examples:

  • rmdir test → Deletes the empty directory test.
  • rmdir -p /home/user/docs/emptydir → Removes nested empty directories.

Tip: For non-empty directories, use rm -r instead.

5. rm — Remove Files or Directories

Use rm to delete files or directories. Be careful — this command permanently removes data.

Syntax:

rm [options] [file/directory]

Examples:

  • rm file.txt → Deletes a file.
  • rm -i file.txt → Asks for confirmation before deleting.
  • rm -r folder → Deletes a folder and its contents.
  • rm -rf folder → Forcefully removes a directory without confirmation.

Warning: Avoid rm -rf / — it can delete your entire system.

6. cp — Copy Files or Directories

The cp command copies files and directories to a new location.

Syntax:

cp [options] source destination

Examples:

  • cp file.txt backup.txt → Copies a file.
  • cp file.txt /home/user/ → Copies a file to another directory.
  • cp -r folder1/ folder2/ → Copies folders and subfolders recursively.

Tip: Use -i to prompt before overwriting existing files.

7. w — Display Logged-In Users

The w command shows which users are logged in and what they’re doing.

Syntax:

w [options] [user]

Examples:

  • w → Lists all active users and their activities.
  • w username → Displays details for a specific user.

Output includes:

  • Username
  • Terminal
  • Login time
  • Idle time
  • Current process

Tip: Useful for monitoring shared servers or multi-user systems.

8. nano — Edit Text Files in Terminal

nano is a simple text editor that runs directly inside the terminal.

Syntax:

nano [file_name]

Examples:

  • nano notes.txt → Opens or creates a text file.
  • Use Ctrl + O to save, Ctrl + X to exit.

Tip: Edit system files with admin rights using sudo nano /etc/hosts.

9. ps — View Running Processes

The ps command shows active processes and their details.

Syntax:

ps [options]

Examples:

  • ps → Lists processes for the current terminal session.
  • ps -e → Displays all processes on the system.
  • ps -ef → Shows full details including user, PID, and CPU usage.
  • ps aux | grep nginx → Filters processes related to “nginx”.

Tip: Combine ps with grep to quickly find specific tasks.

10. Bash I/O Operators — Redirecting Input and Output

In Bash, I/O operators control where command input comes from and where output goes. They’re powerful tools for automating tasks and managing data flow.

Standard Streams

  • stdin (0): Standard input (keyboard)
  • stdout (1): Standard output (screen)
  • stderr (2): Standard error output (error messages)

Common I/O Operators

1. Output Redirection (> and >>)

  • > → Redirects output to a file (overwrites it).
  • >> → Appends output to a file.
    Examples:
ls > files.txt
ls >> files.txt
echo "Hello Linux" > message.txt

2. Input Redirection (<)


Redirects input from a file.

cat < file.txt

3. Error Redirection (2> and 2>>)


Redirects error messages to a file.

ls nofile 2> errors.txt
ls nofile 2>> errors.txt

4. Combine Output and Errors (&>)


Sends both standard output and error messages to the same file.

ls /root /home &> output.txt

5. Piping (|)


Sends the output of one command as input to another.

ls | grep txt
ps aux | grep apache

6. Here Document (<<)


Feeds multiple lines of input into a command.

cat << EOF
Line 1
Line 2
EOF

Conclusion

Mastering these basic Bash commands and I/O operators is the foundation of working efficiently in Linux. With these tools, you can manage files, monitor processes, and redirect data like a professional system administrator.

Pro Tip: Use man command (e.g., man ls) or command --help (e.g., cp --help) to explore more advanced features and parameters.

9 basic Linux Bash Commands you should know

Post navigation