shell script that allows to empty a file when it exceeds a certain size - linux

I'm looking for a Linux script that allows to empty the contents of a file when it exceeds a certain size for example 50 kB.
I tried this script :
#!/bin/bash
find /home/walid/Documents -type f -size +50k -exec echo >"{}" \;
but it does not work.
On the other hand it works well for deleting files:
#!/bin/bash
find /home/walid/Documents -type f -size +50k -exec rm "{}" \;

Your redirection (>) takes place before starting find. You probably now have a file of name {}.
I propose to use truncate instead of a redirection for overwriting the file:
find /home/walid/Documents -type f -size +50k -exec truncate --size 0 "{}" \;

A little tweak on your first script should work fine:
#!/bin/bash
find /home/walid/Documents -type f -size +50k -exec sh -c 'echo -n > {}' \;

Give a try to this:
find /home/walid/Documents -type f -size +50k -exec cp /dev/null {} \;
That should work in any *nix like operating system, but also you could give a try to truncate -s 0 filename
find /home/walid/Documents -type f -size +50k -exec truncate -s 0 {} \;

Related

Problems with understanding and combining linux terminal commands

First one :
We found several files and we have to copy that to kat4 and here is code, but it doesn't seem to work corectly
find /home/imk-prac/ -type f -size -13c -name '*\?plik\?*' -exec cp {} /home/inf-19/aduda/\*kat1\*/\*kat2\*/\*kat4\*/ \; 2> /dev/null
'cp' I assume that it is copy, but I don't know what 'exec' and '{}' do.
Second one:
find /home/imk-prac/ \( -type f -size -13c -name '*\?plik\?*' \) -o\( -type d -name '\[Kolo1\]*' \)2> /dev/null
Generally,I understand this line (except for '2' and '-o') , but I want to add looking for files which were modificated in less that 30 days and here is what I wanted to combine with upper command :
find /home/imk-prac/ -type f -mtime -30 -exec ls -l {} \; > /dev/null
As a result I wrote it down as:
find /home/imk-prac/ \( -type f -size -13c -name '*\?plik\?' -mtime -30 -exec ls -l{}\) -o \( -type d -name '\[Kolo1\]*' \) 2> /dev/null
but it doesn't work
Moreover, I wanted to add looking for files with speciefied quantity of symbols and I found this command:
grep -Po '(^|\s)\S{64}(\s|$)' file
But I have no idea how to combine all of those 3 upper commands.
I will be grateful for any help, thank you for your time!

How to delete files matching linux command output

I have a list of files which I got using find / -type f -size +10M -exec ls -l {} \;
I got this command from here
How can I remove all these files ?
I tried
sudo rm `find / -type f -size +10M -exec ls -l {} \;`
but it doesn't work.
Also, what does {} \ do ? And what's the use of -exec in this command, will the pipe operator not work ?
I think it should be possible to have find run rm on each file found, but I couldn't get it to work.
So here is my solution using a for loop:
for $f in `find / -type f -size +10M`;do rm $f;done
Thanks guys, I finally got it to work with #some-programmer-dude suggestion:
find / -type f -size +10M -exec rm {} \;

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"' _ {} \;

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

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/ \;

Resources