Problems with understanding and combining linux terminal commands - linux

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!

Related

Combining a few " find "commands in linux

find /home/imk-prac/ \( -type f -size -13c -name '*\?plik\?*' \) -o\( -type d -name '\[Kolo1\]*' \)2> /dev/nul;
This command counts normal files which has less than 13 symbols and contains a sequence of symbols ?plik?.
I want to add looking for files which were modified less than 30 days and I wrote this command:
find /home/imk-prac/ -type f -mtime -30 -exec ls -l {} \; > /dev/null
I don't know how to combine this two commands in to one.
I wanted to add looking for files with specified quantity of symbols and I found this command:
grep -Po '(^|\s)\S{64}(\s|$)' file
But there is the same problem or even worse, because of grep command.
Thanks for your time and I hope you will help me to figure it out ;)

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

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

FInd patternf for multiple pattern of files

I need to search for multiple pattern of files and check their mtime and if it morethan 30 days then delete all the files. I am using the below command but it's deleting only one pattern of file and not all. Kindly let me know where is the mistake in my command.
find /root -type f \( -name "*.tgz" -o -name "*.bz2" \) -mtime +30 -print -exec rm '{}' +
Try escaping parentheses in the command and adding a wildcard character:
find /root -type f \( -name "*.tgz" -o -name "*.bz2" \) -mtime +30 -exec rm {} \+

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

Regular expression - Find ocurrences that has one string

Im trying to find all files modified during de last 24 hours in /var/www/vhost directory.
That is working ok with the find command, then, I want to filter the list because i don't want jpg files, jpeg files and so on.
Now i have this and it's working ok:
find /var/www/vhosts/ -ctime 0 -type f | grep -ve ".jpg$" | grep -ve ".jpeg"
I guess (and know) there's a better solution to my problem.
Any help?
change your find command itself to
find /var/www/vhosts/ -not \( -name "*.jpeg" -o -name "*.jpg" \) -ctime 0 -type f
You can do it all with one find command:
find /var/www/vhosts -ctime 0 -type f \! -iname \*.jpg \! -iname \*.jpeg
Use -regex and ! (negation):
find $DIR -regextype posix-extended ! -regex '.*\.(gif|jpg|pdf|png)$'

Resources