Home Wiki Programming & Logic File and Directory Management: Copy, Move, Delete, and Search
Programming & Logic

File and Directory Management: Copy, Move, Delete, and Search

Creating Files and Directories: touch and mkdir

Managing files and directories is one of the most frequent tasks on industrial Linux servers. Whether organizing sensor logs or setting up applications, you need to create structures quickly.

touch sensor_config.yaml           # Create an empty file
touch reading1.csv reading2.csv    # Create multiple files

If the file exists, touch updates its timestamp without changing contents -- useful for triggering monitoring scripts.

mkdir logs                         # Create a directory
mkdir -p /opt/scada/data/2026/01   # Create full path including parents
mkdir -p backups/{daily,weekly,monthly}  # Multiple subdirectories

The -p flag is essential. Without it, mkdir fails if parent directories do not exist.

Copying and Moving: cp and mv

cp config.yaml config.yaml.bak                # Backup copy
cp -r /opt/scada/config/ /backup/scada/        # Copy directory recursively
cp -p important.log /backup/                   # Preserve permissions and timestamps
cp -u *.csv /archive/                          # Copy only newer files

The -p flag preserves ownership and timestamps, important for audit trails on industrial configs.

mv old_name.log new_name.log                   # Rename a file
mv sensor_data.csv /archive/2026/              # Move to another directory
mv /tmp/upload/*.csv /opt/scada/incoming/      # Move multiple files

Moving within the same filesystem is instant -- only the directory entry changes, not the data.

Deleting: rm and rmdir -- With Caution!

Deletion on Linux is permanent. There is no recycle bin on the command line.

rm old_report.csv                  # Delete a file
rm -i *.tmp                        # Ask confirmation before each deletion
rm -r /tmp/old_sensor_data/        # Delete directory and contents
rm -rf /tmp/cache/                 # Force delete without prompting

Always preview before deleting: use ls with the same pattern first to see what would be affected.

rmdir empty_folder                 # Remove only empty directories (safe)

Searching for Files: find and locate

find /var/log -name "*.log"                    # Find all .log files
find /opt/scada -name "*.yaml" -type f         # Files only (not directories)
find /data -mtime -7                           # Modified in last 7 days
find /tmp -size +100M                          # Files larger than 100 MB
find /data -name "*.csv" -mtime +30 -delete    # Delete CSVs older than 30 days
sudo updatedb                      # Update the file database
locate modbus.conf                 # Instant search by filename
locate -i SCADA                    # Case-insensitive

locate is faster than find because it uses a pre-built database, but newly created files will not appear until updatedb runs.

Wildcards: *, ?, and []

Wildcards match multiple files with a single pattern:

ls *.csv                           # All .csv files
ls sensor_?.log                    # sensor_1.log, sensor_2.log, etc.
ls data_[0-9].csv                  # data_0.csv through data_9.csv
ls {config,setup}*.yaml            # config*.yaml and setup*.yaml

Wildcards are expanded by the shell, so every command that accepts filenames accepts wildcards:

cp /data/2026-04-*.csv /backup/    # Copy all April 2026 CSV files
wc -l sensor_[1-5].log             # Count lines in sensors 1-5

Practical Example: Organizing Sensor Logs Into Date-Based Folders

Your factory server has months of sensor data in a flat directory:

ls /opt/scada/data/
# sensor_20260101.csv ... sensor_20260415.csv

# Create date-based structure
mkdir -p /opt/scada/data/{2026-01,2026-02,2026-03,2026-04}

# Move files by month
mv /opt/scada/data/sensor_202601*.csv /opt/scada/data/2026-01/
mv /opt/scada/data/sensor_202602*.csv /opt/scada/data/2026-02/
mv /opt/scada/data/sensor_202603*.csv /opt/scada/data/2026-03/
mv /opt/scada/data/sensor_202604*.csv /opt/scada/data/2026-04/

# Verify and check for old files to archive
ls -R /opt/scada/data/
find /opt/scada/data/ -name "*.csv" -mtime +90

This pattern of date-based organization is a daily task on industrial servers. In a later lesson, you will automate this with a Bash script.

Summary

In this lesson you learned essential file management commands:

  • touch and mkdir -p create files and directory trees.
  • cp -r copies directories; mv moves or renames.
  • rm is permanent -- always preview with ls before deleting.
  • find searches by name, date, and size; locate is faster for name lookups.
  • Wildcards (*, ?, []) work with any command that accepts filenames.
  • Date-based folder organization is a common pattern for managing sensor data.

In the next lesson, you will learn text processing tools -- grep, sed, and awk -- to search, filter, and transform file contents.

files directories cp mv rm find الملفات المجلدات النسخ النقل الحذف البحث