Need help in ls -ltr linux bash - bash

I want to list the last 3 logs in a specific folder and redirect the output to another tmp.out file.
ls -ltr /home/oracle/$dbserver/*.log | awk '{print $9}' | tail -3 | tee tmp.out
What I expect to see in the tmp file is:
a.out
b.out
c.out
What I get instead is:
/home/oracle/DB1/a.out
/home/oracle/DB1/b.out
/home/oracle/DB1/c.out
I definitely need to use something between tail and tee. Can somebody help me?

ls -ltr /home/oracle.$dbserver/*.log | tail -3 | awk '{print $NF}' | awk -F/ '{print $NF}' | tee tmp.out

Jut cd to that directory and then
cd /home/oracle/$dbserver/ && ls -ltr *.log | awk '{print $9}' | tail -3 | tee $OLDPWD/tmp.out

the basename command will take a string of the form /foo/bar/baz.txt and return simply baz.txt. Invoke it via xargs, making sure to use the command-line flag -n 1 so it only sends one filename at a time to basename.
ls -ltr /home/oracle/$dbserver/*.log | awk '{print $9}' | xargs -n 1 basename | tail -3 | tee tmp.out

Related

how to retrieve open file handle count for a pid via shell script

I am trying to retreive open file handle count for a particular PID in a variable via shell script and displaying the same.It is not showing the correct count. Can someone please advise?
pid=$(ps -ef | grep 'instance="AC"' | grep -v grep | awk '{print $2}') f_count=$(ls /proc/$'{pid}' | wc -l)
Expected output:
=============
When executed in command line , it shows
ps -ef | grep 'service_instance="AC"' | grep -v grep | awk '{print $2}'
25939
ls /proc/25939/fd | wc -l
98
Actual Output:
f_count= 0
Appreciate your help, thanks
Suggesting to replace:
pid=$(ps -ef | grep 'instance="AC"' | grep -v grep | awk '{print $2}')
With:
pid=$(pgrep -f 'instance="AC"')
Notice that pid takes the first matched process.
If there are more than one matched process pgrep returns multiple lines.
Suggesting to replace:
f_count=$(ls /proc/$'{pid}' | wc -l)
with
f_count=$(ls /proc/${pid}/fd | wc -l)
Or all together in one line
f_count=$(ls /proc/$(pgrep -f 'instance="AC"')/fd | wc -l)

SHELL: Display oldest created files DATE and its path

macOS Majove
I can find the oldest Created file in the directory:
cd /path/ && ls | xargs mdls -name kMDItemContentCreationDate | awk '{print $3, $4}' | sort -n | head -n1
Outputs:
2020-02-04 08:24:46
But I struggle to get its path displayed alongside. I would want something like:
2020-02-04 08:24:46 /Path/Filename
I can easily do it with last access time but not when it comes to creation time. Any help would be much appreciated. Pretty sure it is something simple, and I am just overthinking it :(
Use a while loop instead of xargs. Then you can print the filename along with the metadata.
cd /path/ && ls | while read f; do
printf '%s %s %s\n' $(mdls -name kMDItemContentCreationDate "$f" | awk '{print $3, $4}') "$f"
done | sort -n | head -n1

AWK variable input

I've got the following bash code:
md5sum -c checksum.md5 2>&1 | grep FAILED | awk '{print $1}' | sed 's/:$// > /tmp/check.tmp
awk '{system("wget http://example.com/"$1"")}' /tmp/check.tmp
How can I use awk without a temp file?
Something like
files=`md5sum -c checksum.md5 2>&1 | grep FAILED | awk '{print $1}' | sed 's/:$//`
awk '{system("wget http://example.com/"$1"")}' $files
You can simplify the whole command to this:
md5sum -c checksum.md5 2>&1 |\
awk -F'[:/]' '/FAILED/{system("wget http://example.com/"$(NF-1))}'
wget has a switch -i that can come in handy:
md5sum -c checksum.md5 2>&1 | \
sed -n '/FAILED$/ { s/: FAILED$//; s!^!http://example.com/!; p; }' | \
wget -i
Like this:
awk '{system("wget http://example.com/"$1"")}' <<< $files

BASH - GREP - invert much not working

I'm trying to find a number of open file descriptors by user "apache". I would like to exclude ls: /proc/PID/fd: No such file or directory but the GREP exclusing is not working:
# for pid in $(lsof -u apache | awk '{ print $2 }' | uniq); do ls -1 /proc/$pid/fd | grep -v "No"; done | wc -l
ls: /proc/PID/fd: No such file or directory
1944
Try |& grep -v "No such file or directory"
Unlike |, |& will also redirect the error output of ls
Try:
for pid in $(lsof -u apache | tail -n +2 | awk '{ print $2 }' | uniq); do ls -1 /proc/$pid/fd ; done | wc -l

Use each line of piped output as parameter for script

I have an application (myapp) that gives me a multiline output
result:
abc|myparam1|def
ghi|myparam2|jkl
mno|myparam3|pqr
stu|myparam4|vwx
With grep and sed I can get my parameters as below
myapp | grep '|' | sed -e 's/^[^|]*//' | sed -e 's/|.*//'
But then want these myparamx values as paramaters of a script to be executed for each parameter.
myscript.sh myparam1
myscript.sh myparam2
etc.
Any help greatly appreciated
Please see xargs. For example:
myapp | grep '|' | sed -e 's/^[^|]*//' | sed -e 's/|.*//' | xargs -n 1 myscript.sh
May be this can help -
myapp | awk -F"|" '{ print $2 }' | while read -r line; do /path/to/script/ "$line"; done
I like the xargs -n 1 solution from Dark Falcon, and while read is a classical tool for such kind of things, but just for completeness:
myapp | awk -F'|' '{print "myscript.sh", $2}' | bash
As a side note, speaking about extraction of 2nd field, you could use cut:
myapp | cut -d'|' -f 1 # -f 1 => second field, starting from 0

Resources