Thursday, June 29, 2023
HomeNetworkingDiscovering information on Linux in all types of how

Discovering information on Linux in all types of how


The Linux discover command can find information based mostly on nearly any standards that you simply may want. This submit describes the various standards you should utilize to search out what you’re in search of – even when you may’t bear in mind what you named a file or while you final modified it or added content material.

Primary discover syntax

The fundamental syntax for the discover command seems to be like this:

$ discover [starting location] [criteria] [options] [action to take]

The beginning location is usually a listing identify (e.g., /var/log), the present listing (.), your private home listing whether or not you’re sitting in it or not (~), or a listing relative to your present place (e.g., ./bin). You could be as particular as you need when getting into the beginning location.

Discovering information by identify

Looking for information by identify is the simplest and most simple approach to discover a file. Word the command under makes use of an asterisk so that it’ll discover any information that begin with “repair”.

$ discover ~ -name "repair*" -print
/residence/shs/fixit

Discovering information by sort

You may as well discover information which are of a selected sort (e.g., a file, listing or symbolic hyperlink)  To discover a file, use “-type f”. 

$ discover Paperwork -name junk* -type f -print
Paperwork/junk.odt
Paperwork/junk.docx

To discover a symbolic hyperlink, use “-type l”.

$ discover . -name "h*" -type l -ls
    28603      0 lrwxrwxrwx   1 shs      shs             9 Jun 27 12:27 ./maintain -> /tmp/hold4me

To discover a listing, use “-type d”.

$ discover . -type d -ls | head -3
     3389      4 drwxr-x---  25 shs      shs          4096 Jun 27 14:24 .
     3467      4 drwxr-xr-x   2 shs      shs          4096 Could 20  2021 ./Footage
     3468      4 drwxr-xr-x   2 shs      shs          4096 Could  7  2021 ./Movies

Figuring out what you need to see or do

You in all probability seen within the two earlier instructions which you could listing (-ls means a protracted itemizing) or just show the identify of a file (-print). These aren’t the one choices.

To delete a file, you’ll want to add the -exec rm command as proven under. This command will take away any file with a “.previous” extension from your private home listing (together with subdirectories).

$ discover ~ -name *.previous -exec rm {} ;

The exec command may even will let you view the contents of a file. Right here’s an instance:

$ discover . -name "h*" -type l -exec cat {} ;
Meow, Meow!

Discovering information by proprietor and/or group

To seek out information by consumer, embrace the “-user uname” specification. You’ll be able to specify the username or the consumer ID. The third instance under ship output that generates an error (e.g., permission denied) to /dev/null in order that it would not muddle up the display screen.

$ discover . -user shs -ls | head -4
     3389      4 drwxr-x---  25 shs      shs          4096 Jun 27 13:18 .
     5617      4 -rw-rw-r--   1 shs      shs            22 Feb 15  2021 ./CHECKME
     8001      4 -rwx------   1 shs      shs           150 Feb 25  2021 ./send_msg
    12727     24 -rw-rw-r--   1 shs      shs         20805 Apr 15  2021 ./history-rece
$ discover . -user 1000 -ls | head -4
     3389      4 drwxr-x---  25 shs      shs          4096 Jun 27 13:18 .
     5617      4 -rw-rw-r--   1 shs      shs            22 Feb 15  2021 ./CHECKME
     8001      4 -rwx------   1 shs      shs           150 Feb 25  2021 ./send_msg
    12727     24 -rw-rw-r--   1 shs      shs         20805 Apr 15  2021 ./history-recent
$ discover /tmp -user shs -ls 2> /dev/null
 26476575      4 -rw-rw-r--   1 shs      shs        10 Jun 27 12:44 /tmp/haha
 26476577      4 drwx------   2 shs      shs      4096 Jun 27 12:09 /tmp/tree
 26476585      0 srwxrwxrwx   1 shs      shs         0 Jun 27 12:09 /tmp/dbus
 26476595      4 -r--r--r--   1 shs      shs        11 Jun 27 12:09 /tmp/.X1-lock

Relying on file permissions, you may have the ability to discover information belonging to different customers as properly.

$ discover /residence -group tom -print
/residence/tom
/residence/tom/report
/residence/tom/myfile

To seek out information by group, use the “-group gname” specification. Group IDs can be utilized instead of group names.

$ discover /tmp -group 1000 -ls 2>/dev/null
 26476575      4 -rw-rw-r--   1 shs      shs            10 Jun 27 12:44 /tmp/haha
 26476577      4 drwx------   2 shs      shs          4096 Jun 27 12:09 /tmp/tracker-extract-files.1000
 26476585      0 srwxrwxrwx   1 shs      shs             0 Jun 27 12:09 /tmp/dbus-HpFApADlMO
 26476595      4 -r--r--r--   1 shs      shs            11 Jun 27 12:09 /tmp/.X1-lock
 26476580      0 srwxrwxrwx   1 shs      shs             0 Jun 27 12:09 /tmp/.ICE-unix/2051
 26476596      0 srwxrwxr-x   1 shs      shs             0 Jun 27 12:09 /tmp/.X11-unix/X1
 26476594      0 srwxrwxr-x   1 shs      shs             0 Jun 27 12:09 /tmp/.X11-unix/X0
 26476586      4 -r--r--r--   1 shs      shs            11 Jun 27 12:09 /tmp/.X0-lock

Discovering information by file permissions

To seek out information which have group write permissions, you would use a command like this one:

$ discover /usr/bin -name "web*" -perm -g=w -ls
    26666  0 lrwxrwxrwx   1 root  root   24 Feb 14  2021 /usr/bin/netcat -> /and so forth/alternate options/netcat

Discover that the start of the file identify is included or there can be extra information listed.

You may as well search for information by file permissions as within the following instance that appears for information that present all entry (777):

$ discover /usr/bin -name "d*" -perm 777 -ls | head -3
    34026      0 lrwxrwxrwx   1 root     root            9 Mar 12  2021 /usr/bin/dvipdfmx -> xdvipdfmx
    29934      0 lrwxrwxrwx   1 root     root           18 Jan 29  2021 /usr/bin/distro-info -> ubuntu-distro-info
    33159      0 lrwxrwxrwx   1 root     root            6 Feb 17  2021 /usr/bin/dvilualatex -> luatex

Discovering information by age

To discover a file by age, use the -mtime possibility and specify the variety of days for the reason that file was modified utilizing a quantity like +100 (greater than 100 days previous) or -10 (modified inside the final 10 days).

$ discover Paperwork -mtime -1
Paperwork
Paperwork/junk.doc

Discovering information by dimension

To seek out empty information, use a command like this one:

$ discover . -size 0 -ls | head -1
    28959      0 -rw-rw-r--   1 shs      shs             0 Jun 27 13:18 ./.abc

With out the addition of the head command on this instance, this command would discover much more empty information – representing the cache and such.

$ discover . -size 0 -ls | wc -l
45

The subsequent command finds information which are bigger than 1 GB: Discover that the command sends all of the “permission denied” messages to the /dev/null.

$ discover / -type f -size +1G -ls 2>/dev/null
  12 2097156 -rw-------  1 root  root  2147483648 Feb 14  2021 /swapfile
  18 8388612 -rw-------  1 root  root  8589934592 Sep  9  2021 /mint/swapfile/40265

Earlier posts on the discover command

A few of my earlier posts on the discover command are listed under:

https://www.networkworld.com/article/3527420/how-to-find-what-you-re-looking-for-on-linux-with-find.html
https://www.networkworld.com/article/3233305/using-the-linux-find-command-with-caution.html

Wrap-up

The discover command has plenty of choices that you should utilize to slim down your search if you end up in search of particular information in your Linux system.

Copyright © 2023 IDG Communications, Inc.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments