Some examples of this command from Linux Debian and FreeBSD Here is how to do this with GNU Linux – %p gives full path name user1@f1-google-joniowa:~$ find . -name “weather_obs.log” -printf “%p\t%TY-%Tm-%Td\n” ./test27/weather_obs/weather_obs.log 2023-05-16 ./test26/weather_obs/weather_obs.log 2022-12-08 ./test28/weather_obs/weather_obs.log 2023-02-28 ./test25/weather_obs/weather_obs.log 2022-08-18 ./python/weather_obs/weather_obs.log 2021-05-24 ./test24/weather_obs/weather_obs.log 2022-11-22 Here is a bsd version – uses exec [user1@freebsd12_3 ~]$ find . -name […]
Find command – listing last modify path with full dir
finding files and using tar for backup.
The most common solution is to use find with xargs to backup Something like this: find /var/www/html/ -type f -name my*.csv -print0 | xargs -0 tar cfvzP /backupdir/backupfile The more files you have, especially over 1000 – this will produce weird results. Number of files in the archive doesn’t match the output of the find xargs will split the input […]
Use sed command to change column names in csv files
Problem: – You have several CSV file that have a title that needs to change. In my example I had a column name that was need to change from “Time(edt)” to just “Time” To run a test: note: no special backslash needed for “(” or “)” You can glob the files and it will do all of them. sed ‘s/Time(edt)/Time/g’ […]
Bash script to check SSL expiration
Script you can run in a cron Several thing of note: when getting the SSL information – you have to echo a “q” because the openssl is interactive. To covert from timestamp to formatted date – use the @ symbol date -d @$timestamp_item ‘+%Y-%m-%d’ #!/bin/bash TARGET=”www.website.net”; RECIPIENT=”admin@websit.net”; DAYS=7; echo “checking if $TARGET expires in less than $DAYS days”; ssl_out=$(echo […]
Bash array operations – short table.
arr=() Create an empty array arr=(1 2 3) Initialize array ${arr[2]} Retrieve third element ${arr[@]} Retrieve all elements ${!arr[@]} Retrieve array indices ${#arr[@]} Calculate array size arr[0]=3 Overwrite 1st element arr+=(4) Append value(s) str=$(ls) Save ls output as a string arr=( $(ls) ) Save ls output as an array of files ${arr[@]:s:n} Retrieve n elements starting at index s
Bash – using mailx command to parse subjects
Problem: It appears that the default mail -H appears to truncate the subject. Grep fails to find text pass the email address. Fix: Use Headline to format echo ‘f 1-$’ | mail -H -S headline=”%>%a%m %20f %16d %3l/%-5o %i%150S” | grep -i ANZ535 | wc -l Here is what the issue looks like [ec2-user@jibsheet data]$ echo ‘f 1-$’ | […]
Using awk with ‘ls -l’ linux command
Some creative ways to use awk with ls -l command. Find all files for a certain month: ls -l | awk ‘$6==”Sep”{print}’ Find all files with a size greater than: ls -l | awk ‘$5>1009{print}’ [user1@jibsheet data]$ ls -l | awk ‘$6==”Sep”{print}’ -rw-rw-r– 1 user1 datausr 54255 Sep 9 12:34 alex_Y2021_M09_D09_H12.txt -rw-r–r– 1 user1 datausr 54255 Sep 17 01:33 alex_Y2021_M09_D17_H01.txt […]
ls -l – sort files by number in file name or extention
Use sort. Use -n for numeric and -t for field separator. then k for the field. [user1@jibsheet ~]$ ll $HOME/tidal* -rw-rw-r– 1 user1 user1 545 Oct 13 10:02 /home/user1/tidal_out.txt -rw-rw-r– 1 user1 user1 545 Oct 13 10:02 /home/user1/tidal_out.txt.1 -rw-rw-r– 1 user1 user1 564 Oct 12 22:39 /home/user1/tidal_out.txt.10 -rw-rw-r– 1 user1 user1 546 Oct 13 08:02 /home/user1/tidal_out.txt.2 -rw-rw-r– 1 user1 […]
How to check for bad filename or path in BASH
So, I am reading about injection attacks. White Listing is better than Black Listing. Bash support some regex. The first IF – check for a valid file name. 2nd checks for a Unix path name and allows spaces. But not “&, >, <, or | “. Ampersand in Linux is really bad. if you allow that in certain user inputs […]