Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Linux File Search and Management Commands

Tech May 13 1

File Searching with find

The find command is essential for searching files and directories in Linux systems. It supports searching by name, type, modification time, and file size.

Basic Syntax

find [path] [options]

Searching by Name

  • -name: Match files by name, supports wildcards like *
  • -type: Filter by type, f for files, d for directories

Wildcard Usage

find /opt -name "*a*"
find /opt -name "*.conf"

Searching by Modification Time

Use -mtime to filter files by last modification time:

  • +3: Files modified more than 3 days ago
  • -3: Files modified within the last 3 days

Deleting Files with find

Two common methods:

  1. Using xargs: find /opt -mtime +3 | xargs rm -f
  2. Using -exec: find /opt -mtime -3 -exec rm -f {} \;

Filtering by Size

Use -size to match files by size:

  • +100M: Files larger than 100MB
  • -100M: Files smaller than 100MB
find / -size +100M
find / -size -100G -type f

File Creation and Information with dd and stat

Creatnig Test Files with dd

The dd command is useful for copying files and converting data. It can create test files of specific sizes:

dd if=/dev/zero of=/opt/testfile bs=1M count=1
  • if: Input file, here /dev/zero for null bytes
  • of: Output file path
  • bs: Block size
  • count: Number of blocks

Checking File Metadata with stat

Use stat to view detailed file information, including timestamps:

stat testfile

Directory Tree Visualization with tree

The tree command displays directory contents in a tree structure. Install it via:

yum install -y tree

Example usage:

tree /opt/d0

Secure File Transfer with scp

The scp command securely copies files between hosts. It supports both downloading and uploading operations.

Downloading Files

scp user@remote:/path/to/remote/file /path/to/local/directory

Uploading Files

scp /path/to/local/file user@remote:/path/to/remote/directory

Coping Directories

Use the -r option to recursively copy directories:

scp -r /path/to/local/dir user@remote:/path/to/remote/dir

Scheduling Tasks with crontab

crontab is used to schedule commands or scripts to run at specified times.

Common Options

  • -l: List current cron jobs
  • -e: Edit cron jobs

Example Entry

To execute a script every minute:

* * * * * /path/to/script.sh

Special Characters in crontab

Percent signs (%) in commands must be escaped with a backslash:

* * * * * echo "Time: \%Y\%m\%d" >> /tmp/time.log

Working with Timestamps

The date command displays and sets the system date and time. It supports formatting options:

date "+%F %T"
  • %F: Full date (YYYY-MM-DD)
  • %T: Full time (HH:MM:SS)
Tags: find

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.