Script fails when using a variable for paths - unix

The following script fails. If I run it manually by giving absolute paths instead of a DIR it works.
#!/bin/ksh
export DIR="/clocal/www/logs/CA"
find $DIR -name "*gz" -mtime +2 -exec ls -lrt {} \;
#Working line : find . -name "*gz" -mtime +2 -exec ls -lrt {} \;
The operating system this is being run under is AIX.

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 find command in terminal does not work

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.

Unix: find files in every subdirectory named upd

I have directory tree structure which looks like this
/app/bad/upd /app/pass/upd /app/bad/upd /app/warn/upd
I want to build a find command which can list all the files in every sub-directory named upd.
Currently I list it individually
find /app/bad/upd -type f -name "*${FILE_NAME}*"
This might be what you look for:
find /app -type d -name upd -exec ls -l {} +
or perhaps:
find /tmp/* -type d -name upd -exec sh -c "ls -l {}/*${FILE_NAME}* 2>/dev/null" sh {} \;
If the upd directory is always in the 2nd directory down, you could simply do:
ls /app/*/upd

Bash, find and delete whilst retaining directory

I am trying my hand at some Bash scripting and any help would be appreciated.
The script is supposed to find all users users in /Users/ and delete everything inside. Movies, Documents, Desktop and Music.
Whilst still retaining the directory essentially emptying it. Also excluding the user files of ladmin, shared and Guest.
When I execute. It doesn't empty the files though I get the feed back I would expect. Am I missing something simple here?
DIR_CD=/Users/
cd $DIR_CD
find . -type d ! -name "*ladmin*" ! -name "*Guest*" ! -name "*Shared*" \
-maxdepth 1 -print -exec rm -rf {}/Desktop/* \;
find . -type d ! -name "*ladmin*" ! -name "*Guest*" ! -name "*Shared*" \
-maxdepth 1 -print -exec rm -rf {}/Documents/* \;
find . -type d ! -name "*ladmin*" ! -name "*Guest*" ! -name "*Shared*" \
-maxdepth 1 -print -exec rm -rf {}/Movies/* \;
find . -type d ! -name "*ladmin*" ! -name "*Guest*" ! -name "*Shared*" \
-maxdepth 1 -print -exec rm -rf {}/Music/* \;
Like this?
for d in /Users/*; do
case ${d#*/} in ladmin | Guest | Shared ) continue ;; esac
rm -rf "$d"/Desktop/* "$d"/Documents/* "$d"/Movies/* "$d"/Music/*
done
With Bash extended globbing you could do it all with a single wildcard expression.

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