Score:0

UBUNTU commands to find file in a folder

cf flag

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:

  1. Why did Method 2 only result in just 7 distinct files? There are 7 other files, with size 0KB, that are omitted.
  2. 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" ?
  3. 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)

Raffa avatar
jp flag
The `.` will translate to the current working directory … So you must not use it in method 3 and you don’t need to use it in method 2 … What do you mean by duplicates? Are there subdirectories in the search directory ? … Are you sure the release of Ubuntu is 12.04 ?!
padjee avatar
cf flag
Hi Raffa. I updated the question. Please kindly read the update. @Raffa
guiverc avatar
cn flag
Only supported releases of Ubuntu (*standard support*) are on-topic for this site. Ubuntu 12.04 LTS is EOL thus off-topic, and Ubuntu 12.04 ESM was in extended support and only supported by Canonical via Ubuntu Advantage thus also off-topic here. Refer https://askubuntu.com/help/on-topic https://help.ubuntu.com/community/EOLUpgrades https://fridge.ubuntu.com/2017/03/15/ubuntu-12-04-precise-pangolin-reaches-end-of-life-on-april-28-2017/
padjee avatar
cf flag
isn't it a general question, applied to all Ubuntu regardless of the version? @guiverc
Score:6
cn flag

The problem is :

find . /backup-nfs/

You ask to search in . (the current directory) and also in /backup-nfs. If you want to search only in /backup-nfs just say it, no need to add the current directory.

find /backup-nfs

Answers:

  1. No idea. You probably did not look right.
  2. because you explicitly ask to search the current directory (.) that happens to be USER's home and /backup-nfs.
  3. find /backup-nfs -iname "*.sql" -print0 | xargs -0 rm
    or as @terdon suggests in a comment, use the -delete action directly
    find /backup-nfs -iname "*.sql" -delete

Also note that you claim that ls - lah gives the correct result, this would mean that you have a folder named - and a folder named lah inside the folder /backup-nfs that together hold 14 files named "*sql". I seriously doubt that.

padjee avatar
cf flag
isn't `-lah` an option for `ls` ? Here : https://www.rapidtables.com/code/linux/ls.html @exore
cn flag
`-lah` **is** an option. but "`-`space`lah`" are 2 arguments that are not options, thus regular arguments.
padjee avatar
cf flag
Yes..sorry for the typo. I just realize.. thanks @exore
terdon avatar
cn flag
No need for `xargs`, you can just do `find /backup-nfs -iname '*.sql' -delete`.
cn flag
Oh. great. I still use the old idioms I learnt years ago. this `-delete` action is even better. Thanks @terdon.
terdon avatar
cn flag
Yeah, the `-delete` is a GNU `find` thing, if you're coming from the UNIX world it won't be familiar.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.