Introduction to the Command Line
Welcome to the world of the Linux command line, where you can communicate with your computer using powerful text-based commands. While it might seem a bit intimidating at first, mastering the basics of the Bash shell can open up a realm of efficiency and control that graphical interfaces can’t match.
In this blog, we’ll take you through some fundamental Bash commands to get you started on your command-line journey.
Getting Started with Bash
The shell is your interface to the operating system, allowing you to interact with it through commands. There are two types of shells: login shells and non-login shells. Most of the time we will work with non-login shells. Understanding the distinction between login and non-login shells is essential, especially when configuring your shell environment. We will cover this in detail in the next blog.
Now, let’s explore how to launch and close shells:
Launching a Shell
GUI Method: Use Search and Type Terminal to open a new terminal.
Shortcut: You can often launch a new shell with a keyboard shortcut. For instance, in many Linux environments, pressing Ctrl + Alt + T
opens a new terminal.
Closing a Shell
- GUI Method: Simply typing
exit
and pressing Enter will close the shell. Alternatively, you can use the window or tab close button. - Shortcut: Some environments offer shortcuts to close a terminal window. For example,
Ctrl + D
is a common shortcut to exit a shell.
Now that we’ve got our shells up and running, let’s embark on our journey into the world of Bash commands.
Navigating the File System
Now that we’ve got our Bash shell up and running, let’s explore the basic commands to move around the file system. This is fundamental for navigating directories and understanding where you are in the system.
pwd (Present Working Directory)
Use the pwd command to find out the current directory you’re in. This is handy when you want to know your location within the file system. For example, in the below snip, you can see I am currently in the home/user_name directory.
ls (List)
The ls command in the Linux Bash shell stands for “list.” It is used to display a list of files and directories in the current working directory. The ls command provides information such as file names, sizes, and permissions, allowing users to view the contents of a directory. For example, the below snip ls command lists out the content of the home/dummy/ directory. There are 2 text files and 1 temp directory.
The ~ sign indicates the home directory. We will explore more about different directories and files in the upcoming blog.
cd (Change Directory)
The cd command is your ticket to move around. If you want to go to a specific directory, just type cd followed by the path. For example, to move into the temp directory we can use the cd command.
Move up one level in the directory hierarchy with “cd ..“
To go back to the home directory, use cd without any arguments.
Now you can confidently check where you are with pwd, navigate through directories using cd, and list the content of directories using ls command. These are your basic tools for exploring the file system in the Linux Bash shell.
Working with Files and Directories
Now that you’re familiar with navigating the file system, let’s see the commands for creating, copying, moving, and deleting files and directories.
mkdir
Create a new directory with mkdir command.
touch
Create a new file with touch command.
cp
Copy files or directories with cp command.
Step to copy a file from one dir to another dir
- cd into the dir from where you want to copy the file
- run the command cp filename /path-to-dir/
- In the below snip, I am copying “myfile.txt” to “mydir” directory
- for understanding, I first checked “mydir” and list content, and since the dir is empty no content is listed by the ls command. All the command used here is covered above, analyze them properly and see if you can understand the flow.
mv
Move or rename files with mv command.
Step to rename file or dir
- Type command mv oldfile newfile_name
- This will rename the file. As in the snip below, I renamed file.txt present in the dummy dir to newfile.txt
Step to move files
- Use syntax mv filename path-to-dir/
- This will move a file to the specified directory. See the below snip for more clarity.
- I moved the newfile.txt from the dummy directory to myDir.
rm
Remove files with rm command. Don’t use this command in the root directly with superuser privileges. Take extra precautions as the changes made will not be reverted.
Syntax: rm filename
Tip: Create one dummy directory with some files to practice this command.
rmdir
Remove empty directories with rmdir command. Don’t use this command in the root directly with superuser privileges. Take extra precautions as the changes made will not be reverted. This command does not work for non-empty directory.
Syntax: rm dirname
Tip: Create one dummy directory with some files to practice this command.
Conclusion
You’ve now dipped your toes into the vast ocean of Bash commands. Remember, practice is key. Experiment with these commands, create files, move them around, and get comfortable with the command line. As you become more familiar, you’ll realize the efficiency and flexibility that Bash provides.