bash Script - find: invalid predicate `-newermt' error [duplicate] - bash

This question already has answers here:
How to use 'find' to search for files created on a specific date? [closed]
(9 answers)
Closed 4 years ago.
I have a scenario where i have to I need to find the files for a specific year for eg- 2012 and empty the files. Trying to use the below command which is throwing
#!/bin/bash
find . -type f -newermt 2012-01-01 ! -newermt 2012-01-01 -exec truncate -s 0 {} \;
find: invalid predicate `-newermt' error Below is the command
Strange thing is this command works for some people.Found that there may be some version compatibility issue for the find with the predicate 'newermt' for my system.
So just wanted to check
1. So how can i resolve the above error
2. Is there any way by which i can perform my task i.e, - find the files for a specific year for eg- 2012 and empty the files.

The issue got resolved for me as in my other box/environment where i needed the scenario i had a upper find version (4.4.2). Only in the test environment i had a lower version and hence was getting the issue.Thanks all for your inputs.

Related

bash script to create folder names out of a portion of a filename [duplicate]

This question already has answers here:
Remove a fixed prefix/suffix from a string in Bash
(9 answers)
Closed last month.
I'm new to bash, and coding. I have a list of files:
test-T01___2022.txt
test-T01__2021.txt
test-T01_NONE.txt
test-T02___2022.txt
test-T02__2021.txt
test-T02_NONE.txt
test-T03___2022.txt
test-T03__2021.txt
test-T03_NONE.txt
I'm trying to write a script to create folders T01 (containing *T01 files), T02 (containing all files with T02), etc. I'm trying with wildcards and regexps and something similar to this post but having some trouble. I appreciate some help.
Many thanks!
Use the bash prefix and suffix removal operations. See the link in the comments for more details.
For example:
files=...
for file in $files
do
a=${file#test-}
dir=${a%%_*}
mkdir "$dir"
mv "$file" "$dir"
done

bash script to remove all directories created before a certain date [duplicate]

I want to use the find command to find these directories:
Access: 2013-12-13 10:59:46.190886900 -0500
Modify: 2013-12-03 07:04:02.995890600 -0500
Change: 2013-12-03 07:04:02.995890600 -0500
Birth: 2013-12-02 07:04:02.000000000 -0500 (I want a time after '12-03')
This is the command I ran but it still lists older directories:
find . -type d -newerBt '2013-12-03 00:00:00' -exec du -h {} \;
How can I modify this line to find the directories created after that date? What is the difference between -newerct and -newerBt. I think I want the birth date.
Note: I am running this with the latest cygwin.
You could use stat instead:
find . -type d -exec bash -c '(( $(stat -c %W "{}") > $(date +%s -d '2013-12-03') )) && du -h "{}"' \;
You are finding directories, but showing files contained therein.
Those files may have birth dates that lie before that of the containing directory. For example, create a file, then a directory, and move the file into that directory.
This is the difference between birth date and change date. If a file is moved into the dir, the dir is changed, so I think -newerct is what you want.
I wonder if it's a TimeZone issue? What is echo $TZ? What happens if you do unset TZ (and unsetenv TZ, too, in csh) and re-try the same commands?
Here's the man page excerpt for -newerXY. Maybe reading it will trigger some thoughts?
-newerXY reference
Compares the timestamp of the current file with reference. The reference
argument is normally the name of a file (and one of its timestamps is used
for the comparison) but it may also be a string describing an absolute time.
X and Y are placeholders for other letters, and these letters select which
time belonging to how reference is used for the comparison.
a The access time of the file reference
B The birth time of the file reference
c The inode status change time of reference
m The modification time of the file reference
t reference is interpreted directly as a time
Some combinations are invalid; for example, it is invalid for X to be t.
Some combinations are not implemented on all systems; for example B is not
supported on all systems. If an invalid or unsupported combination of XY is
specified, a fatal error results. Time specifications are interpreted as for
the argument to the -d option of GNU date. If you try to use the birth time
of a reference file, and the birth time cannot be determined, a fatal error
message results. If you specify a test which refers to the birth time of
files being examined, this test will fail for any files where the birth time
is unknown.

Bash - compare parameters thrown as $# and pass them as $1 $2 accordingly [duplicate]

This question already has answers here:
How to change a command line argument in Bash?
(3 answers)
How to compare timestamp on files in bash scripting
(3 answers)
Closed 5 years ago.
I often use exiftool to copy metadata from one file to another.
exiftool -TagsFromFile FILE1[SOURCE] FILE2[TARGET]
I am trying to speed up the workflow and figure out a bash script that would allow me to simply select two files in file explorer - right click on them - open with - thescript.sh
/path/to/thescript.sh $1 $2
As you can see the most important part is to select a right file as the source ($1) / target ($2).
Source files often named like this:
20170630_181348_2674.jpg or 20170630_181348_2674.dng
Target files usually have a suffix added to the name e.g. 20170630_181348_2674_0001.jpg or 20170630_181348_2674_v2.jpg
So one thing I know for sure is that the source file name is always shorter.
Another thing, the files I use as the source are always older than the ones I copy metadata to.
I was wondering if there is any way in bash to compare inputs ($#) by their file name or by the file modify date and place them as $1 and $2 accordingly.
Any ideas would be greatly appreciated.

Change multiple file names in a pattern [duplicate]

This question already has answers here:
Renaming files in bash
(5 answers)
Closed 7 years ago.
I have a number of files with somewhat similar names:
HappyBD_Stereo_144kbps.mp3
HappyBD_Stereo_192kbps.mp3
HappyBD_Stereo_256kbps.mp3
...
For some reason, I need to change/shorten these names into something like
HappyBD_Ste_144k.mp3
HappyBD_Ste_192k.mp3
HappyBD_Ste_256k.mp3
...
Can someone recommend a good way to automate this kind of file name changes? Thanks.
you can try,
for filename in `ls *_Stereo_*.mp3`; do
newfilename=$(sed 's/_Stereo_/_Ste_/g' <<< $filename);
mv $filename $newfilename;
done

Exclude directories with a certain prefix using find [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
Provided I'm not missing something that should be obvious, the OSX version of bash is different from Linux here, because I thought this question would provide all I needed to know. It didn't.
I'm trying to use find to find all directories that do not begin with "." (Mac OS X uses the .-prefix for hidden folders, e.g., /volumes/OD/.Trashes). I want to pipe all the non-hidden directories to rysnc to periodically mirror two local directories.
I tested to make sure I'm using find correctly with this code here:
find /volumes/OD -type d -iname ".*"
It finds the directories:
/volumes/OD/.Trashes
/volumes/OD/.Spotlight-V100
/volumes/OD/.fseventsd
So far so good. But when I try negating the condition I just tested, I get an error:
find /volumes/OD -type d -iname ! ".*"
yields this error:
find: .*: unknown primary or operator
I've tried escaping the "." with "\", but I only get the same error message. I've tried removing the parenthesis, but I get same error message. What am I missing here? Should I be using another operator besides iname?
The ! must precede the condition:
find /volumes/OD -type d '!' -iname '.*'
That said, you shouldn't need to pipe your file-list from find to rsync; I'm sure the latter offers a way to exclude dot-folders. Maybe
rsync --exclude='.*' ...
?
Reason :
! is prefix of jobs such as:
!23
!routENTER
You should do :
find /volumes/OD -type d \! -iname '.*'
ruakh has got the right answer, but here is an alternate way of doing a search:
find /volumes/OD -type d -not -iname ".*"

Resources