![](/style/images/good.png)
![](/style/images/bad.png)
Furry File Finagling in Linux
source link: https://mikesmithers.wordpress.com/2022/06/06/furry-file-finagling-in-linux/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Furry File Finagling in Linux
The UK this week, it’s been all about The Queen. Being a cat, Cleo naturally assumes that the Queen in question is her.
Cleo’s interest in what follows is less lending a helping paw and more being curious as to what her minions are up to. That and the fact that she’ll do anything to get her paws on the cat treat known as Dreamies.
Mind you, she does have something of a technical background :
Sat on the Mat is such a cliche !
From my perspective, today’s “that’s handy, I really must write that down” instalment covers some useful Linux commands for managing files, together with some surprising date formats.
What was that Your Majesty ? Carry On, Cleo ?
Watch
You may well be familiar with the follow option of tail, which allows you to read lines as they are being added to a file, but what if you want to see the changes to a file’s properties, or even, wait for the file to turn up in the first place…
watch -n5 ls -l swish.log |
This will run the specified command every n seconds. We can see the output change once the file is created…
Using the g switch, we can exit watch once the output of the command changes. This means that we can write a script like this to follow the file once it arrives (called wait_for_file.sh) :
#!/bin/sh watch -n5 -g ls swish.log tail -f swish.log |
To demonstrate, here’s the script to write to swish.log :
#!/bin/sh for i in {1..10} do treat=$([ "$i" == 1 ] && echo 'dreamy' || echo 'dreamies' ) msg= "$i lovely $treat waiting just for me" echo $msg >>swish.log sleep 1 done |
If I start wait_for_file.sh it will repeat until I start the write_log.sh script in another session.
Relative Date String Items
To satisfy Cleo’s dreamy craving, we could simply create the file …
touch dreamies.txt
But what if we want to create a file with a different timestamp.
Cleo has received enough tribute from her subjects today, but she’ll require more tomorrow. Fortunately, GNU’s date formatting options make this rather simple :
date -d now
Fri 3 Jun 21:22:57 BST 2022
touch -d tomorrow dreamies.txt
ls -l dreamies.txt
-rw-rw-r-- 1 mike mike 0 Jun 4 2022 dreamies.txt
It’s worth exploring the date formats that you can specify as they go well beyond the traditional Day/Month/Year options.
To test what’s possible we can use the -d option of date…
date -d now
Fri 3 Jun 21:44:18 BST 2022
date -d yesterday
Thu 2 Jun 21:44:23 BST 2022
date -d tomorrow
Sat 4 Jun 21:44:29 BST 2022
date -d fortnight
Fri 17 Jun 15:41:23 BST 2022
date -d "next monday"
Mon 6 Jun 00:00:00 BST 2022
date -d "next month"
Sun 3 Jul 21:44:49 BST 2022
date -d "last year"
Thu 3 Jun 21:44:59 BST 2021
date -d "2 June 1952"
Mon 2 Jun 00:00:00 BST 1952
date -d "10 years ago"
Sun 3 Jun 21:45:38 BST 2012
Things get even more interesting when you combine these strings…
date -d "last tuesday 2 weeks ago"
Tue 17 May 00:00:00 BST 2022
date -d "last saturday next week"
Sat 4 Jun 00:00:00 BST 2022
…although it is possible to get carried away…
date -d "first monday next month"
Wed 6 Jul 00:00:00 BST 2022
As a loyal subject, one of my duties is to tidy up after Cleo.
In this instance, I need to cleardown any files that are at least 7 days old.
In order to create some test data, I can simply pass a date to touch to create files with a selection of timestamps, some of which should remain after running our script.
To create the files :
touch dreamies.txt more_dreamies.txt even_more_dreamies.txt
touch -d "last month" old_dreamies.txt
touch -d "last week" fairly_old_dreamies.txt
ls -lt *dreamies.txt
-rw-rw-r-- 1 mike mike 0 Jun 4 15:52 dreamies.txt
-rw-rw-r-- 1 mike mike 0 Jun 4 15:52 even_more_dreamies.txt
-rw-rw-r-- 1 mike mike 0 May 28 15:52 fairly_old_dreamies.txt
-rw-rw-r-- 1 mike mike 0 Jun 4 15:52 more_dreamies.txt
-rw-rw-r-- 1 mike mike 0 May 4 15:52 old_dreamies.txt
To identify the files that are at least 7 days old, I could run :
find *.txt -type f -mtime +6
…or simply use a relative date…
find *.txt ! -newermt "last week"
…which means that my cleardown script (cleardown.sh) looks like this :
#!/bin/sh find *.txt ! -newermt "last week" | xargs rm |
If I now run this, the older files will be deleted but the others will remain untouched :
. ./cleardown.sh
ls -lt *dreamies.txt
-rw-rw-r-- 1 mike mike 0 Jun 4 15:52 dreamies.txt
-rw-rw-r-- 1 mike mike 0 Jun 4 15:52 even_more_dreamies.txt
-rw-rw-r-- 1 mike mike 0 Jun 4 15:52 more_dreamies.txt
If you want to find more information on relative items in dates, the GNU manual is well worth a read.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK