Linux File Search and Management Commands
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,ffor files,dfor 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:
- Using
xargs:find /opt -mtime +3 | xargs rm -f - 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/zerofor null bytesof: Output file pathbs: Block sizecount: 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)