I want to find files in a certain folder with certain extension, lets say finding ".sql" in "/backup-nfs". Later on, I will use this command on crontab to delete some files automatically.
I use 3 different methods and they give out different results :
Method 1 :
- login as A USER.
- go to /backup-nfs
- type in
ls -lah
- Results are 14 files. Of course, this is correct.
Method 2 :
- login as A USER.
- go to /backup-nfs
- type in :
find . /backup-nfs/ -name "*.sql" -type f -ls
- Results are 14 files (7 of them are duplicates, so essentially only resulted 7 files). Eg : file A, file B, ... file G, /backup-nfs/file A, /backup-nfs/file B,... /backup-nfs/file G
Method 3 :
- login as A USER
- go to root of that user
- type in :
find . /backup-nfs/ -name "*.sql" -type f -ls
- Results are too many files. It searches the whole directory, not just /backup-nfs !
Question:
- Why did Method 2 only result in just 7 distinct files? There are 7 other files, with size 0KB, that are omitted.
- Why does Method 3 search the /backup-nfs and also the entire directory outside "/backup-nfs" even though I have specified to search to only "
/backup-nfs
" ?
- What's the correct method? I do not want to delete other files that do not meet the criteria.
I tried on Ubuntu 12.04 (server) and 20.04 (non server)