• Contact
  • About Me
  • Privacy Policy
DefenceDev
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
  • 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
  • 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
19
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)

ADVERTISEMENT
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

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).

ADVERTISEMENT
ADVERTISEMENT

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.

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:

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

Next Post
Linux Networking: ping, Troubleshooting Network Connectivity

Linux Networking: ping, Troubleshooting Network Connectivity

Follow Us

  • Trending
  • Comments
  • Latest
MikroTik: Export Configuration in Text File

MikroTik: Export Configuration in Text File

Fortinet FortiGate: Static Route Configuration via GUI and CLI

Fortinet FortiGate: Static Route Configuration via GUI and CLI

Immich: Installation on Ubuntu 22.04

Immich: Installation on Ubuntu 22.04

Fortinet FortiGate Upgrade Path Tool

Fortinet FortiGate Upgrade Path Tool

NextCloud: Port Forwarding - Essential Ports for Smooth Functionality

NextCloud: Port Forwarding – Essential Ports for Smooth Functionality and Remote Access

Organizing and Managing Photos with Immich: Features I Use

Organizing and Managing Photos with Immich: Features I Use

Install Ubuntu 22.04 on Oracle VM Virtual Box

Install Ubuntu 22.04 on Oracle VM Virtual Box

Linux Directories: cd (Change Directory), pwd (Print Working Directory), mkdir (Make Directory)

Linux Directories: cd (Change Directory), pwd (Print Working Directory), mkdir (Make Directory)

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

Recent News

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

MikroTik: Check Your Wireless Password

MikroTik: Check Your Wireless Password

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

Browse by Category

  • 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

Recent News

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

  • 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
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos

© 2025 defencedev.com - All rights reserved.