0% found this document useful (0 votes)
177 views14 pages

1.1 Find - Command

The document provides examples of commands using the Linux find command to search for files and directories based on various criteria such as name, permissions, size, date, and user. It gives 26 examples ranging from finding files by name, type, permissions to finding files modified within a certain time period and deleting files matching certain criteria. The find command allows searching by various file attributes and combining with other commands like exec to perform actions on the results.

Uploaded by

gidum2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
177 views14 pages

1.1 Find - Command

The document provides examples of commands using the Linux find command to search for files and directories based on various criteria such as name, permissions, size, date, and user. It gives 26 examples ranging from finding files by name, type, permissions to finding files modified within a certain time period and deleting files matching certain criteria. The find command allows searching by various file attributes and combining with other commands like exec to perform actions on the results.

Uploaded by

gidum2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 14

1

The Linux Find Command is one of the most important and much used command in Linux
systems. Find command used to search and locate list of files and directories based on
conditions you specify for files that match the arguments. Find can be used in variety of
conditions like you can find files by permissions, users, groups, file type, date, size and
other possible criteria.

The find command is available on most linux distros by default so you do not have to install
any package. The find command is an essential one to learn, if you want to get super
productive with the command line on linux.

The basic syntax of the find command looks like this

$ find location comparison-criteria search-term

2
1. Find Files Using Name in Current Directory

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

# find . -name technology.txt

2. Find Files Under Home Directory

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

# find /home -name technology.txt

3. Find Files Using Name and Ignoring Case

# find /home -iname technology.txt

3
4. Find Directories Using Name

# find / -type d -name abcd

5. Find xyz Files Using Name

Find all xyz files whose name is technology.xyz in a current working directory.

# find . -type f -name technology.xyz

6. Find all xyz files in directory

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

4
7. Find Files With 777 Permissions

# find . -type f -perm 0777 -print

8. Find Files Without 777 Permissions

# find / -type f ! -perm 777

9. Find Read Only Files

# find / -perm /u=r

5
10. Find Executable Files

find / -perm /a=x

11.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 {} \;

12. 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 {} \;

6
13. Find and remove single File

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

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

14. 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 {} \;

7
15. Find all Empty Files

To find all empty files under certain path.

# find /tmp -type f -empty

16. File all Hidden Files

To find all hidden files, use below command.

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

8
17. Find all Files Based on User

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

# find /home -user tecmint

Find Files and Directories Based on Date and Time

18.Find Last 50 Days Modified Files

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

# find / -mtime 50

9
19.Find Last 50 Days Accessed Files

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

# find / -atime 50

20. Find Changed Files in Last 1 Hour

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

# find / -cmin -60

10
21. Find Modified Files in Last 1 Hour

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

# find / -mmin -60

22. Find Accessed Files in Last 1 Hour

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

# find / -amin -60

23. Find 50MB Files

To find all 50MB files, use.

# find / -size 50M

11
24. Find Size between 50MB – 100MB

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

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

25. Find and Delete 100MB Files

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

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

26. 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 rm {} \;

12
Find Top Running Processes by Highest Memory and CPU Usage in Linux

ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head

ps -eo pmem,pcpu,vsize,pid,cmd | sort -k 1 -nr | head -5

ps axo ruser,%mem,comm,pid,euser | sort -nr | head -n 10

top -b -o +%MEM | head -n 22

From the command above, the option:

-b : runs top in batch mode


-o : used to specify fields for sorting processes
head utility displays the first few lines of a file and
the -n option is used to specify the number of lines to be displayed.

13
Find Top Running Processes by Highest Memory and CPU Usage in Linux

Biggest cpu consuming processes

ps -eo pmem,pcpu,pid,args | tail -n +2 | sort -rnk 2 | head

ps Report a snapshot of the current processes


- -e Select all processes
- o Specify user-defined format
- pmem,pcpu,pid,args user-defined format: memory,cpu, pid number and command
- | tail -n +2 Output lines starting to the second line (to avoid column names such %MEM, etc ...)
- | sort -rnk 1 reverse (r), numeric sort (n) by column 1 (memory)
- | sort -rnk 2 reverse (r), numeric sort (n) by column 2 (cpu)
- | head output the 10 first lines

14

You might also like