finding files that were modified before a certain file - linux

Hello I am trying to write a unix code that will find all the files which were modified before a certain
file.
So far I have,
find [directory] -type f -mtime date -r [certain_file]
but the system return an error saying
find: invalid argument `date' to `-mtime'
I am trying to write this in one line and I just can't seem to figure out a way to specify the timestamp
of [certain_file].
How can I express
-mtime {date -r [certain_file]}?

Try the negation of find's -newer condition.
find [directory] -type f \! -newer [certain_file]

Related

Get n level parent with find command

I want to find my n-th level parent with the find command,
initially, when used this command, it gives me the whole file path:
Modified_files_users="$(find /var/lib/abcccc/tamm/acb-Beta-DB-abcc/abc-central/src/main/taff/com/hifinite/components/user
-type f -mtime -5;)";
Output:
/var/lib/abcccc/tamm/acb-Beta-DB-abcc/abc-central/src/main/taff/com/hifinite/components/user/file/foo.ext
Hence I used the basename GNU with find, but it only gives the file name.
Modified_files_users="$(find /var/lib/abcccc/tamm/acb-Beta-DB-abcc/abc-central/src/main/taff/com/hifinite/components/user
-type f -mtime -5 -exec basename \{} \;)";
Ouput:
foo.txt
but the Output I expect is
/file/foo.ext
Is there any way I can get this by adding anything to the -exec command?
basically either I should be able to specify the nth parent which should be included in the output OR) find the whole path after
/var/lib/abcccc/tamm/acb-Beta-DB-abcc/abc-central/src/main/taff/com/hifinite/components/user
You need to use printf with %P:
find somedirectory -type f -printf '%P\n'
Document:
%P File’s name with the name of the command line argument under which it was found removed.
Example:
$ find /home/abc/temp -type f
/home/abc/temp/A2018001.txt
/home/abc/temp/myfiles.zip
/home/abc/temp/org/springframework/boot/loader/PropertiesLauncher$PrefixMatchingArchiveFilter.class
With printf %P:
$ find /home/abc/temp -type f -printf '%P\n'
A2018001.txt
myfiles.zip
org/springframework/boot/loader/PropertiesLauncher$PrefixMatchingArchiveFilter.class

How to rename files in different directories with the same name using find

I have files named test.txt in different directories like this
./222/test.txt
./111/test.txt
I want to rename all test.txt to info.txt
I've tried using this
find . -type f -iname 'test.txt' -exec mv {} {}info \;
I get test.txtinfo
Your idea is right, but you need to use -execdir instead of just -exec to simplify this.
find . -type f -iname 'test.txt' -execdir mv {} info.txt ';'
This works like -exec with the difference that the given shell command is executed with the directory of the found pathname as its current working directory and that {} will contain the basename of the found pathname without its path. Also note that the option is a non-standard one (non POSIX compliant).

How to avoid find command in Linux from throwing errors

I know the way to delete older files using the find command is:
find /mydir/typ* -type f -mtime + 5 -delete
However if it doesn't find a file, it returns an error saying no matches found. Is there a way to just fail silently, i.e. not to throw an error if it can't find the file. If it does, delete it.
find /mydir/typ* -type f -mtime + 5 -delete 2> /dev/null
So, from your comment on another answer, your full error is zsh: no matches found. The error is coming from your shell, not find.
/mydir/typ* is a shell glob, and zsh by default gives you an error if no files match the glob. More info on that here.
It's not clear what your directory structure and intended use are, but if you want to find files matching typ* in /mydir, you want find /mydir -name 'typ*' -type f -mtime +5 -delete. Otherwise, you'll have to be more specific about your situation.

Inserting text in a 'find' command search

I have a find string that finds all the instances of a particular filename in a path, like so:
find /opt/logs* -type f -name "deploy.log" -exec ls {} \;
I need to return the result with 'FINENAME=' prepended on each line. Having a hard time figuring the best way.
find /opt/logs* -type f -name deploy.log | sed 's/^/FILENAME=/'
Note that if you have a directory named /opt/logs (and you're not trying to look in /opt/logs-foo/ and /opt/logs-date, or the like) you can drop the * and write find /opt/logs -type ...
Use the -printf option:
find /opt/logs* -type f -name "deploy.log" -printf='FILENAME=%p\n'
%p will get expanded to the file's name.

Use find results as command line argument for another process

How can I use find results as a command line argument for another process?
For example:
find -type f -iname "server*error*log" | vim ??
How to make vim to open returned filename?
You need to use the -exec option of find:
find -type f -iname "server*error*log" -exec vim {} \;
The placeholder {} will get replaced by the actual filename. The -exec command needs to get terminated with the \;.
If you want not just one file, but many, to be acted on by one program (in your case editing in Vim), I think you want to pass it to xargs:
find -type f -iname "server*error*log" | xargs vim

Resources