I want to find all *.pdf files recursively in directory /dir
In that dir there are specific files such as /dir/1/2.pdf [modified today], /dir/2/3.pdf [modified today], /dir/4/4.pdf [from yesterday]
IN that case I only want files that were modified today so: 2.pdf and 3.pdf
I also want to move those files to directory called /pdf/
I found that I can find all files in current directory modified today with:
find -maxdepth 1 -type f -mtime -1
How can I find files from today in subdirectories and move them to /pdf/ dir?
Thanks!!!
Adam
Remove -maxdepth 1 from your find command to find files in subdirectories and use -exec to move those to the target directory:
find /dir -type f -mtime -1 -exec mv {} /pdf \;
Related
The parent directory has 5 sub folders, each subfolder has .mp4s, .txt, and other file extensions, how to be in the parents folder and enter a terminal command to only pull all *.mp4s into another specified folder in Bash.
find /path/to/src -type f -name “*.mp4” | xargs -iF mv F /path/to/dst
I stand in the specified parent directory and move the files to the other specified folder that I assume is ../other-spec-dir ( a folder that is not in the search path of find)
find . -type f -name "*.mp4s" -exec mv {} ../other-spec-dir \;
Note that if there are files with identical name only the last one will survive.
I have files of type mp3 and flac. Lets say my basic directory structure is
/music/artist/album
I have (already) made
/music/artist/album/flac/
/music/artist/album/mp3/
with a recursive batch operation.
I want to loop through all of /music/ and do the following:
If /music/artist/album/ has mp3s, move them to /music/artist/album/mp3/.
If /music/artist/album/ has flacs, move them to /music/artist/album/flac/.
Some folders have mp3 and flac. Looking for either script file or command line solutions
find /music/ -name '*.mp3' -execdir mv {} mp3/ \;
find /music/ -name '*.flac' -execdir mv {} flac/ \;
-execdir runs the specified command in the directory of the current file. This lets you use mp3/ and flac/ directories relative to the current artist/album.
Here you have one way to do it :
find /music -type f -name "*.mp3" | xargs mv $1 /music/artist/album/mp3/
find /music -type f -name "*.flac" | xargs mv $1 /music/artist/album/flac/
Hope it works for you.
Greetings!
Change the current directory to where your music files are, then move mp3s to the mp3 folder and flacs to the flac folder.
cd /music/artist/album/
mv *.mp3 mp3/
mv *.flac flac/
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.
I have a directory structure like this:
news_images folder -> subfolder with current date ex. 20140626 -> files
I would like to remove all subfolders with files inside news_images directory that are older than 3 days.
This does remove only files inside news_images directory but do not touch the subfolders.
find news_images -mtime +3 -exec rm {} \;
Did you try to find only subdirectories of news_images (then remove them recursively) ?
Something like (not tested but I think it should work):
find news_images -mindepth 1 -maxdepth 1 -mtime +3 -type d -exec rm -r {} +
rm without options is not able to delete directories. Try option -r.
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