find files or directories that are 30 or more days old, recursively, BUT starting the search from specific directory - linux

I have been searching but I cannot find a way to essentially do the following in 1 line at Linux, so as to find files and directories that are more than 30 days old, starting the recursive search from script_dir:
cd $script_dir
find . -type f -or -type d -mtime +30
If I do not do the cd to change to directory that I need to start searching from recursively (and use directly only the find), then, although I specify the script_dir at find the recursive search starts from the directory I am currently and NOT from the script_dir and beneath this directory. I want to do something like the following and even if I am currently at other directory than script_dir, the recursive search to start from script_dir:
find $script_dir -type f -or -type d -mtime +30
Thank you.

In one line, you can do like this :
cd /path/to/directory && find . -type f -or -type d -mtime +30
that do the search from the specified directory

Related

how to move jpg and jpeg files whose size is greater than 10kb [duplicate]

I have some automated downloads in a proprietary linux distro.
They go to a temp scratch disk. I want to move them when they're finished to the main RAID array. The best way I can see to do this is to check the folders on the disk to see if the contents have changed in the last minute. If not then its probably finished downloading and then move it.
Assuming there could be hundreds of folders or just one in this location and its all going to the same place. Whats the best way to write this?
I can get a list of folder sizes with
du -h directory/name
The folders can contain multiple files anywhere from 1.5mb to 10GB
Temp Loc: /volume2/4TBScratch/Processing
Dest Loc when complete: /volume1/S/00 Landing
EDIT:
Using this:
find /volume2/4TBScratch/Processing -mindepth 1 -type d -not -mmin +10 -exec mv "{}" "/volume1/S/00 Landing" \;
find: `/volume2/4TBScratch/Processing/test': No such file or directory
4.3#
yet it DOES copy the relevant folders and all files. But the error worries me that something might go wrong in the future.... is it because there is multiple files and it's running the same move command for EACH file or folder in the root folder? But since it moves it all on the first iteration it cant find it on the next ones?
EDIT2:
Using Rsync
4.3# find /volume2/4TBScratch/Processing -mindepth 1 -type d -not -mmin +10 -exec rsync --remove-source-files "{}" "/volume1/S/00 Landing" \;
skipping directory newtest
skipping directory erw
RESOLVED: EDIT3
Resolved with the help in the comments below. Final script looks like this:
find /volume2/4TBScratch/Processing -mindepth 1 -type d -not -mmin +10 -exec rsync -a --remove-source-files "{}" "/volume1/S/00 Landing" \;
find /volume2/4TBScratch/Processing -depth -type d -empty -delete
rsync to move folders and files but leaves empty root dir
the next command finds empty folders and removes them.
Thanks all!
You can use GNU find with options -size for detecting files/folders of certain size and use mv with the -exec option to move to destination directory. The syntax is
find /volume2/4TBScratch/Processing -type d -maxdepth 1 -size -10G -exec mv "{}" "/volume1/S/00 Landing" \;
Using rsync
find /volume2/4TBScratch/Processing -type d -maxdepth 1 -size -10G -exec rsync --remove-source-files "{}" "/volume1/S/00 Landing" \;
The size with a - sign to indicate less than the mentioned size which in this case is 10GB. A note on each of the flags used
-type d -> For identifying only the folders from the source path.
-maxdepth 1 -> To look only on the current source directory and not
being recursive.
-exec -> Execute command following it.
Alternatively, if you want to find files that are last modified over a certain time(minutes), find has an option for -mmin which can be set to a value. E.g. -mmin -5 would return files modified five minutes ago.
So suggest adding it to your requirement, for x as you need and see if the directories are listed, then you can add the -exec option for moving the directories
find /volume2/4TBScratch/Processing -type d -maxdepth 1 -mmin -2 -size -10G
Refer to the GNU documentation for finding files according to size on how this works.
Note:- The double quotes("") are added to avoid Bash from splitting the names containing spaces.

Delete files in dir but exclude 1 subdir

I have a dir that is full of many htm reports that I keep around for 30 days and delete old ones via a cron, but there is one sub-dir I would like to keep longer. So this is the line I made in the cron, but how do I tell it to leave one sub-dir alone.
5 0 * * * find /var/www -name "*.htm*" -type f -mtime +30 -exec rm -f {} \;
Any help is greatly appreciated!
Use -prune to prevent going into a directory that matches some conditions.
find /var/www -type d -name 'excluded-directory' -prune -o -name "*.htm*" -type f -mtime +30 -exec rm -f {} \;
In addition to suggestion below, suggesting to use full path in cron.
Also to use find option -delete in-place of -exec rm -f {} \;. It is somewhat safer.
-delete
Delete found files and/or directories. Always returns true.
This executes from the current working directory as find recurses
down the tree. It will not attempt to delete a filename with a
"/" character in its pathname relative to "." for security
reasons. Depth-first traversal processing is implied by this
option. The -delete primary will fail to delete a directory if
it is not empty. Following symlinks is incompatible with this
option.
5 0 * * * /usr/bin/find /var/www -type d -name 'excluded-directory' -prune -o -name "*.htm*" -type f -mtime +30 -delete

How to exclude multiple subdirectories (same directory name) when using find command to delete files older than 30 days in a batch file?

Given the below linux directory structure, how would I skip each excluded directory, (/assess), to remove files older than 30 days for the rest of the entire directory structure?
Thanks for your assistance...
/mnt/nfsmountpoint/location1/appliance1
/assess
/discover
/bkup
/mnt/nfsmountpoint/location1/appliance2
/assess
/discover
/bkup
etc...
I cobbled together an answer and proofs:: (my asterisks * are not showing sorry)
Run from the /mnt/nfsmountpoint/ directory.
find .// -not -path "/assess/" -type f -mtime +30 -delete
validate::
Does it skip the directory?:
find .// -not -path "/assess/" -type f -mtime +30 -ls|more
Verify no current month (January 2021) files included?:
find .// -not -path "/assess/" -type f -mtime +30 -ls|grep Jan
How much space is made free?:
find .// -not -path "/assess/" -type f -mtime +30 -print0 | du --files0-from=- hc | tail -n1
find /path/to/dir -mtime +30 -type f -not -name "*assess*" -delete
Find files (-type f) in /path/to/dir as well as children directories. Specify only files that have been modified more than 30 days ago (-mtime +30) and do not include files that contain "assess" (-not -name "assess")

Alternative way to find all files in a folder and subfolders

Is there any alternative for below command to find files in a folder and subfolders as well.
find . | egrep '\./[^/]+/[^/]+/[^/]+'
Note: I don't want directories I want to get only files
Additionally, you could specify list of files extensions as your search options:
find . -type f -name "*.js" -o -name "*.ros" -o -name "*.php"
Above example, would only display file names with *.ros, *.php, and *.js as file extensions under specific folder and subfolders.
Why don't you just use find?
I am not sure from your question if you want to limit the depth. If so:
find . -type f -depth 2 -print
If you just want to find files
find . -type f -print
If you just want to find directories
find . -type d -print
You can also use -ls if -print does not float your boat.
find . -type f -ls

find folders in a directory, without listing the parent directory

Having trouble listing the contents of a folder I'm not in, while excluding the actual folder name itself.
ex:
root#vps [~]# find ~/test -type d
/root/test
/root/test/test1
However I want it to only display /test1, as the example.
Thoughts?
There's nothing wrong with a simple
find ~/test -mindepth 1
Similarly, this will have the same effect:
find ~/test/*
as it matches everything contained within ~/test/ but not ~/test itself.
As an aside, you'll almost certainly find that find will complain about the -mindepth n option being after any other switches, as ordering is normally important but the -(min|max)depth n switches affect overall behaviour.
You can do that with -exec and basename:
find ~/test -type d -exec basename {} \;
Explanation:
The find ~/test -type d part finds all directories recursively under ~/test, as you already know.
The -exec basename {} \; part runs the basename command on {}, which is where all the results from the last step are substituted into.
Then you need -type f instead of -type d.
Or, if you want to display list of folders, excluding the parent -mindepth 1 (find ~/test -type d -mindepth 1).
And now that you edited it, I think what you want may be
find ~/test -type d -mindepth 1 |cut -d/ -f3-
But I think you need to be more specific ;-)
I just fixed it with sed
find $BASE -type d \( ! -iname "." \)|sed s/$BASE//g
Where $BASE is initial foldername.

Resources