unix find command in terminal does not work - unix

I need to place a command that will search for all files in the current directory as well as in its sub-directories - ending by ~, and/or all files that start or end by #. The command line will show and erase all files found. Only one command is allowed: no ’;’ or ’&&’ or other shenanigans.
here is my command:
find . -name "#*" -o -name "*#" -o -name "*~" -print -delete
but it erases only the files ending in ~

You forgot to enclose the conditions with parenthesis (). This means that only the last condition will trigger the actions -print and -delete.
The default is and -a, which would not require the parenthesis, that's why most find commands such as find -type f -name "pattern" -print works without parenthesis.
You should try:
find . \( -name "#*" -o -name "*#" -o -name "*~" \) -print -delete

How about -print0 primary in conjunction with xargs -0'' like this .
find . -type f -print0 | xargs -0 rm
za:temp za$ ls
file.txt file.txt~
za:temp za$ find . -name "*~" -print0 | xargs -0 rm
za:temp za$ ls
file.txt
Or with xargs -I {} plus your comand which does the same thing .
# xargs -I {} to capture the value of find
find . -iname *something* | xargs -I {} rm {}
edit : if you can't see the files that start with # using find . then the files have spaces within the name of the file(s) like # file.txt. you will need to find files with spaces with something like find . -name "* *" and then remove the spaces.

Related

Searching for particular files in a directory non-recursively using find. AIX

I have a script which has the following command. I am trying to edit this in such a way that it only searches the files in the directory of the path without going in the subdirectories. That is not recursive search
find {Path} -name "cor*" -type f -exec ls -l {} \;
Example: The command should give cor123.log only and not cor456.log. Currently it gives both
<Path>
..cor123.log
<directory>
..cor456.log
I tried using -maxdepth but it's not supported in AIX. -prune and -depth didn't help either.
Will appreciate any help. Thanks
You can use
find . -name . -o -prune
to find files and directories non-recursively.
So in your case this one will work:
find . -name . -o -prune -name 'cor*' -type f -exec ls -l {} \;
Do you need find for selecting files only?
When you know that all files starting with cor are regula files, you can use
ls -l ${Path}/cor*
or
ls -l ${Path}/cor*.log
When you need the -type f, you can try to filter the results.
The filename can not have a /, so remove everything with an / after the Path.
We do not know the last char of ${Path}. It can be /, which will make the grep -Ev "${Path}/.*/" filter useless. After the Path at least one character is needed before looking for the next /.
find "${Path}" -name "cor*" -type f 2>/dev/null| grep -Ev "${Path}..*/" | xargs -ls
Late answer but may save some. In aix
find /some/directory/* -prune -type f -name *.log
For instance make your path have the last forward slash with a wildcard then -prune
/*
find /some/directory/* -prune -name "cor*" -type f -exec ls -l {} \
Tested.

Unix shell script Or operator to delete fnix

I want to delete .txt and .csv files from a particular folder. Below is the shell script I am using. But its not deleting the files, if I use only of the file extension then it works. Can you please help here. I also used ||, but did not work.
function purge_inbox_donefile(){
cd $INBOX_DIR
find . -path "*/done*" -iname ["*.txt" -o "*.csv"] -exec rm -f {} \; -print
}
You can use -o between -iname parameters rather than within the pattern. Such as:
-iname "*.txt" -o -iname "*.csv"
If you need to bracket your expression to keep it distinct from your other parameters, do not forget to escape your brackets, ie:
\( -iname "*.txt" -o -iname "*.csv" \)

Concatenate all files recursively, ignoring one file extension

I want to create a concatenated file which appends all files except those which end in .XYZ from a directory (recursing into subdirectories).
I tried this but it does not work:
find . -type f | grep -v *.XYZ -exec cat {} \; > /tmp/alldata.txt
This works but fails to exclude files ending in ".XYZ":
find . -type f -exec cat {} \; > /tmp/alldata.txt
find . -type f -not -name "*.XYZ" -exec cat {} \; > /tmp/alldata.txt
More recent versions of gnu find include -not which negates the next argument. In this case, you can combine that with the -name argument to get what you want without invoking grep -v.

How do I delete certain files in the current directory that doesn't match the given pattern?

using rm *.sh to delete files ending in .sh is easy and understandable. But how do i delete all files in the current directory that does not end in .jar
something like rm * -except *.jar
Try this:
find . -mindepth 1 -maxdepth 1 ! -name '*.jar' | sort
If you really want to delete all the files in its output, then just do
find . -mindepth 1 -maxdepth 1 ! -name '*.jar' -delete
You can read the find(1) manual page for more information on this really powerful tool.
EDIT:
Since the -delete flag is only found in GNU find > 4.2.3 (as pointed out by SiegeX), here are a couple of alternatives, which also make sure we are not trying to delete directories:
find . -mindepth 1 -maxdepth 1 ! -type d ! -name '*.jar' -print0 | xargs -0 -r rm -f
The -r xargs flags is a GNU extension, so this is slightly more portable (it works on *BSD), but not as clean:
find . -mindepth 1 -maxdepth 1 ! -type d ! -name '*.jar' -print0 | xargs -0 rm -f
As a last - but most portable - resort:
find . -mindepth 1 -maxdepth 1 ! -type d ! -name '*.jar' -exec rm '{}' ';'
This has the disadvantage of invoking rm separately for each file, which makes it significantly slower.
echo $(ls | grep -v '.jar$')
rm $(ls | grep -v '.jar$')
You can do this by enabling the extended glob extglob option and then putting your pattern inside !() like so:
shopt -s extglob;
rm !(*.jar)
Note that extglob also gives you the following:
?() -- Match zero or one of the pattern
*() -- Match zero or more of the pattern
#() -- Match exactly one of the pattern
!() -- Match anything except the pattern

Collect .txt and .log files using find

I currently have this script to compress log files:
find . -name '*.log' -print0 | xargs -0 tar zcf $file
Currently finds and compress all the *.log files. I would like to modify it to include also all the ".txt" files but I don't know how, this should be fairly simple right?
find . -type f \( -name "*.log" -o -name "*.txt" \) -exec tar zcf "$file" {} +
Alternatively:
find . -type f -regex ".*\.\(txt\|log\)$" -exec tar zcf "$file" {} +
No need for xargs if your version of find is POSIX compliant and can have it's -exec command terminated with a + (most can)

Resources