linux_rename files in directory by prepending with timestamp using find command - linux

I am trying to rename files in current directory by prepending timestamp value using find command like below
find . -type f -exec mv {} $(date +%Y-%m-%d)_{} \;
But getting 'Can't move, no such file or directory error', but appending works well with below command, not sure what is the difference between two.
find . -type f -exec mv {} {}.$(date +%Y-%m-%d) \;

Try this
find . -type f -exec mv {} $(date +%Y-%m-%d)$(basename {}) \;

Related

log deleted files in linux

I am using the following command to delete the files that are older than 10 days, but I also want to store(log) the list of files that are being deleted using the below command.
find ./path/delete -type f -name '*' -mtime +10 -exec rm {} \;
If you create a file to log to (touch ./path/to/logfile), just add another -exec to your command. Below is a very basic example, but you can add to it:
find ./path/delete -type f -name '*' -mtime +10 -exec rm {} \; -exec echo {} >> ./path/to/logfile \;

How do I recover files that disappeared after wrong MV command format?

I'm trying to move files from the current directory to another directory by date, but I accidentally used the wrong target format:
find . -maxdepth 1 -mtime +365 -type f -exec mv "{}" "..\folder" \;
instead of
find . -maxdepth 1 -mtime +365 -type f -exec mv "{}" "../folder" \;
Then my files just disappeared.
I can't seem to find it anywhere. I've tried on both target & source directories and even the non existent directory that I have accidentally sent the files to.
I would just like to know if I can still recover the files.
They're all gone. When you run:
find . -maxdepth 1 -mtime +365 -type f -exec mv "{}" "..\folder" \;
You are executing, for every file, the command:
mv filename ..folder
In other words, you renamed every file to the name ..folder. Each file overwrote the next one. The contents of the ..folder file are whatever file was last processed by your command, and all the rest are gone.

How to clean up folders efficiently using shell script

I am using a directory structure with various folders. There are new files created daily in some of them.
I have created some programs to clean up the directories, but I would like to use a shell script to make it more efficient.
Therefore I would like to store an "archiving.properties" file in every folder that needs to be cleaned up. The properties file should contain the following variables
file_pattern="*.xml"
days_to_keep=2
Now my clean up routine should:
find all properties files
delete all files that match the file name pattern (file_pattern) and that are older then the defined number of days (days_to_keep) in the directory where the properties file was found.
So my question is how can I do this in the most efficient way?
find . -type f -name "archiving.properties" -print
find . -type f -name "<file_pattern>" -mtime +<days_to_keep> -delete
currently I was trying the following in a single folder. It prints out the command correctly, but it is not executed.
#!/bin/bash
. archiving.properties
find . -type f -name "*.xml" -mtime +1 -exec rm -rf {} \;
echo " find . -type f -name \"${file_pattern}\" -mtime +${days_to_keep} -exec rm -rf {} \;"
Result is: find . -type f -name "*.xml" -mtime +1 -exec rm -rf {} \;
Thanks for your help in advance.
I got a final result
echo "start deleting files in " $(pwd) " ... "
#filename of the properties
properties="clean_up.properties"
#find all properties files
for prop in $(find . -type f -name $properties);do
#init variables
file_pattern="*._html"
days_to_keep=14
#load the variables from the properties file
. "$prop"
#define the folder of the properties file
folder=${prop%?$properties}
#remove all files matching the parameters in the folder where the properties were found
echo ">>> find $folder -type f -name \"${file_pattern}\" -mtime +${days_to_keep} -exec rm -f {} \;"
find $folder -type f -name "${file_pattern}" -mtime +${days_to_keep} -exec rm -f {} \;
done
echo "... done"

Using find command on in a Bash Script to find integers

I need to find and archive files with a certain file name e.g. ABC_000.jpg
find ~/my-documents/ -iname "ABC_***.JPG" -type f -exec cp {} ~/my-documents/archive/ \;
however I can not seem to find a way to limit the find function to find only 3 integers as there are files that are named ABC_CBA.jpg that I do not want included
Try this find:
find ~/my-documents/ -iname "ABC_[0-9][0-9][0-9].JPG" -type f -exec cp '{}' ~/my-documents/archive/ \;
EDIT: Or using regex:
find -E ~/my-documents/ -iregex ".*ABC_[0-9]{3}\.JPG" -type f -exec cp '{}' ~/my-documents/archive/ \;

In Unix,cmd to search a file recursively and retrieve the file instead of just the path of the file

In Unix, what is the single cmd that lets me search and locate a file recursively and then retrieve the file instead of just the path of the file?
What do you mean by retrieve?
You can simply use -exec argument to find.
$ find /path/to/search -type f -name '*.txt' -exec cat {} \;
$ find /path/to/search -type f -name 'pattern' -exec cp {} /path/to/new \;
The second one should work.
cat `find /wherever/you/want/to/start/from -name name_of_file`
Note those quotes are backquotes (`).

Resources