Friday, 2 August 2019

find Command

find command is used for searching the files and directories details on the server.
Some of the basic options used with the find command are
-name
This option uses to search a particular file with that file name.
Syntax:
find –name filename

Example:
Finding a particular file in a directory based on the name.
Command output….

-type
This option is used to search the file with the type of that file. Whether that file is ASCII text file, Directory, Block special file, character special file etc.
Syntax:
find –type file-name

 -size
This option is used to search the file with the size of the file.
Syntax:
find –size file-size


-atime
This option is used to search the file using the access time of the file.
Syntax:
find –atime file-size 


-mtime
This option is used to search the file using the modified time of the file.
Syntax:
find –mtime file-size

-nouser
This option is used to search the files which are not owned by any other user.
 Syntax:
find –nouser file-name

-exec
This option is used to execute the other commands in the find command.

-user
This option is used to find the files owned by a particular user.
Syntax:
find – user filename

For more options see the man page of the find command.

Some basic tasks on the usage of find command

 Write a command to search for the file 'abc' in the current directory?
[root@sys2 ~]# find -name abc -type f
./Downloads/abc
[root@sys2 ~]# pwd
/root


 Write a command to search for the file 'sda' in '/dev' directory?
Syntax
find /dev -name sda -type b

In the above command ‘b’ represents the block special file.

Output
[root@sys2 ~]# find /dev -name sda -type b
/dev/sda


• Write a command to search for zero byte size files in the current directory?
Syntax
find -size 0 -type f

Output 
[root@sys2 ~]# find -size 0 -type f
./ithonours2
./.gconf/apps/gnome-session/%gconf.xml
./.gconf/apps/nautilus/%gconf.xml
./.gconf/apps/nautilus/desktop-metadata/%gconf.xml


• Write a command to list the files that are accessed 2 days ago in the current directory?
Syntax
find -atime 2 -type f

Output
[root@sys2 ~]# find -atime 2 -type f
./.mozilla/firefox/profiles.ini
./.mozilla/firefox/21mz7ghr.default/startupCache/startupCache.8.little
./.mozilla/firefox/21mz7ghr.default/urlclassifier3.sqlite
./.mozilla/firefox/21mz7ghr.default/prefs.js

• Write a command to list the files that were modified 4 days ago in the current directory?
Syntax
find -mtime 4 -type f

Output
[root@sys2 ~]# find -mtime 4 -type f
./.viminfo
./.mozilla/firefox/21mz7ghr.default/startupCache/startupCache.8.little
./.mozilla/firefox/21mz7ghr.default/formhistory.sqlite
./.mozilla/firefox/21mz7ghr.default/bookmarkbackups/bookmarks-2016-03-13.json


• Write a command to search for the files in the current directory which are not owned by any user in the /etc/passwd file?
Syntax
find . -nouser -type f

Output
[root@sys2 /]# find -nouser -type f
./home/john/.bash_logout
./home/john/.bash_history
./home/john/.viminfo
./home/john/.bash_profile
./home/john/.bashrc
./var/spool/mail/john


• Write a command to search for the files in '/root' directory that start with 'i'?
Syntax
find /root -name 'i*' -type f

Output
[root@sys2 ~]# find /root -name 'i*' -type f
/root/ithonors
/root/install.log
/root/ithonours2
/root/ithonours3
/root/ithhard
/root/ithonors2
/root/install.log.syslog


• Write a command to search for the files that start with 'i' in the /root directory and then display the contents of the file?
Syntax
find /root -name 'i*' -type f -exec cat {} \;

Output
[root@sys2 ~]# find /root -name 'i*' -type f -exec cat {} \;
HI
how are you
Installing libgcc-4.4.7-3.el6.x86_64
warning: libgcc-4.4.7-3.el6.x86_64: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Installing fontpackages-filesystem-1.41-1.1.el6.noarch
Installing m17n-db-1.5.5-1.1.el6.noarch
Installing ca-certificates-2010.63-3.el6_1.5.noarch
Installing setup-2.8.14-20.el6.noarch
Installing liberation-fonts-common-1.05.1.20090721-5.el6.noarch
Installing xkeyboard-config-2.6-6.el6.noarch
Installing xml-common-0.6.3-32.el6.noarch
Installing iso-codes-3.16-2.el6.noarch
Installing filesystem-2.4.30-3.el6.x86_64
Installing dejavu-fonts-common-2.30-2.el6.noarch
Installing tzdata-2012j-1.el6.noarch
………………………………..
……………………………………………..
…………………………………………………………..


• Write a command to list the files whose status is changed 1 days ago in the current directory?
Syntax
find -ctime 1 -type f

Output
[root@sys2 ~]# find -ctime 1 -type f
./.gconfd/saved_state
./.pulse/26e2d5055a12c7096abf9b180000002c-default-sink
./.pulse/26e2d5055a12c7096abf9b180000002c-default-source
./.gtk-bookmarks
./.gconf/apps/gnome-session/options/%gconf.xml


• Write a command to list the files in '/root' directory that start with 'i' and then display the number of lines in each file?
Syntax
find /root -name 'i*' -type f -exec wc -l {} \;

Output
[root@sys2 ~]# find /root -name 'i*' -type f -exec wc -l {} \;
2 /root/ithonors
1111 /root/install.log
0 /root/ithonours2
0 /root/ithonours3
2 /root/ithhard
2 /root/ithonors2
298 /root/install.log.syslog

0 comments:

Post a Comment