Author's image | Midjourney and Canva
Understanding the Bash Shell
Bash, the Bourne-Again Shell, is a command-line interpreter that allows users to interact with an operating system by typing commands. It is commonly used on Unix-based systems such as Linux and macOS and offers a variety of tools for managing files and directories.
To start using bash, you will need to open the terminal:
- In Linuxlook for the terminal application in the applications menu.
- In Mac OSuse Spotlight search (Cmd + Space) and type “Terminal”.
- In WindowsYou can use Git Bash or Windows Subsystem for Linux (WSL).
Once you have the terminal open and at your disposal, we are ready to learn how to manage files and directories with bash. We will start with some basic navigation commands, and then move on to managing directories and files.
pwd
– Print working directory
He pwd
The command displays the current directory you are in. This is useful for confirming your location in the file system.
ls
– Directory contents list
He ls
The command lists the files and directories in the current directory. You can add options like -l
for detailed information or -a
to include hidden files.
mkdir
– Create directories
Syntax: mkdir
Example: Create a directory called data
You can create multiple directories at once:
To create nested directories, use the -p
option:
mkdir -p parent/child/grandchild
rmdir
– Delete directories
Syntax: rmdir
Example: Delete an empty directory named data
:
Note that rmdir
Only works with empty directories. To remove non-empty directories, use rm -r
.
cp
– Copy files and directories
Syntax: cp
Example: Copy a file called file.txt
toward backup
directory:
To copy multiple files:
cp file1.txt file2.txt backup/
To copy directories, use the -r
Option (recursive):
mv
– Move/rename files and directories
Syntax: mv
Example: Moving a file named file.txt
toward backup
directory:
Rename file.txt
to file_backup.txt
:
mv file.txt file_backup.txt
He mv
The command can move and rename files/directories.
rm
– Delete files and directories
Syntax: rm
Example: Delete a file named file.txt
:
To remove directories and their contents, use the -r
Option (recursive):
For forced removal without warnings, add the -f
(force) option:
Practical examples for data scientists
Creating a project directory structure
Example: Creating directories for a data science project
mkdir -p project/{data,scripts,results}
Organizing data files
Example: Move all .csv
files to a data
directory
Clean up unnecessary files
Example: Delete all .tmp
files
Combining commands
Wearing &&
To chain commands
Example: Create a directory and move files in one command
mkdir backup && mv *.csv backup/
Using semicolons to execute sequentially
Example: List contents and then delete a file
Tips and best practices
Security with rm
Always double check routes before using them rm
to prevent accidental deletion.
Using wildcards
Wildcards like *
You can match multiple files, which makes commands more efficient. For example, *.csv
matches all CSV files.
Backup important files
Before performing bulk operations, create backups to prevent data loss.
Quick reference
Below is a quick reference summary table that summarizes the syntax and usage of cp
, mv
, rm
and mkdir
.
Domain | Syntax | Description |
---|---|---|
password | password | Print working directory |
is | is | List the contents of the directory |
mkdir | mkdir | Create new directory |
rm directory | rm directory | Delete empty directory |
cp | cp | Copy files or directories |
motor vehicle | motor vehicle | Move or rename files or directories |
Scorpion | Scorpion | Delete files or directories |
Matthew May (twitter.com/mattmayo13″ rel=”noopener”>@mattmayo13) holds a master's degree in computer science and a postgraduate diploma in data mining. As editor-in-chief of KDnuggets & Statologyand contributing editor at Mastering Machine LearningMatthew aims to make complex data science concepts accessible. His professional interests include natural language processing, language models, machine learning algorithms, and exploring emerging ai. His mission is to democratize knowledge in the data science community. Matthew has been coding since he was 6 years old.
<script async src="//platform.twitter.com/widgets.js” charset=”utf-8″>