Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Mastering File Search and Task Scheduling in Linux: Using Find and Crontab Commands

Tech Jun 17 2

The find command is a powerful utility for searching files in Linux based on various criteria. The basic syntax is:

find [path] [options]

Common Find Options

Use -name to search by filename with wildcard support. The asterisk (*) wildcard matches zero or more characters:

  find /data/ -name "*report*"      # Find all files containing "report"
  find /data/ -name "*.log" -type f  # Find all regular files with .log extension
  find /data/ -name "temp*" -type d  # Find all directories starting with "temp"

The -type option filters by file type: f for regular files and d for directories.

Example: Locating Configuration Files

# Search for nginx.conf file
find / -name "nginx.conf" -type f
# If not found, install nginx
yum install -y nginx
# Search again
find / -name "nginx.conf" -type f
# Results:
/etc/nginx/nginx.conf
/usr/lib/tmpfiles.d/nginx.conf
# Narrow the search to /etc/ for faster results
find /etc/ -name "nginx.conf" -type f

Example: Finding Files by Extension

# Get all .conf files in /etc/
find /etc/ -name "*.conf" -type f
# Sample results:
/etc/resolv.conf
/etc/sysctl.conf
/etc/nginx/conf.d/nginx.conf
...

Example: Searching by Filename Pattern

# Find all files starting with "nginx"
find /etc/ -name "nginx*" -type f
# Results:
/etc/sysconfig/nginx
/etc/logrotate.d/nginx
/etc/nginx/nginx.conf

File Timestamp Management

Viewing File Timestamps with stat

The stat command displays detailed file information including timestamps:

stat /var/log/system.log
  File: "/var/log/system.log"
  Size: 10240       Blocks: 24         IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 13432845    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: system_u:object_r:var_log_t:s0
Access: 2024-07-15 09:10:43.906250733 +0800
Modify: 2024-07-15 09:10:43.906250733 +0800
Change: 2024-07-15 09:10:43.906250733 +0800
 Birth: -

Setting Custom Timestamps with touch

The touch command can create new files or update timestamps of existing files:

touch /backup/archive.txt -m -d "2024-7-13 00:00"
touch /backup/old.txt -m -d "2024-7-12 00:00"
touch /backup/older.txt -m -d "2024-7-11 00:00"

Creating Files with Specific Sizes

Using dd to Create Files

The dd command is useful for creating files with specific sizes:

# Clear the test directory
rm -rf /test/files/*
# Create files of different sizes
dd if=/dev/zero of=/test/files/small.dat bs=1M count=1
dd if=/dev/zero of=/test/files/medium.dat bs=5M count=1
dd if=/dev/zero of=/test/files/large.dat bs=10M count=1
# Verify file sizes
ls -lh /test/files/

In these commands:

  • if=/dev/zero specifies the input file (provides null bytes)
  • of=/path/to/file specifies the output file
  • bs=SIZE sets the block size
  • count=N specifies the number of blocks

Finding and Managing Files by Time

Using Find with Time Criteria

Use find with -mtime to locate files based on modification time:

# Find files modified more than 3 days ago
find /backup/ -mtime +3
# Find files modified within the last 3 days
find /backup/ -mtime -3
# Find files modified exactly 3 days ago
find /backup/ -mtime 3

Deleting Old Files

There are two common methods to delete files found with find:

Method 1: Using -exec

find /backup/ -mtime +30 -exec rm -rf {} \;

Method 2: Using xargs

find /backup/ -mtime +30 | xargs rm -rf

The xargs command converts input from standard input into command arguments. It's especially useful for handling large numbers of files.

Finding Files by Size

Use find with -size to locate files based on their size:

# Find files larger than 100MB
find / -size +100M
# Find files larger than 1GB
find / -size +1G
# Find files smaller than 100KB
find / -size -100k
# Find files exactly 5MB in size
find /data/ -size 5M

Directory Structure Visualization

Using tree Command

The tree command displays directory structures in a tree format:

# Basic usage
tree /home/user/project
# Limit display depth
tree -L 2
# Show only directories
tree -d
# Show file permissions
tree -p
# Save output to file
tree > directory_structure.txt

Example output:

/home/user/project
├── file1.txt
├── dir1
│   ├── file2.txt
│   └── subdir1
│       └── file3.txt
└── dir2
    └── file4.txt

Secure File Transfer with SCP

SCP (Secure Copy) allows secure file transfer between Linux systems over SSH:

# Upload a file to remote server
scp /local/file.txt user@remote:/remote/path/
# Upload a directory recursively
scp -r /local/directory/ user@remote:/remote/path/
# Download a file from remote server
scp user@remote:/remote/file.txt /local/path/
# Download a directory recursively
scp -r user@remote:/remote/directory/ /local/path/

Additional SCP options:

  • -p: Preserve timestamps and permissions
  • -P: Specify custom SSH port
  • -i: Use specific identity file
  • -q: Quiet mode (no progress meter)

Scheduling Tasks with Crontab

Crontab Basics

Crontab allows you to schedule commands to run at specific times. The time fields are:

minute hour day month weekday command

Examples:

# Run every minute
* * * * * /path/to/command
# Run at 5 AM every day
0 5 * * * /path/to/command
# Run at 6:30 PM on weekdays
30 18 * * 1-5 /path/to/command
# Run at midnight on January 1st
0 0 1 1 * /path/to/command

Managing Crontab

# View existing crontab entries
crontab -l
# Edit crontab
crontab -e
# Example: List directory contents every minute
*/1 * * * * /bin/ls /var/log/ >> /root/directory_list.txt
# Example: Create timestamped backup every hour
0 * * * * /bin/tar -czvf /backup/archive_$(date +\%Y\%m\%d\%H\%M\%S).tar.gz /data/

When using date formatting in crontab, remember to escape the percent signs with a backslash.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.