Loop Over Directories, Process files & Rename New Files - linux

I'm writing a script that would loop over the sub-directories of a given directory, find for ".js" files, compiles with closure. I'm doing this with this commands:
find ./js/ -type f -name "*.js" -exec java -jar compiler.jar --compilation_level SIMPLE_OPTIMIZATIONS --js '{}' --js_output_file '{}'.compiled \;
And then removing the old ".js" files with:
find ./js/ -type f -name "*.js" | xargs rm -f
But, I can't rename the files with the names "foo.js.compiled" to "foo.js".
Please help. Thanks in advance.

Try
for i in `find . -type f -name "*.js.compiled"`; do mv $i ${i%.*} ; done

You can do something like:
find . -name "*.js.compiled" -exec rename -v 's/\.compiled$//' {} +
Test:
$ find . -name "foo*"
./fil/foo.js.compiled
$ find . -name "*.js.compiled" -exec rename -v 's/\.compiled$//' {} +
'./fil/foo.js.compiled' renamed to './fil/foo.js'
$ find . -name "foo*"
./fil/foo.js

use the following code:
find ./js/ -name "*.js.compiled" -print0 | while read -r -d '' filename; do
mv "$filename" "${filename/js.compiled/js}";
done

Related

Linux find command get all text in the file and print file path

I need to get all the texts in the matching file in the folder. However, at the same time need to get the matching file path as well. How can I get the matching file path as well using the following command.
find . -type f -name release.txt | xargs cat
try
find . -type f -name release.txt -exec grep -il {} \; | xargs cat
Skip xargs, just do:
find . -type f -name release.txt -exec sh -c 'echo "$1"; cat "$1"' _ {} \;

find and copy so files while removing all but major version number

Trying to use "find" to copy a bunch of shared objects. Almost there, but would like to remove all version numbers except the major version.
example would be somesharedobject.so.30.0.4 copied to somesharedobject.so.30
find . -maxdepth 1 -type f -name '*.so.*' -exec cp '{}' test/'{}' \;
I'm guessing I'm going to have to pipe to xargs and sed but just hitting a mental block.
find . -maxdepth 1 -type f -name '*.so.*'|xargs -I '{}' cp '{}' test/'{}'
Think I'm just going to go with something like this
find . -maxdepth 1 -type f -name '*.so.*' -exec cp '{}' test/'{}' \;
for f in test/*.so.* ; do mv "$f" "${f%.*.*}" ; done
seems to work ok from my tests
I would write a function + script to make the job easy
#!/bin/bash
specialised_copy(){
version="${1##*so.}"
# extract the version part alone in the above step
cp "$1" "test/${1%%.so*}.so.${version%%.*}"
#cut the major version part from the version and use it for copy
#note folder test should be relative to where the script is saved
}
export -f specialised_copy
find . -maxdepth 1 -type f -name '*.so.*' -exec bash -c 'specialised_copy "$1"' _ {} \;

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 remove files without certain extension?

How to remove all files without the .txt and .exe extensions recursively in the current working directory? I need a one-liner.
I tried:
find . ! -name "*.txt" "*.exe" -exec rm -r {} \
find -type f -regextype posix-extended -iregex '.*\.(txt|exe)$'
Try this.
find . -type f ! -name "*.exe" ! -name "*.txt" -exec rm {} \;
The above command will remove all the files other than the .exe and .txt extension files in the current directory and sub directory recursively.
If you have GNU find with the -delete action:
find . -type f ! \( -name '*.txt' -o -name '*.exe' \) -delete
And if not:
find . -type f ! \( -name '*.txt' -o -name '*.exe' \) -exec rm -f {} +
using -exec ... {} + to execute rm as few times as possible, with the arguments chained.
Try the following:
rm -f $(find . -type f ! \( -name "*.txt" -o -name "*.exe" \))
This will first recursively find all files that do not end with .txt or .exe extensions, and then delete all of these files.

find command to find files and concatenate them

I am trying to find all the files of type *.gz and cat them to total.gz and I think I am quite close on this.
This is the command I am using to list all *.gzfiles:
find /home/downloaded/. -maxdepth 3 -type d \( ! -name . \) \
-exec bash -c "ls -ltr '{}' " \
How to modify it so that it will concatenate all of them and write to ~/total.gz
Directory structure under downloaded is as follows
/downloaded/wllogs/303/07252014/SysteOut.gz
/downloaded/wllogs/301/07252014/SystemOut_13.gz
/downloaded/wllogs/302/07252014/SystemOut_14.gz
Use cat in -exec and redirect output of find:
find /home/downloaded/ -type f -name '*.gz' -exec cat {} \; > output
Use echo in -exec and redirect the output:
find /home/downloaded/ -name "*.gz" -exec echo {} \; > output

Resources