Finding a File and taking backup in the same directory - linux

Lets say i have a folder /tmp and you have some files abc.sh, kbc.sh, cdg.sh, nope.py, kim.r, uio.csv. Now if you are copying new versions of abc.sh, kbc.sh from a different server like your prod but you want to take your existing file backups in the same folder like abc.sh-12-08-2016, kbc.sh-12-08-2016, cdg.sh-12-08-2016, how can you do this in one command. So here is the answer
find * -type f -exec cp {} {}_`date + "%m-%d-%Y"` \;
Above command will take back up of all files in that folder.
If you want to only take back up of .sh files
find * -type f -name "*.sh" -exec cp {} {}_`date + "%m-%d-%Y"` \;
Hope it helps

find * -type f -exec cp {} {}_date + "%m-%d-%Y" \;
Above command will take back up of all files in that folder.
If you want to only take back up of .sh files
find * -type f -name "*.sh" -exec cp {} {}_date + "%m-%d-%Y" \;

Related

linux_rename files in directory by prepending with timestamp using find command

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 {}) \;

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.

problem in copying find results in another directory

I'm trying to execute this command to copy the latest file that exist in the courant directory to another one .
find . -mtime -1 -exec cp -r {} /media/96DB-120D/bck \;
but after copying the recent files , I find the other content of the folder that does not respond to the condition -mtime -1 .
If any one had an idea about how to fix it to just copy the result of find command and thanks.
The find command probably includes the directory and then cp copies all the files in the directory. Add -type f to only have find report actual files.
Try the -p option of cp command which will preserve the timestamp of the copied file:
find . -mtime -1 -exec cp -pr {} /media/96DB-120D/bck \;
I think this is the best solution :
find . -mtime -1 -type f -exec cp --parents {} /media/960DB-120D/db \;

Find and files of same name only in directory below and copy to a different directory

This script is finding the correct file but it is also searching through other directories and copying them. How can I find only the specific files, rename to data.log_$1 and cp in new directory.
find . -mindepth 1 -maxdepth 2 -type f -name Data.log -exec cp -rfp {} / extracted/ \;
It seems you have a stray slash there: cp -rfp {} / extracted/ should probably be cp -rfp {} extracted/ (or maybe cp -rfp {} /extracted/)

Linux - Recursively go through directories?

Let's say I am in the directory /home/videos and want to iterate recursively through all of the directories underneath it. If the directory name contains "images" I want to delete the directory and all of its contents. Also, can this be done for files? Let's say in each directory go through every file and check if its name ends with ".mp3" and delete it?
Thanks
find . -name "*images*" -type d -exec rm -r {} \;
find . -name "*.mp3" -type f -exec rm -rf {} \;
-exec rm -rf {} \; : Delete all files matched by file pattern.
-type f : Only match files and do not include directory names.
-type d : matches only directory names
Here is a nice tutorial on this. http://www.cyberciti.biz/faq/linux-unix-how-to-find-and-remove-files/
So you can do something like
find /home/videos -type d -name "*images*" -exec rm -rf {} \;
and
find /home/videos -type f -name "*.mp3" -exec rm -rf {} \;

Resources