I broke the FIND command of Ubuntu, mtime not working as it should - find

I'm building a script for backup and I'm heavily using the FIND with -mtime.
Yesterday I used find -mtime +1 a lot, to search the file modified more than a day ago.
At the end of the day, the command I used for the whole day stopped working.
user#ubuntu-4:~$ mkdir test
user#ubuntu-4:~$ cd test/
user#ubuntu-4:~/test$ touch -t 201601180830 yesterdayMorning
user#ubuntu-4:~/test$ touch -t 201601181725 yesterdayAfternoon
user#ubuntu-4:~/test$ ll
total 32
drwxrwxr-x 2 user user 4096 Jan 19 09:37 ./
drwx------ 9 user user 12288 Jan 19 09:36 ../
-rw-rw-r-- 1 user user 0 Jan 18 17:25 yesterdayAfternoon
-rw-rw-r-- 1 user user 0 Jan 18 08:30 yesterdayMorning
The result of FIND -mtime n
user#ubuntu-4:~/test$ find -mtime +1
user#ubuntu-4:~/test$ find -mtime -1
.
./yesterdayAfternoon
user#ubuntu-4:~/test$ find -mtime 0
.
./yesterdayAfternoon
user#ubuntu-4:~/test$
I should be able to find the file named yesterdayMorning because at the time I'm writing (09:48 am of 19 january) that file is older than 1 day.
find -mtime -1 (or 0 too) show the correct result because the file's last modification is less than 24 hours.
And yesterday before 05.00 pm I swear it was working!

It's actually not 24 hours ago but more than n days ago. I.e. for -mtime +1 it would have to be modified two days ago.
Use find -mtime +0 to match also yesterday's files.

As stated in the accepted answer -mtime +0 will work for you in this case.
Note:
find using -mtime and -daystart
-mtime n
File's data was last modified n*24 hours ago.
-daystart
Measure times (for -amin, -atime, -cmin, -ctime, -mmin, and
-mtime) from the beginning of today rather than from 24 hours
ago.
This option only affects tests which appear later on the
command line.
date
Tue Jan 19 10:24:43 CET 2016
~/test $ ls -n
total 0
-rw-r--r-- 1 1000 1000 0 Jan 18 10:15 yesterdayMorning10:15.txt
-rw-r--r-- 1 1000 1000 0 Jan 18 10:45 yesterdayMorning10:45.txt
~/test $ find -mtime +0
./yesterdayMorning10:15.txt
~/test $ find -mtime 0
./yesterdayMorning10:45.txt
~/test $ find -daystart -mtime +0
./yesterdayMorning10:15.txt
./yesterdayMorning10:45.txt

Related

the rules of find -cmin 0 and -ctime 0

I was studying the command find, and I saw:
-cmin n
File's status was last changed n minutes ago.
-ctime n
File's status was last changed n*24 hours ago.
but when n is zero, comes the difference, is there a sound explanation for this behavior?
$ date
Thu 23 Jul 2020 02:16:17 PM CST
$
$ touch a
$ find -name a -cmin 0
$ find -name a -cmin 1
./a
$ find -name a -ctime 0
./a
$ find -name a -ctime 1
$
$ date
Thu 23 Jul 2020 02:16:18 PM CST
$

command line; delete static file in a folders who begins with certain character

I have hundreds of folders like
170102texta
170204textb
180306textc
180408textd
these folders and many subfolder is the file test.txt present.
I would like to to delete all the "test.txt" files but only in the folders who begins with "17.."
every advice is appreciated :)
you just have to run rm -rf {pattern}*
C02XF0N6JG5M:test kkadam$ ls -lrt
total 0
drwxr-xr-x 2 kkadam staff 64 Feb 13 21:00 11test
drwxr-xr-x 2 kkadam staff 64 Feb 13 21:00 11b
drwxr-xr-x 2 kkadam staff 64 Feb 13 21:00 11c
C02XF0N6JG5M:test kkadam$ rm -rf 11*
C02XF0N6JG5M:test kkadam$ ls -lrt
total 0

Alias directory minus one file

I'd like to make an alias for a directory that contains many files. If I open the aliased directory, I want to view all files except for one. New files will be added to the directory over time, and I want everything to be aliased except for the one file permanently not-aliased.
Is it possible to set this up without needing to run a script every time a file in the directory changes?
E.g.: myDir contains fileA, fileB, and fileC.
I want myAliasedDir to contain fileA and fileB, but not fileC.
Can you get away with symbolic links?
Assuming your shell is bash:
$ shopt -s extglob
$ mkdir dir1 dir2
$ cd dir1
$ touch file{1..3}
$ ls -l
total 0
-rw-r--r-- 1 jackman jackman 0 Apr 1 12:40 file1
-rw-r--r-- 1 jackman jackman 0 Apr 1 12:40 file2
-rw-r--r-- 1 jackman jackman 0 Apr 1 12:40 file3
$ cd ../dir2
$ for f in ../dir1/!(file2); do ln -s "$f"; done
$ ls -l
total 8
lrwxrwxrwx 1 jackman jackman 13 Apr 1 12:41 file1 -> ../dir1/file1
lrwxrwxrwx 1 jackman jackman 13 Apr 1 12:41 file3 -> ../dir1/file3

Need to remove files with spaces in Debian

I need to find and remove files with spaces in them in a certain folder.
$ ls -l
total 16
-rw-r--r-- 1 smw staff 10 Feb 6 16:10 Foo Bar
-rw-r--r-- 1 smw staff 11 Feb 6 16:10 foobar
$ ls -l *\ *
-rw-r--r-- 1 smw staff 10 Feb 6 16:10 Foo Bar
$ rm -i *\ *
remove Foo Bar? y
$ ls -l
total 16
-rw-r--r-- 1 smw staff 11 Feb 6 16:10 foobar
You'll have to deal with bash's niceties when dealing with spaces...
First, you need to iterate over the files, in a way that gives you the files properly regardless of spaces. Check out this question. I'd favor this:
find ... | while read line ; do command "$line" ; done
And then it's a matter of using something like sed to change $line into whatever you need (such as the same thing without spaces) right where command "$line" is.
This is how I removed a file with a space
pi#raspberrypi ~/Music $ ls -l
-rw-r--r-- 1 pi pi 0 Feb 25 16:05 Sleep Away.mp3
pi#raspberrypi ~/Music $ rm Sleep\ Away.mp3
use the "\" forward slash to escape any spaces

Convert `ls -lR` output to format of `find` output

I need to convert ls -lR output to the format of find output
E.g. I have a text file, which is an output of ls -lR. The file contains:
/tmp/1:
total 0
drwxr-xr-x 3 user1 ubuntu 80 May 10 21:13 2
-rw-r--r-- 1 user1 ubuntu 0 May 10 21:13 f1
/tmp/1/2:
total 0
drwxr-xr-x 2 user1 ubuntu 60 May 10 21:13 3
-rw-r--r-- 1 user1 ubuntu 0 May 10 21:13 f2
/tmp/1/2/3:
total 0
-rw-r--r-- 1 user1 ubuntu 0 May 10 21:13 f3
I want to convert this file to the second one with different format, just like find utility uses by default:
/tmp/1
/tmp/1/f1
/tmp/1/2
/tmp/1/2/f2
/tmp/1/2/3
/tmp/1/2/3/f3
If I have the fs, which was used for generating ls-lR, I'll just run a find /tmp/1, but in my case I have no access to the original fs.
Is it possible? There must be a short perl-script for this conversion.
You can use -ls as primary (which is slightly different from ls -l) or -exec ls -l {} +. It will however never be completely the same as ls -lR since that does different groupings and prints totals.
Actually, I think find . -type d -exec ls -l {} + is the closest approximation.