MOVING Files and place them into folders accordingly to text file - linux

I need to move files from ORIGIN and place them to DESTINATION accordingly to the information contained in text file "toto.txt"
I do NOT know how to code the part which says:
place these files accordingly with the information contained in toto.txt which states
the sub-folder structure on DESTINATION folder"
toto.txt conatins the folder structure of ORIGIN and the files must be moved accordingly to DESTINATION but with the original folder structure location.
# My working Paths
MY_DIR1="/media/nss/MBVOL1/TEST/ORIGIN"
MY_DIR2="/media/nss/MBVOL1/TEST/DESTINATION"
# Flag files older than 1 day and list their name\full path to “TOTO” text file
echo "REPORT Created"
cd $MY_DIR1 && find . -mindepth 0 -maxdepth 40 -mtime +1 -type f > toto.txt
cp $MY_DIR1/toto.txt /$MY_DIR2
# Flag files older than 1 day then MOVE file to “DESTINATION” Folder
echo "FILES Moved"
find $MY_DIR1 -mindepth 0 -maxdepth 400 -type f -mtime +14 -exec mv '{}' $MY_DIR2 \;

Try this:
cd "$MY_DIR1"
# Duplicate directory structure
find . -type d -exec mkdir -p "$MY_DIR2"/{} \;
# move files older than 1 day
find . -type f -mtime +1 -exec mv {} "$MY_DIR2"/{} \;
You can combine them into one command:
find . -type d -exec mkdir -p "$MY_DIR2"/{} \; -o -type f -mtime +1 -exec mv {} "$MY_DIR2"/{} \;

Use something like this...
cat ${MY_DIR2}/toto.txt | while read FILE ; do
mv -v "${MY_DIR1}/${FILE}" "${MY_DIR2}"
done

Related

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"

How to copy files along with a part of folder structure in unix?

I want to copy all the files from a folder which is more than x days old to a new path.
Source folder structure:
/opt/install/sw1/team/p2.csv
/opt/install/sw2/team/p2.csv
Destination should be:
/work/backup/sw1/team/p2.csv
/work/backup/sw2/team/p2.csv
Will this do:
mkdir /work/backup/sw1/team
find /opt/install/sw1/team -maxdepth 0 -mtime +5 -exec cp '{}' /work/backup/sw1/team \;
mkdir /work/backup/sw2/team
find /opt/install/sw2/team -maxdepth 0 -mtime +5 -exec cp '{}' /work/backup/sw2/team \;
If I understand correctly:
cp -a /opt/install/sw1 /opt/install/sw2
Or for verbose:
cp -av /opt/install/sw1 /opt/install/sw2
-a says to copy everything and preserve permissions. If this is not what you want, then -r might be more appropriate.

Bash script unrar then rename with dir name

I want to rename files in a dir then change the file name base on the dir name this is what I had but it's not quite working
find . -type d | while read dir
do
cd "$dir"
find . -name \*.rar -exec unrar x '{}' \;
find . -name \*.avi -exec mv '{}' "$dir.avi" \;
cd ..
done
root/directory.1.with.rar.in/
root/directory.2.with.rar.in/
root/directory.2.with.rar.in/
root/directory.2.with.rar.in/
root/directory.2.with.rar.in/
I want all the rar in the Directory to be extracted in root then renamed with the name of the directory it was extracted from.

Find and rename a directory

I am trying to find and rename a directory on a linux system.
the folder name is something like : thefoldername-23423-431321
thefoldername is consistent but the numbers change every time.
I tried this:
find . -type d -name 'thefoldername*' -exec mv {} newfoldername \;
The command actually works and rename that directory. But I got an error on terminal saying that there is no such file or directory.
How can I fix it?
It's a harmless error which you can get rid of with the -depth option.
find . -depth -type d -name 'thefoldername*' -exec mv {} newfoldername \;
Find's normal behavior is to process directories and then recurse into them. Since you've renamed it find complains when it tries to recurse. The -depth option tells find to recurse first, then process the directory after.
It's missing the -execdir option! As stated in man pages of find:
-execdir command {};
Like -exec, but the specified command is run from the subdirectory containing the matched file, which is not normally the directory in which you started find.
find . -depth -type d -name 'thefoldername*' -execdir mv {} newfoldername \;
With the previous answer my folders contents are disappeared.
This is my solution. It works well:
for i in find -type d -name 'oldFolderName';
do
dirname=$(dirname "$i")
mv $dirname/oldFolderName $dirname/newFolderName
done
.../ABC -> .../BCD
find . -depth -type d -name 'ABC' -execdir mv {} $(dirname $i)/BCD \;
Replace 1100 with old_value and 2200 with new_value that you want to replace.
example
for i in $(find . -type d -iname '1100');do echo "mv "$i" "$i"__" >> test.txt; sed 's/1100__/2200/g' test.txt > test_1.txt; bash test_1.txt ; rm test*.txt ; done
Proof
[user#server test]$ ls -la check/
drwxr-xr-x. 1 user user 0 Jun 7 12:16 1100
[user#server test]$ for i in $(find . -type d -iname '1100');do echo "mv "$i" "$i"__" >> test.txt; sed 's/1100__/2200/g' test.txt > test_1.txt; bash test_1.txt ; rm test*.txt ; done
[user#server test]$ ls -la check/
drwxr-xr-x. 1 user user 0 Jun 7 12:16 2200
here __ in sed is used only to change the name it have no other significance

Resources