Pages

Thursday, January 24, 2013

Find Command variants

1. Find Files Using Name in Current Directory

Find all the files whose name is server.txt in a current working directory.

# find . -name server.txt

./server.txt

2. Find Files Under Home Directory

Find all the files under /home directory with name server.txt.

# find /home -name server.txt

/home/server.txt

3. Find Files Using Name and Ignoring Case

Find all the files whose name is server.txt and contains both capital and small letters in /home directory.

# find /home -iname server.txt

./server.txt
./server.txt

4. Find Directories Using Name

Find all directories whose name is server in / directory.

# find / -type d -name server

/server

5. Find PHP Files Using Name

Find all php files whose name is server.php in a current working directory.

# find . -type f -name server.php

./server.php

6. Find all PHP Files in Directory

Find all php files in a directory.

# find . -type f -name "*.php"

./server.php
./login.php
./index.php

Part II – Find Files Based on their Permissions
7. Find Files With 777 Permissions

Find all the files whose permissions are 777.

# find . -type f -perm 0777 -print

8. Find Files Without 777 Permissions

Find all the files without permission 777.

# find / -type f ! -perm 777

9. Find SGID Files with 644 Permissions

Find all the SGID bit files whose permissions set to 644.

# find / -perm 2644

10. Find Sticky Bit Files with 551 Permissions

Find all the Sticky Bit set files whose permission are 551.

# find / -perm 1551

11. Find SUID Files

Find all SUID set files.

# find / -perm /u=s

12. Find SGID Files

Find all SGID set files.

# find / -perm /g+s

13. Find Read Only Files

Find all Read Only files.

# find / -perm /u=r

14. Find Executable Files

Find all Executable files.

# find / -perm /a=x

15. Find Files with 777 Permissions and Chmod to 644

Find all 777 permission files and use chmod command to set permissions to 644.

# find / -type f -perm 0777 -print -exec chmod 644 {} \;

16. Find Directories with 777 Permissions and Chmod to 755

Find all 777 permission directories and use chmod command to set permissions to 755.

# find / -type d -perm 777 -print -exec chmod 755 {} \;

17. Find and remove single File

To find a single file called server.txt and remove it.

# find . -type f -name "server.txt" -exec rm -f {} \;

18. Find and remove Multiple File

To find and remove multiple files such as .mp3 or .txt, then use.

# find . -type f -name "*.txt" -exec rm -f {} \;

OR

# find . -type f -name "*.mp3" -exec rm -f {} \;

19. Find all Empty Files

To file all empty files under certain path.

# find /tmp -type f -empty

20. Find all Empty Directories

To file all empty directories under certain path.

# find /tmp -type d -empty

21. File all Hidden Files

To find all hidden files, use below command.

# find /tmp -type f -name ".*"

Part III – Search Files Based On Owners and Groups
22. Find Single File Based on User

To find all or single file called server.txt under /root directory of owner root.

# find / -user root -name server.txt

23. Find all Files Based on User

To find all files that belongs to user server under /home directory.

# find /home -user server

24. Find all Files Based on Group

To find all files that belongs to group Developer under /home directory.

# find /home -group developer

25. Find Particular Files of User

To find all .txt files of user server under /home directory.

# find /home -user server -iname "*.txt"

Part IV – Find Files and Directories Based on Date and Time
26. Find Last 50 Days Modified Files

To find all the files which are modified 50 days back.

# find / -mtime 50

27. Find Last 50 Days Accessed Files

To find all the files which are accessed 50 days back.

# find / -atime 50

28. Find Last 50-100 Days Modified Files

To find all the files which are modified more than 50 days back and less than 100 days.

# find / -mtime +50 –mtime -100

29. Find Changed Files in Last 1 Hour

To find all the files which are changed in last 1 hour.

# find / -cmin -60

30. Find Modified Files in Last 1 Hour

To find all the files which are modified in last 1 hour.

# find / -mmin -60

31. Find Accessed Files in Last 1 Hour

To find all the files which are accessed in last 1 hour.

# find / -amin -60

Part V – Find Files and Directories Based on Size
32. Find 50MB Files

To find all 50MB files, use.

# find / -size 50M

33. Find Size between 50MB – 100MB

To find all the files which are greater than 50MB and less than 100MB.

# find / -size +50M -size -100M

34. Find and Delete 100MB Files

To find all 100MB files and delete them using one single command.

# find / -size +100M -exec rm -rf {} \;

35. Find Specific Files and Delete

Find all .mp3 files with more than 10MB and delete them using one single command.

# find / -type f -name *.mp3 -size +10M -exec ls -l {} \;

 

Find all empty files (zero byte file) in your home directory and its subdirectory

Most files of the following command output will be lock-files and place holders created by other applications.

# find ~ -empty

List all the empty files only in your home directory.

# find . -maxdepth 1 -empty

List only the non-hidden empty files only in the current directory.

# find . -maxdepth 1 -empty -not -name ".*"

‘df‘ command stand for linux “disk filesystem“

Linux has a strong built in utility called ‘df‘. The ‘df‘ command stand for “disk filesystem“, it is used to get full summary of available and used disk space usage of file system on Linux system.

Using ‘-h‘ parameter with (df -h) will shows the file system disk space statistics in “human readable” format, means it gives the details in bytes, mega bytes and gigabyte.
How to Check Disk Space in Linux

Useful df Command Examples

This article explain a way to get the full information of Linux disk space usage with the help of ‘df‘ command with their practical examples. So, you could better understand the usage of df command in Linux.
1. Check File System Disk Space Usage

The “df” command displays the information of device name, total blocks, total disk space, used disk space, available disk space and mount points on a file system.

[root@tecmint ~]# df

Filesystem 1K-blocks Used Available Use% Mounted on
/dev/cciss/c0d0p2 78361192 23185840 51130588 32% /
/dev/cciss/c0d0p5 24797380 22273432 1243972 95% /home
/dev/cciss/c0d0p3 29753588 25503792 2713984 91% /data
/dev/cciss/c0d0p1 295561 21531 258770 8% /boot
tmpfs 257476 0 257476 0% /dev/shm

2. Display Information of all File System Disk Space Usage

The same as above, but it also displays information of dummy file systems along with all the file system disk usage and their memory utilization.

[root@tecmint ~]# df -a

Filesystem 1K-blocks Used Available Use% Mounted on
/dev/cciss/c0d0p2 78361192 23186116 51130312 32% /
proc 0 0 0 - /proc
sysfs 0 0 0 - /sys
devpts 0 0 0 - /dev/pts
/dev/cciss/c0d0p5 24797380 22273432 1243972 95% /home
/dev/cciss/c0d0p3 29753588 25503792 2713984 91% /data
/dev/cciss/c0d0p1 295561 21531 258770 8% /boot
tmpfs 257476 0 257476 0% /dev/shm
none 0 0 0 - /proc/sys/fs/binfmt_misc
sunrpc 0 0 0 - /var/lib/nfs/rpc_pipefs

3. Show Disk Space Usage in Human Readable Format

Have you noticed that above commands displays information in bytes, which is not readable yet all, because we are in a habit of reading the sizes in megabytes, gigabytes etc. as it makes very easy to understand and remember.

The df command provides an option to display sizes in Human Readable formats by using ‘-h’ (prints the results in human readable format (e.g., 1K 2M 3G)).

[root@tecmint ~]# df -h

Filesystem Size Used Avail Use% Mounted on
/dev/cciss/c0d0p2 75G 23G 49G 32% /
/dev/cciss/c0d0p5 24G 22G 1.2G 95% /home
/dev/cciss/c0d0p3 29G 25G 2.6G 91% /data
/dev/cciss/c0d0p1 289M 22M 253M 8% /boot
tmpfs 252M 0 252M 0% /dev/shm

4. Display Information of /home File System

To see the information of only device /home file system in human readable format use the following command.

[root@tecmint ~]# df -hT /home

Filesystem Type Size Used Avail Use% Mounted on
/dev/cciss/c0d0p5 ext3 24G 22G 1.2G 95% /home

5. Display Information of File System in Bytes

To display all file system information and usage in 1024-byte blocks, use the option ‘-k‘ (e.g. –block-size=1K) as follows.

[root@tecmint ~]# df -k

Filesystem 1K-blocks Used Available Use% Mounted on
/dev/cciss/c0d0p2 78361192 23187212 51129216 32% /
/dev/cciss/c0d0p5 24797380 22273432 1243972 95% /home
/dev/cciss/c0d0p3 29753588 25503792 2713984 91% /data
/dev/cciss/c0d0p1 295561 21531 258770 8% /boot
tmpfs 257476 0 257476 0% /dev/shm

6. Display Information of File System in MB

To display information of all file system usage in MB (Mega Byte) use the option as ‘-m‘.

[root@tecmint ~]# df -m

Filesystem 1M-blocks Used Available Use% Mounted on
/dev/cciss/c0d0p2 76525 22644 49931 32% /
/dev/cciss/c0d0p5 24217 21752 1215 95% /home
/dev/cciss/c0d0p3 29057 24907 2651 91% /data
/dev/cciss/c0d0p1 289 22 253 8% /boot
tmpfs 252 0 252 0% /dev/shm

7. Display Information of File System in GB

To display information of all file system statistics in GB (Gigabyte) use the option as ‘df -h‘.

[root@tecmint ~]# df -h

Filesystem Size Used Avail Use% Mounted on
/dev/cciss/c0d0p2 75G 23G 49G 32% /
/dev/cciss/c0d0p5 24G 22G 1.2G 95% /home
/dev/cciss/c0d0p3 29G 25G 2.6G 91% /data
/dev/cciss/c0d0p1 289M 22M 253M 8% /boot
tmpfs 252M 0 252M 0% /dev/shm

8. Display File System Inodes

Using ‘-i‘ switch will display the information of number of used inodes and their percentage for the file system.

[root@tecmint ~]# df -i

Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/cciss/c0d0p2 20230848 133143 20097705 1% /
/dev/cciss/c0d0p5 6403712 798613 5605099 13% /home
/dev/cciss/c0d0p3 7685440 1388241 6297199 19% /data
/dev/cciss/c0d0p1 76304 40 76264 1% /boot
tmpfs 64369 1 64368 1% /dev/shm

9. Display File System Type

If you notice all the above commands output, you will see there is no file system type mentioned in the results. To check the file system type of your system use the option ‘T‘. It will display file system type along with other information.

[root@tecmint ~]# df -T

Filesystem Type 1K-blocks Used Available Use% Mounted on
/dev/cciss/c0d0p2 ext3 78361192 23188812 51127616 32% /
/dev/cciss/c0d0p5 ext3 24797380 22273432 1243972 95% /home
/dev/cciss/c0d0p3 ext3 29753588 25503792 2713984 91% /data
/dev/cciss/c0d0p1 ext3 295561 21531 258770 8% /boot
tmpfs tmpfs 257476 0 257476 0% /dev/shm

10. Include Certain File System Type

If you want to display certain file system type use the ‘-t‘ option. For example, the following command will only display ext3 file system.

[root@tecmint ~]# df -t ext3

Filesystem 1K-blocks Used Available Use% Mounted on
/dev/cciss/c0d0p2 78361192 23190072 51126356 32% /
/dev/cciss/c0d0p5 24797380 22273432 1243972 95% /home
/dev/cciss/c0d0p3 29753588 25503792 2713984 91% /data
/dev/cciss/c0d0p1 295561 21531 258770 8% /boot

11. Exclude Certain File System Type

If you want to display file system type that doesn’t belongs to ext3 type use the option as ‘-x‘. For example, the following command will only display other file systems types other than ext3.

[root@tecmint ~]# df -x ext3

Filesystem 1K-blocks Used Available Use% Mounted on
tmpfs 257476 0 257476 0% /dev/shm

=====================
[root@tecmint ~]# df
[root@tecmint ~]# df -h
[root@tecmint ~]# df -a
[root@tecmint ~]# df -hT /home
[root@tecmint ~]# df -k
[root@tecmint ~]# df -m
[root@tecmint ~]# df -h
[root@tecmint ~]# df -i
[root@tecmint ~]# df -T
[root@tecmint ~]# df -t ext3
[root@tecmint ~]# df -x ext3

[root@tecmint ~]# df -aihT
=====================

(Disk Usage) “du” is a standard Unix/Linux command

The Linux “du” (Disk Usage) is a standard Unix/Linux command, used to check the information of disk usage of files and directories on a machine. The du command has many parameter options that can be used to get the results in many formats. The du command also displays the files and directory sizes in a recursively manner.
Check Disk Usage In Linux

Check Disk Usage of Files and Folders In Linux

1. To find out the disk usage summary of a /home/server directory tree and each of its sub directories. Enter the command as:

[root@]# du /home/server

40 /home/server/downloads
4 /home/server/.mozilla/plugins
4 /home/server/.mozilla/extensions
12 /home/server/.mozilla
12 /home/server/.ssh
689112 /home/server/Ubuntu-12.10
689360 /home/server

The output of the above command displays the number of disk blocks in the /home/server directory along with its sub-directories.

2. Using “-h” option with “du” command provides results in “Human Readable Format“. Means you can see sizes in Bytes, Kilobytes, Megabytes, Gigabytes etc.

[root@server]# du -h /home/server

40K /home/server/downloads
4.0K /home/server/.mozilla/plugins
4.0K /home/server/.mozilla/extensions
12K /home/server/.mozilla
12K /home/server/.ssh
673M /home/server/Ubuntu-12.10
674M /home/server

3. To get the summary of a grand total disk usage size of an directory use the option “-s” as follows.

[root@server]# du -sh /home/server

674M /home/server

4. Using “-a” flag with “du” command displays the disk usage of all the files and directories.

[root@server]# du -a /home/server

4 /home/server/.bash_logout
12 /home/server/downloads/uploadprogress-1.0.3.1.tgz
24 /home/server/downloads/Phpfiles-org.tar.bz2
40 /home/server/downloads
12 /home/server/uploadprogress-1.0.3.1.tgz
4 /home/server/.mozilla/plugins
4 /home/server/.mozilla/extensions
12 /home/server/.mozilla
4 /home/server/.bashrc
689108 /home/server/Ubuntu-12.10/ubuntu-12.10-server-i386.iso
689112 /home/server/Ubuntu-12.10
689360 /home/server

5. Using “-a” flag along with “-h” displays disk usage of all files and folders in human readeable format. The below output is more easy to understand as it shows the files in Kilobytes, Megabytes etc.

[root@server]# du -ah /home/server

4.0K /home/server/.bash_logout
12K /home/server/downloads/uploadprogress-1.0.3.1.tgz
24K /home/server/downloads/Phpfiles-org.tar.bz2
40K /home/server/downloads
12K /home/server/uploadprogress-1.0.3.1.tgz
4.0K /home/server/.mozilla/plugins
4.0K /home/server/.mozilla/extensions
12K /home/server/.mozilla
4.0K /home/server/.bashrc
673M /home/server/Ubuntu-12.10/ubuntu-12.10-server-i386.iso
673M /home/server/Ubuntu-12.10
674M /home/server

6. Find out the disk usage of a directory tree with its subtress in Kilobyte blcoks. Use the “-k” (displays size in 1024 bytes units).

[root@server]# du -k /home/server
40 /home/server/downloads
4 /home/server/.mozilla/plugins
4 /home/server/.mozilla/extensions
12 /home/server/.mozilla
12 /home/server/.ssh
689112 /home/server/Ubuntu-12.10
689360 /home/server

7. To get the summary of disk usage of directory tree along with its subtrees in Megabytes (MB) only. Use the option “-mh” as follows. The “-m” flag counts the blocks in MB units and “-h” stands for human readable format.

[root@server]# du -mh /home/server

40K /home/server/downloads
4.0K /home/server/.mozilla/plugins
4.0K /home/server/.mozilla/extensions
12K /home/server/.mozilla
12K /home/server/.ssh
673M /home/server/Ubuntu-12.10
674M /home/server

8. The “-c” flag provides a grand total usage disk space at the last line. If your directory taken 674MB space, then the last last two line of the output would be.

[root@server]# du -ch /home/server

40K /home/server/downloads
4.0K /home/server/.mozilla/plugins
4.0K /home/server/.mozilla/extensions
12K /home/server/.mozilla
12K /home/server/.ssh
673M /home/server/Ubuntu-12.10
674M /home/server
674M total

9. The below command calculates and displays the disk usage of all files and directories, but excludes the files that matches given pattern. The below command excludes the “.txt” files while calculating the total size of diretory. So, this way you can exclude any file formats by using flag “-–exclude“. See the output there is no txt files entry.

[root@server]# du -ah --exclude="*.txt" /home/server

4.0K /home/server/.bash_logout
12K /home/server/downloads/uploadprogress-1.0.3.1.tgz
24K /home/server/downloads/Phpfiles-org.tar.bz2
40K /home/server/downloads
12K /home/server/uploadprogress-1.0.3.1.tgz
4.0K /home/server/.bash_history
4.0K /home/server/.bash_profile
4.0K /home/server/.mozilla/plugins
4.0K /home/server/.mozilla/extensions
12K /home/server/.mozilla
4.0K /home/server/.bashrc
24K /home/server/Phpfiles-org.tar.bz2
4.0K /home/server/geoipupdate.sh
4.0K /home/server/.zshrc
120K /home/server/goaccess-0.4.2.tar.gz.1
673M /home/server/Ubuntu-12.10/ubuntu-12.10-server-i386.iso
673M /home/server/Ubuntu-12.10
674M /home/server

10. Display the disk usage based on modification of time, use the flag “–time” as shown below.

[root@server]# du -ha --time /home/server

4.0K 2012-10-12 22:32 /home/server/.bash_logout
12K 2013-01-19 18:48 /home/server/downloads/uploadprogress-1.0.3.1.tgz
24K 2013-01-19 18:48 /home/server/downloads/Phpfiles-org.tar.bz2
40K 2013-01-19 18:48 /home/server/downloads
12K 2013-01-19 18:32 /home/server/uploadprogress-1.0.3.1.tgz
4.0K 2012-10-13 00:11 /home/server/.bash_history
4.0K 2012-10-12 22:32 /home/server/.bash_profile
0 2013-01-19 18:32 /home/server/xyz.txt
0 2013-01-19 18:32 /home/server/abc.txt
4.0K 2012-10-12 22:32 /home/server/.mozilla/plugins
4.0K 2012-10-12 22:32 /home/server/.mozilla/extensions
12K 2012-10-12 22:32 /home/server/.mozilla
4.0K 2012-10-12 22:32 /home/server/.bashrc
24K 2013-01-19 18:32 /home/server/Phpfiles-org.tar.bz2
4.0K 2013-01-19 18:32 /home/server/geoipupdate.sh
4.0K 2012-10-12 22:32 /home/server/.zshrc
120K 2013-01-19 18:32 /home/server/goaccess-0.4.2.tar.gz.1
673M 2013-01-19 18:51 /home/server/Ubuntu-12.10/ubuntu-12.10-server-i386.iso
673M 2013-01-19 18:51 /home/server/Ubuntu-12.10
674M 2013-01-19 18:52 /home/server

=================
[root@server]# du /home/server
[root@server]# du -h /home/server
[root@server]# du -sh /home/server
[root@server]# du -a /home/server
[root@server]# du -ah /home/server
[root@server]# du -k /home/server
[root@server]# du -mh /home/server
[root@server]# du -ch /home/server
[root@server]# du -ah --exclude="*.txt" /home/server
[root@server]# du -ha --time /home/server
[root@server]# du -sach *
=================