Bash script unrar then rename with dir name - linux

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.

Related

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 move only files to subdirectory in linux

I would like to create a sub directory and move only files from the directory dir to the new created sub directory. I tried this:
$ mkdir dir/subdir ----> subdir is created
$ mv -if dir/* dir/subdir -----> all files and directories in dir will be moved into dir/subdir
How to move only files to subdirectory?
You can use find(1).
$ mkdir dir/subdir
$ find dir -maxdepth 1 -type f -exec mv {} dir/subdir \;
$ find dir -maxdepth 1 -type f -exec mv {} dir/subdir \;

How to zip 90days old files and move it to a specific folder using bash in linux

I have lots of files in my FILES folder. I want to zip the files that are 90days old then remove it from the FILES folder and move it to the ARCHIVES folder using bash in linux.
This is my folder structure:
root#user:/var/FILES
root#user:/var/ARCHIVES
I have created a script to zip a file but don't know how to specify the age of the file
zip -r zipped.zip *.*
so i coded something like
FILE=find *.* -mtime +90
zip -r zipped.zip $FILE
but only returns error. Thanks
You can use:
find . -mtime +90 -exec zip zipped.zip '{}' +
EDIT If you want move zipped file to an archive folder then you can do:
find . -mtime +90 -exec zip zipped.zip '{}' + && mv zipped.zip /var/ARCHIVES
you can try find
find /var/FILES/ -type f -mtime +90 -exec zip -r zipped.zip {} \; -exec mv {} /var/ARCHIVES \;
Not sure whether I understand you correct, if you want to save zipped.zip in /var/ARCHIVES
just use this:
find /var/FILES/ -type f -mtime +90 -exec zip -r /var/ARCHIVES/zipped.zip {} \;

MOVING Files and place them into folders accordingly to text file

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

How to change file and directory names with find?

I changed project name and now I have many files an directories with old name. How to replace these names with find?
find . -name "*old_name*" -exec ???
This find should work for you:
find . -name "old_name" -execdir mv "{}" new_name +
This will find files with the name old_name from the current dir in all sub directories and rename them to new_name.
Below is what I have used in the past. The biggest gotcha is the RHEL rename (c) vs Debian rename (perl) - They take different options. The example below uses RHEL c based rename command. Remove the '-type f' to also rename the directories.
find . -type f -name "*old_name*" -print0 | xargs -0 -I {} /usr/bin/rename "old_name" "new_name" {}
newname="myfile.sh"; for files in $(find Doc2/scripts/ -name gw_watch_err.sh); do echo $files; dir=${files%/*}; cfile=${files##*/}; echo "$dir -- $cfile"; echo "mv $cfile $newname"; done
Doc2/scripts/gateway/gw_watch_err.sh
Doc2/scripts/gateway -- gw_watch_err.sh
mv gw_watch_err.sh myfile.sh
you could also add:
find . -maxdepth 1 -iname file
where maxdepth will ensure you dont need to worry about sub folders and iname means case sensitive
Ok, my solution:
find . -name "*old_name*" -exec rename 's/old_name/new_name/g' {} \;
But this works for directories which name not contain "old_name", otherwise find say for example:
find: `./old_name': No such file or directory
Because it trying search in "old_name" directory, and the directory is already a "new_name"
renaming multiple directories with find: how to catch the catches
here's a little story on how to rename multiple files with find, but for the impatient I first put the proper command for your specific problem here:
find -depth -name "old name" -execdir mv -iv {} "new name" \;
and for the patient ones, a few misadventures with find, restricting changes to directories only:
we want to rename all directories named old dir into new dir recursively inside a current directory
we create an empty directory inside which we create a simple directory hierarchy:
$ cd $(mktemp -d) && mkdir -p "old dir" "subdir/old dir/old dir"
$ find | sort
.
./old dir
./subdir
./subdir/old dir
./subdir/old dir/old dir
the use of of the -exec action works only for a rename directly under a current directory, which you can see by dry-running a rename command (note that the shell doesn't output the quotes, which is not an error):
$ find -type d -name "old dir" -exec echo mv -iv {} "new dir" \;
mv -iv ./subdir/old dir new dir
mv -iv ./subdir/old dir/old dir new dir
mv -iv ./old dir new dir
note that only the first rename command would work as expected
the use of the -execdir action runs the command from the subdirectory containing the matched directory:
$ find -type d -name "old dir" -execdir echo mv -iv {} "new dir" \;
mv -iv ./old dir new dir
mv -iv ./old dir new dir
mv -iv ./old dir new dir
which seems fine as the rename commands are run in the matching directories, so we no longer dry-run:
$ find -type d -name "old dir" -execdir mv -iv {} "new dir" \;
'./old dir' -> 'new dir'
find: ‘./subdir/old dir’: No such file or directory
'./old dir' -> 'new dir'
find: ‘./old dir’: No such file or directory
$ find | sort
.
./new dir
./subdir
./subdir/new dir
./subdir/new dir/old dir
the problem is that the old dir is renamed new dir and find cannot descend further inside a renamed directory
a solution is to process each directory's contents before the directory itself, which is precisely what the -depth option does:
$ cd $(mktemp -d) && mkdir -p "old dir" "subdir/old dir/old dir"
$ find -depth -type d -name "old dir" -execdir mv -iv {} "new dir" \;
'./old dir' -> 'new dir'
'./old dir' -> 'new dir'
'./old dir' -> 'new dir'
$ find | sort
.
./new dir
./subdir
./subdir/new dir
./subdir/new dir/new dir
1. First, backup your directories and files
The following Bash code run on my OS X and Ubuntu boxes.
2. Rename the directories from old_dir to new_dir:
for d in $(find . -maxdepath X -type d -name 'old_dir'); do mv $d "$(dirname $d)/new_dir"; done
X is a number used to specify the depth of replacing old_dir
3. Rename the files from old_file to new_file:
for f in $(find . -type f -name 'old_file'); do mv $f "$(dirname $f)/new_file"; done
Don't care about #Benjamin W. and #ghoti them one for his ForMatted code and #ghoti try to orient the question to his Pitfalls.
Hi, #Benjamin W. what about this new post? and #ghoti did you run the above code incorrectly on your machine? If the code can't work just let me know or post a question pls, and if you had a better one pls post here let we know.

Resources