Skip to content

Posts tagged ‘common linux commands’


Locating files using the find command with 15 examples

Find is a versatile tool which can be used to locate files and directories satisfying different user criteria. But the sheer number of options for this command line tool makes it at the same time both powerful and encumbering for the user. Here I will list a few combinations which one can use to get useful results using find command.

Find all HTML files starting with letter ‘a‘ in your current directory (Case sensitive)

$ find . -name a\*.html

Same as above but case insensitive search.

$ find . -iname a\*.html

Find files which are larger than 5 MB in size.

$ find . -size +5000k -type f

Here the ‘+’ in ‘+5000k’ indicates greater than and k is kilobytes. And the dot ‘.’ indicates the current directory. The -type option can take any of the following values:

    f - file
    d - directory
    l - symbolic link
    c - character
    p - named pipe (FIFO)
    s - socket
    b - block device

Find all empty files in your directory

$ find . -size 0c -type f

… Which is all files with 0 bytes size. The option -size can take the following:

    c - bytes
    w - 2 byte words
    k - kilo bytes
    b - 512 byte blocks

Note: The above command can also take the -empty parameter.

Find is very powerful in that you can combine it with other commands. For example, to find all empty files in the current directory and delete them, do the following:

$ find . -empty -maxdepth 1 -exec rm {} \;

To search for a html file having the text ‘Web sites‘ in it, you can combine find with grep as follows:

$ find . -type f -iname \*.html -exec grep -s "Web sites" {} \;

The  -s option in grep suppresses errors about non-existent or unreadable files. And {} is a placeholder for the files found. The semicolon ‘;’ is escaped using backslash so as not to be interpreted by bash shell.

Note: You can use the -exec option to combine any command in Linux with the find command. Some of the useful things you can do with it are as follows:

Compress log files on an individual basis

$ find /var -iname \*.log -exec bzip {} \;

Find all files which belong to user pravi and change its ownership to pcuser

# find / -user pravi -exec chown pcuser {} \;

Note: You can also use xargs command instead of the -exec option as follows:

$ find /var -iname \*.log | xargs bzip –

Find all files which do not belong to any user:

$ find . -nouser

Find files which have permissions rwx for user and rw for group and others :

$ find . -perm 766

List the output of the above command  find . -perm 766

$ find . -perm 766 -exec ls -l {} \;

Find all directories with name bug_files

$ find . -type d -iname \*bug_files\*

Suppose you want to find files of size between 800k and 900k, do the following:

$ find . \( -size +800k -and -size -900k \)

And how about getting a formatted output of the above command with the size of each file listed ?

$ find . \( -size +700k -and -size -1000k \) -exec du -Hs {} \; 2>/dev/null

Here, the ‘2>/dev/null’ means all the error messages are discarded or suppressed.

You can also limit your search by file system type. For example, to restrict search to files residing only in the NTFS and VFAT filesystem, do the following:

$ find / -maxdepth 2 \( -fstype vfat -or -fstype ntfs \) 2> /dev/null

These are the most common uses of the find command. You can see additional uses by reading the find manual.


10 System info Commands

10 System info Commands

If you are developing an application for Linux desktop and would like to automatically find out more information about system, use the following commands in shell scripts to gather information about system. Before applying a command you must know how to use the command effectively in Shell. Here we are explaing basich linux system commands and their usage.


This command will help you to find out your present working direcotry.

Command Usage:

[root@l3admins ~]# pwd
[root@l3admins ~]#

2. hostname

The kernel maintains the current (unqualified) hostname of the system. Before trying to set your hostname, it’s important to know how to tell when you’ve got it right. Here are a few of the numerous ways to obtain it:

Command Usage:

[root@l3admins ~]# hostname
[root@l3admins ~]#

3. whoami

This will display the login name like admin,root or your user name.

Command Usage:

[root@l3admins ~]# whoami
[root@l3admins ~]#

4. id
This command will display all the informations about a user like user ID, memeber of which Group and prints other user identity information

Command Usage:

 [root@l3admins ~]# id root
uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel) context=root:system_r:unconfined_t:SystemLow-SystemHigh
[root@l3admins ~]#

5. date

This command will display the current date and timezone of your machine

Command Usage:

[root@l3admins ~]# date
Tue Jan  4 01:01:14 PST 2011
[root@l3admins ~]#

6. W

Its a command which conveys so many informations like load, number of users logged in, Uptime for the system.

[root@l3admins ~]# w
 01:46:06 up 59 min,  3 users,  load average: 0.00, 0.00, 0.04
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     :0       -                00:49   ?xdm?  25.17s  0.39s /usr/bin/gnome-session
root     pts/1    :0.0             00:50   43:23   0.12s  0.12s bash
root     pts/2     01:03    0.00s  0.16s  0.01s w
[root@l3admins ~]#

7. finger

This is a user information lookup program, Finger displays the userâs login name, real name, terminal name and write status (as a ââ*ââ after the terminal name if write permission is denied), idle time, login time, office location and office phone number.

Command Usage:

[root@l3admins ~]# finger root
Login: root                             Name: root
Directory: /root                        Shell: /bin/bash
On since Tue Jan  4 00:49 (PST) on :0 (messages off)
On since Tue Jan  4 00:50 (PST) on pts/1 from :0.0
   49 minutes 26 seconds idle
On since Tue Jan  4 01:03 (PST) on pts/2 from
New mail received Fri Dec 31 02:30 2010 (PST)
     Unread since Sun Dec  5 08:36 2010 (PST)
No Plan.
[root@l3admins ~]#

8. last

This command will bring you the information about the last loggedin user.

Command Usage:

[root@l3admins ~]# last
root     pts/2     Tue Jan  4 01:03   still logged in
root     pts/1        :0.0             Tue Jan  4 00:50   still logged in
root     :0                            Tue Jan  4 00:49   still logged in
root     :0                            Tue Jan  4 00:49 - 00:49  (00:00)
reboot   system boot  2.6.18-194.el5   Tue Jan  4 06:18          (-4:-23)

9. lastb

This command will give you the information about the last bad (unsuccessful) login attempts to the machine.

Command Usage:

[root@l3admins ~]# lastb
root     ssh:notty     Tue Jan  4 01:58 - 01:58  (00:00)
root     ssh:notty     Tue Jan  4 01:58 - 01:58  (00:00)

btmp begins Tue Jan  4 01:58:31 2011
[root@l3admins ~]#

10. free

Memory info (in kilobytes). “Shared” memory is the memory that can be shared between processes (e.g., executable code is “shared”). “Buffered” and “cashed” memory is the part that keeps parts of recently accessed files – it can be shrunk if more memory is needed by processes.

Command usage:

[root@l3admins ~]# free
             total       used       free     shared    buffers     cached
Mem:       1035108     655116     379992          0      88648     410024
-/+ buffers/cache:     156444     878664
Swap:      2096472          0    2096472
[root@l3admins ~]#