How to Manage Files and Directories in Bash


How to Manage Files and Directories in Bash
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.



Recent Articles

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here