• Contact
  • About Me
  • Privacy Policy
  • Disclaimer
DefenceDev
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
    • Bash Scripts
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
No Result
View All Result
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
    • Bash Scripts
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
No Result
View All Result
DefenceDev
No Result
View All Result
ADVERTISEMENT
Home Linux Tutorials Commands

Linux Files Operation: find Temp Files and Execute Commands on Found Files

neo by neo
May 27, 2025
in Commands
0
Linux Files Operation: find Temp Files and Execute Commands on Found Files

Linux Files Operation: find Temp Files and Execute Commands on Found Files

0
SHARES
25
VIEWS
Share on FacebookShare on Twitter
ADVERTISEMENT

Files Operation: find Temp Files and Execute Commands

Linux Files Operation: find Temp Files and Execute Commands on Found Files is crucial for system maintenance, particularly when dealing with temporary files that can quickly accumulate and take up valuable storage space. In this article, we will explore how to use the find command to locate temporary files across the system. Additionally, we will cover how to execute specific commands on the files that the find command identifies. By the end, you’ll be able to streamline file management tasks, automate processes, and ensure your system runs smoothly without unnecessary clutter. Let’s dive into how you can easily locate and act upon temporary files using powerful Linux tools.

find Basic Syntax

find [path] [expression]


[path]: The directory to start the search from.
[expression]: The criteria to match files and directories against.

Find File by Name

Search for files or directories by name:

find /path/to/search -name "filename"

Search for files or directories by name (case-insensitive search)

find /path/to/search -iname "filename"

Find File Type Search

To search for files:

find /path/to/search -type f

To search for directories:

find /path/to/search -type d

Size Search

For example, files larger than 1000 MB:

find /path/to/search -size +1000M

# find /path/to/search -size [+/-]N[cwbkMG]
# M -> Megabytes
# G -> Gigabytes

Modification Time Search

# Example: Files modified in the last 7 days:
find /path/to/search -mtime -7

# find /path/to/search -mtime -N

User and Group Search

To find files with specific permissions: Example: Files with 755 permissions

ADVERTISEMENT
find /path/to/search -perm 755

# find /path/to/search -perm mode

Combining Criteria

You can combine multiple criteria using logical operators such as -and, -or, and ! (not).

Example: Find files named “example.txt” that are larger than 1 MB:

find /path/to/search -name "example.txt" -and -size +1M

Using find and exec Command

On Ubuntu, the find command serves as a powerful tool for searching files and directories based on criteria like name, size, or timestamp. When paired with -exec, it becomes even more versatile, enabling users to perform actions on the found files, such as deleting, moving, or executing commands.

The -exec option allows you to run a command on each file that matches your search criteria.

Example 1: Find temp Files and Delete Them

In this example, we’ll use the find command to search for temporary files, such as those with .tmp extensions, and delete them to free up space. The find command, combined with the -exec option, allows you to automatically remove each matching file. By specifying the correct path and search criteria, you can efficiently clean up temporary files across your system with a single command.

ADVERTISEMENT
find /path/to/search -name "*.tmp" -exec rm {} \
  • {} is a placeholder for the found file.
  • \; signifies the end of the -exec command.

Example 2: Find temp Files and Delete them with xargs

The xargs command is often used with find to handle large numbers of files more efficiently.

Deleting all .tmp files using xargs:

ADVERTISEMENT
find /path/to/search -name "*.tmp" -print0 | xargs -0 rm

–print0 prints the full file name on the standard output, followed by a null character (instead of the newline character).
–0 option in xargs tells it to expect input items to be terminated by a null character.

On this link you can find how to remove files older than x days.

Conclusion

In conclusion, the find command in Linux proves to be an invaluable tool for locating files based on specific criteria. When paired with the -exec option, it further enhances your ability to automate actions like deleting, moving, or executing commands on the found files. By using this powerful combination, you can streamline system maintenance tasks, improve file organization, and save valuable storage space. With the examples provided, you can easily apply these techniques to efficiently manage your Linux file system and ensure your system remains optimized.

defencedev.com

About The Author

neo

See author's posts

ADVERTISEMENT
Previous Post

Linux Networking: Add Multiple Network Gateways (Redundancy) on Ubuntu with netplan

Next Post

Linux Networking: ping, Troubleshooting Network Connectivity

neo

neo

Related Posts

Check Disk Usage on the Linux with du Command
Commands

Check Disk Usage on the Linux with du Command

Linux Process, List all Running Services with ps Command
Commands

Linux Process, List all Running Services with ps Command

Descripton for kill command on Ubuntu
Commands

Linux Process Management, kill Command

Linux Files Operation: chwon (Change Ownership of The Files)
Commands

Linux Files Operation: chown (Change Ownership of the Files)

Linux Files Operation: grep, Efficient Text Search
Commands

Linux Files Operation: grep, Efficient Text Search

Linux Files Operation: tar and zmore Extracting Files
Commands

Linux Files Operation: tar and zmore Extracting Files

Next Post
Linux Networking: ping, Troubleshooting Network Connectivity

Linux Networking: ping, Troubleshooting Network Connectivity

Recommended

Solution: Connect from Ubuntu to the FortiGate Firewall and Execute Command (Bash Script)

Solution: Connect from Ubuntu to the FortiGate Firewall and Execute Command (Bash Script)

MikroTik VLAN Configuration

MikroTik VLAN Configuration

My Way to Organize a Git Repository for Bash, Mikrotik, FortiGate and other Scripts

My Way to Organize a Git Repository for Bash, Mikrotik, FortiGate and other Scripts

My Ultimate Linux/Ubuntu Commands Cheat Sheet (2025)

My Ultimate Linux/Ubuntu Commands Cheat Sheet (2025)

My Ultimate Docker Command Cheat Sheet (2025 Edition)

My Ultimate Docker Commands Cheat Sheet (2025 Edition)

Categories

  • Blog
  • Cloud
    • Private
  • Linux Tutorials
    • Bash Scripting Tutorials
    • Commands
    • Networking
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Solutions
    • Docker
  • Video Tutorials
    • MikroTik Videos
ADVERTISEMENT

DefenceDev Tutorials

defencedev Logo

Whether you’re just starting or looking to expand your skills, I hope you find useful information and engaging discussions here. Let me take you through my journey and the goals behind this space!

Follow Us

Recent News

My Way to Organize a Git Repository for Bash, Mikrotik, FortiGate and other Scripts

My Way to Organize a Git Repository for Bash, Mikrotik, FortiGate and other Scripts

My Ultimate Linux/Ubuntu Commands Cheat Sheet (2025)

My Ultimate Linux/Ubuntu Commands Cheat Sheet (2025)

  • Site Map
  • Privacy Policy
  • Facebook Page
  • Disclaimer
  • Contact
  • About Me

© 2025 defencedev.com - All rights reserved.

No Result
View All Result
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
    • Bash Scripts
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos

© 2025 defencedev.com - All rights reserved.