SHELL: Display oldest created files DATE and its path - bash

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

Related

Count erros messages in multiple files per day

So i have a log file that display err|error messages that i wanna count every day
cat user.log | grep 'err|error' | wc -l
gimme almos all that i need whoever there are other log files that are zip so
zcat user.log.* | grep 'err|error' | wc -l
also almost there
so here is where im stuck, i need to check every log.zip file check if has any error message of today date in multiple files and also on user.log that is my current file collecting the errors
so i need to check over 50 user.log.Z files count all line that has today date
Oct 8 00:00:00 until 23:59:59
and my user.log
gratz in advance
EDIT-------------
solverd with
DATE=`date "+%b %e"` ;find /var/adm/ ! -path /var/adm/ -prune -name "user.log*" -prune -mtime -1 -exec zgrep "$DATE" {} \; |grep "user:err|error" |wc -l
If the date is part of the filename, append it (whereever it's added by logrotate):
zcat -f user.log.* user.log-$(date +%Y-%m-%d).* | grep 'err|error' | wc -l
zcat -f user.log user.log.$(date +%Y-%m-%d).* | grep 'err|error' | wc -l
grep can also count, zgrep can also process compressed files, so a bit shorter:
zgrep -c 'err|error' user.log user.log.$(date +%Y-%m-%d)*
If the date is not part of the filename, you need to process all files and filter out all lines with a different date:
zgrep $(date +%Y-%m-%d) file ... | grep -c 'err|error'
zgrep $(date "+%b %d") file ... | grep -c 'err|error'
Or using awk:
# Assumes the date is in the first field, like "2018-10-09 ... err|error ..."
zcat ... | awk 'BEGIN{d=strftime("%Y-%m-%d")}/err\|error/&&$01==d{c++}END{print c}'

grep search with filename as parameter

I'm working on a shell script.
OUT=$1
here, the OUT variable is my filename.
I'm using grep search as follows:
l=`grep "$pattern " -A 15 $OUT | grep -w $i | awk '{print $8}'|tail -1 | tr '\n' ','`
The issue is that the filename parameter I must pass is test.log.However, I have the folder structure :
test.log
test.log.001
test.log.002
I would ideally like to pass the filename as test.log and would like it to search it in all log files.I know the usual way to do is by using test.log.* in command line, but I'm facing difficulty replicating the same in shell script.
My efforts:
var-$'.*'
l=`grep "$pattern " -A 15 $OUT$var | grep -w $i | awk '{print $8}'|tail -1 | tr '\n' ','`
However, I did not get the desired result.
Hopefully this will get you closer:
#!/bin/bash
for f in "${1}*"; do
grep "$pattern" -A15 "$f"
done | grep -w $i | awk 'END{print $8}'

Getting file size in bytes with bash (Ubuntu)

Hi, i'm looking for a way to output a filesize in bytes. Whatever i try i will get either 96 or 96k instead of 96000.
if [[ -d $1 ]]; then
largestN=$(find $1 -depth -type f | tr '\n' '\0' | du -s --files0-from=- | sort | tail -n 1 | awk '{print $2}')
largestS=$(find $1 -depth -type f | tr '\n' '\0' | du -h --files0-from=- | sort | tail -n 1 | awk '{print $1}')
echo "The largest file is $largestN which is $largestS bytes."
else
echo "$1 is not a directory..."
fi
This prints "The largest file [file] is 96k bytes"
there is -b option for this
$ du -b ...
Looks like you're trying to find the largest file in a given directory. It's more efficient (and shorter) to let find do the heavy lifting for you:
find $1 -type f -printf '%s %p\n' | sort -n | tail -n1
Here, %s expands to the size in bytes of the file, and %p expands to the name of the file.

sort list of files by date in bash

Given a text file containing some list of files, e.g.
$ cat file_list.txt
/var/x/file1.txt
/var/y/file2.txt
<etc>
How can I sort this list of files by some criteria - like their last accessed time, or last changed time?
Thanks in advance.
You can use stat command with sort like this:
while read -r line; do
stat -c '%Y %n' "$line"
done < file_list.txt | sort -n | cut -d ' ' -f2
stat -c '%Y %n' lists time of last modification, seconds since Epoch followed by a space and file name
sort -n sorts timestamps and their filename numerically
cut -d ' ' -f2 prints only file names from sort's output
Try one liner (by modification time):
ls -t $(cat file_list.txt)
OR
ls -t `cat file_list.txt`
You can get the most recently changed file with
cat file_list.txt | xargs stat -c '%Y %n' | sort | tail -1 | cut -c 12-
You can get the most recent timestamp with
cat file_list.txt | xargs stat -c '%Y %n' | sort | tail -1 | cut -c -10

Need help in ls -ltr linux 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

Resources