Image by Author | Midjourney & 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’s commonly used in Unix-based systems like Linux and macOS and provides myriad tools for managing files and directories.
To start using bash, you will need to open the terminal:
- On Linux, look for the terminal application in your application menu.
- On macOS, use the Spotlight search (Cmd + Space) and type “Terminal.”
- On Windows, you can use Git Bash or the 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 start with some basic navigational commands, and then move on to managing directories and files.
Â
pwd
– Print Working Directory
Â
The pwd
command displays the current directory you are in. This is useful to confirm your location in the file system.
Â
ls
– List Directory Contents
Â
The ls
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
– Make Directories
Â
Syntax: mkdir <directory_name>
Example: Create a directory named data
Â
You can create multiple directories at once:
Â
To create nested directories, use the -p
option:
mkdir -p parent/child/grandchild
Â
rmdir
– Remove Directories
Â
Syntax: rmdir <directory_name>
Example: Remove an empty directory named data
:
Â
Note that rmdir
only works for empty directories. To remove non-empty directories, use rm -r
.
Â
cp
– Copy Files and Directories
Â
Syntax: cp <source> <destination>
Example: Copy a file named file.txt
to the backup
directory:
Â
To copy multiple files:
cp file1.txt file2.txt backup/
Â
To copy directories, use the -r
(recursive) option:
Â
mv
– Move/Rename Files and Directories
Â
Syntax: mv <source> <destination>
Example: Move a file named file.txt
to the backup
directory:
Â
Rename file.txt
to file_backup.txt
:
mv file.txt file_backup.txt
Â
The mv
command can move files/directories and rename them.
Â
rm
– Remove Files and Directories
Â
Syntax: rm <file_name>
Example: Remove a file named file.txt
:
Â
To remove directories and their contents, use the -r
(recursive) option:
Â
For forced removal without prompts, add the -f
(force) option:
Â
Practical Examples for Data Scientists
Â
Creating a Project Directory Structure
Example: Create directories for a data science project
mkdir -p project/data,scripts,results
Â
Organizing Data Files
Example: Move all .csv
files to a data
directory
Â
Cleaning Up Unnecessary Files
Example: Remove all .tmp
files
Â
Combining Commands
Â
Using &&
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 remove a file
Â
Tips and Best Practices
Â
Safety with rm
Always double-check paths before using rm
to avoid accidental deletion.
Â
Using Wildcards
Wildcards like *
can match multiple files, making 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
Â
Here is a quick reference summary table, summarizing the syntax and use of cp
, mv
, rm
, and mkdir
.
Â
Command | Syntax | Description |
---|---|---|
pwd | pwd | Print working directory |
ls | ls | List directory contents |
mkdir | mkdir <directory_name> | Create new directory |
rmdir | rmdir <directory_name> | Remove empty directory |
cp | cp <source> <destination> | Copy files or directories |
mv | mv <source> <destination> | Move or rename files or directories |
rm | rm <file_name> | Remove files or directories |
Â
Â
Matthew Mayo (@mattmayo13) holds a master’s degree in computer science and a graduate diploma in data mining. As managing editor of KDnuggets & Statology, and contributing editor at Machine Learning Mastery, Matthew aims to make complex data science concepts accessible. His professional interests include natural language processing, language models, machine learning algorithms, and exploring emerging AI. He is driven by a mission to democratize knowledge in the data science community. Matthew has been coding since he was 6 years old.