Linux command to find directories of n days old - bash

I am using the command "find /path/* -type d -ctime +5" to find directories which are 5 days old. This command lists the directory and all its sub-directories. But I want to stop at the first matched directory.
For the following directory structure:
/temp/a/b/c/file.txt
Let's say directories 'b' and 'c' were created 5 days ago.
The above command lists the following as the output:
/temp/a/b and /temp/a/b/c.
Instead of the above output, I want only "/temp/a/b" as the output.
Is there any way to do that?

You can stop searching a branch with -prune:
find /path -type d -ctime +4 -prune
This will print all the directories whose ctime is older than 5 days, but skip all their subdirectories.

Related

Copy the file created newly in UNIX

I have a directory where so many files created daily and need to copy the new files which were generated. And all files will be created with starting name abc_
Ex:I have a file abc_0520123.pdf on the next day two files were created abc_0521234.pdf and abc_0521254.pdf now I want to copy only these two files created newly.
Please help me how can I compare old files with new one and to copy them.
You can use find.
find /my_directory -mtime -1d # Finds everything modified less than one day ago.
find /my_directory -ctime -1d # Finds everything created less than one day ago.
find /my_directory -ctime +5d # Finds stuff created more than 5 days ago.
If you want to move the files you can use -exec
find /my_directory -mtime -1d -type f -exec mv {} /new_dir/. \;
Finds files only located under /my_directory which are less than 1 day old and moves them to /new_dir
Find is one of the most useful commands you can ever learn!

issue facing with find command in linux

I am facing one problem using find command. I have below directory structure. In which a,b,c directories contain some files but i want to scan only b directory when i am using below command it scans c directory as well which is not required.
/a/b/c/
find /a/b/ f -type -mtime +90
Is there any solution to resolve this problem?
Please assist.
i want to write a find command which scans a directory for files but does not scan files of its subdirectories.
Use -maxdepth 1 to look in the b directory only.
find /a/b/ -type f mtime +90 -maxdepth 1
From man find:
-maxdepth levels
Descend at most levels (a non-negative integer) levels of directories below
the command line arguments. '-maxdepth 0' means only apply the tests and
actions to the command line arguments.
find /a/b/ -maxdepth 1 -type f -mtime +90

Bash script that deletes files older than N days but excludes files in certain folder

I need to create a bash script that deletes all files older than N days in downloads folder but would exclude all files in archive sub-folder. My folder structure is like this:
downloads/
user1_folder/
archive/
user2_folder/
archive/
...
Based on this Q&A I was able to create script that finds and deletes files older than N days, but I would like to exclude all files in archive subfolders.
#!/bin/bash
find ./downloads -mtime +32 -type f -delete
Try:
find ./downloads -maxdepth 2 -type f -mtime +32 -delete
-maxdepth levels
Descend at most levels (a non-negative integer) levels of directories below the command line arguments. -maxdepth 0
means only apply the tests and actions to the command line arguments.
Adding ! -path (your path) should do the trick
find ./downloads ! -path ./downloads/*/archive/* -mtime +32 -type f -delete

shell find files in current directory only

I am trying to display a list of files from my current folder. The files must be modified within the last n days but - and here comes my problem - they must belong to current folder only. For example, I have a folder: 1. This folder contains a file test.txt and another folder: 11. This folder contains a file: test.txt and another folder 111.
I'm using find -mtime in order to get the files modified within the past n days, but this finds the files recursively - in the subfolders. I need to get only the file from current folder: /1/test.txt.
I tried to use the option -maxdeptx but id doesn't work, it is not recognized.
Any ideas will be appreciated.
Thanks!
Use the -prune primary to prevent recusing into subdirectories.
find /1 -prune -mtime ...
-prune is a standard option; -maxdepth is a GNU extension. Note that -maxdepth 1 and -prune are roughly equivalent, that is, -maxdepth generalizes the -prune primary to support a limited form of directory following.

Unix shell script to delete older log files

Could someone please give me the command to delete log files before today (except today and yesterday) date?
You can use find with the -mtime option to get a list of files modified more than N days ago.
For example:
find . -maxdepth 1 -name '*.txt' -mtime +2
will give you all the *.txt files in the current directory older than 48 hours.
You can add -delete to actually get rid of them.
find /path/to/files* -mtime +2 -delete

Resources