Working with files and directories is one of the most important skills in Bash scripting. Many Linux automation tasks involve creating folders, copying files, moving files, deleting files, checking paths, and organizing data. If you are learning Bash scripting, file and directory handling is a must-know topic.
Bash scripts are commonly used to manage backups, logs, reports, configuration files, scan results, and temporary files. For cybersecurity learners, Bash can help organize security reports, save scan outputs, check log files, and automate file-based tasks.
You can create an empty file using the touch command.
touch report.txt
Inside a Bash script:
#!/bin/bashtouch security-report.txtecho "File created successfully"
You can also write content into a file using echo:
echo "This is a Bash file example" > report.txt
The > operator creates or overwrites a file.
To create a directory, use the mkdir command.
mkdir backups
To create parent directories if they do not exist, use:
mkdir -p backups/logs
Example script:
#!/bin/bashbackup_dir="$HOME/backups"mkdir -p "$backup_dir"echo "Backup directory created at: $backup_dir"
To copy a file, use the cp command.
cp file.txt backup-file.txt
Example script:
#!/bin/bashcp report.txt report-backup.txtecho "File copied successfully"
To copy a directory, use:
cp -r old-folder new-folder
The -r option copies directories recursively.
The mv command is used to move or rename files.
Rename a file:
mv oldname.txt newname.txt
Move a file to another directory:
mv report.txt backups/
Example script:
#!/bin/bashmkdir -p reportsmv security-report.txt reports/echo "Report moved to reports directory"
To delete a file, use:
rm file.txt
To delete an empty directory:
rmdir foldername
To delete a directory with files inside:
rm -r foldername
Be careful with rm -r because deleted files may not be easy to recover.
#!/bin/bashtarget="192.168.1.1"scan_dir="$HOME/security-scans"mkdir -p "$scan_dir"echo "Running scan on $target..."nmap "$target" > "$scan_dir/nmap-result.txt"echo "Scan result saved in: $scan_dir"
This script creates a directory and saves an Nmap scan result inside it.
Working with files and directories using Bash scripts is a basic but powerful skill. You can create, copy, move, rename, and delete files automatically. You can also create directories for backups, logs, reports, and scan results.
For beginners, learning file and directory handling in Bash is essential for Linux automation, system administration, backup management, and cybersecurity scripting.
Journalists use OSINT to verify public information before publishing. In 2026, misinformation, AI-generated images, fake…
Docker is an open-source platform that lets you package and run applications inside containers. Each container…
PostgreSQL (often called Postgres) is an open-source relational database system. It supports advanced features like JSON…
Xrdp is an open-source server that lets you connect to your Ubuntu machine from another computer…
Apache Tomcat is an open-source web server and Java servlet container. It is one of the…
Keeping your Ubuntu system updated is one of the best ways to protect it. Security…