Linux find all php files

Содержание
  1. Finding all PHP files within a certain directory containing a string
  2. Finding all PHP files within a certain directory containing a string
  3. Shell script to show all 1 line php files
  4. Shell Command to copy a directory to another location
  5. Executing shell script in different directory using PHP
  6. 35 Practical Examples of Linux Find Command
  7. 1. Find Files Using Name in Current Directory
  8. 2. Find Files Under Home Directory
  9. 3. Find Files Using Name and Ignoring Case
  10. 4. Find Directories Using Name
  11. 5. Find PHP Files Using Name
  12. 6. Find all PHP Files in the Directory
  13. 7. Find Files With 777 Permissions
  14. 8. Find Files Without 777 Permissions
  15. 9. Find SGID Files with 644 Permissions
  16. 10. Find Sticky Bit Files with 551 Permissions
  17. 11. Find SUID Files
  18. 12. Find SGID Files
  19. 13. Find Read-Only Files
  20. 14. Find Executable Files
  21. 15. Find Files with 777 Permissions and Chmod to 644
  22. 16. Find Directories with 777 Permissions and Chmod to 755
  23. 17. Find and remove single File
  24. 18. Find and remove Multiple File
  25. 19. Find all Empty Files
  26. 20. Find all Empty Directories
  27. 21. File all Hidden Files
  28. 22. Find Single File Based on User
  29. 23. Find all Files Based on User
  30. 24. Find all Files Based on Group
  31. 25. Find Particular Files of User
  32. 26. Find Last 50 Days Modified Files
  33. 27. Find Last 50 Days Accessed Files
  34. 28. Find Last 50-100 Days Modified Files
  35. 29. Find Changed Files in Last 1 Hour
  36. 30. Find Modified Files in Last 1 Hour
  37. 31. Find Accessed Files in Last 1 Hour
  38. 32. Find 50MB Files
  39. 33. Find Size between 50MB – 100MB
  40. 34. Find and Delete 100MB Files
  41. 35. Find Specific Files and Delete

Finding all PHP files within a certain directory containing a string

Finally, prints the lines which end with (after removing the ), which will be the filenames of the files for which the count of lines containing a non-whitespace character was exactly one. Ending the with tells to provide a list of files rather than triggering the on every file, which is a lot more efficient.

Finding all PHP files within a certain directory containing a string

Im wondering if someone can help me out.

Im currently using the following to find all PHP files in a certain directory

find /home/mywebsite -type f -name "*.php" 

How would i extend that to search through those php files and get all files with the string base64_decode?

find /home/mywebsite -type f -name '*.php' -exec grep -l base64_decode <> + 

The -exec option to find executes a command on the files found. <> is replaced by the filename, and the + means that it should keep repeating this for all the filenames. grep looks for a string in the file, and the -l option tells it to print just the filename when there’s a match, not all the matching lines.

If you’re getting an error from find , you may have an old version that doesn’t support the + feature of -exec . Use this command instead:

find /home/mywebsite -type f -name '*.php' | xargs grep -l base64_decode 

xargs reads its standard input and turns them into arguments for the command line in its arguments.

Executing shell script in different directory using PHP, Use php exec function: exec(‘/path/to/shell/script’);. However this will only work if safe_mode is disabled, php has permissions to such

Shell script to show all 1 line php files

Trying to create a shell script to search a directory recursively and display a list of all php files that only contain 1 line

I think something is wrong with my IF statement but I’m not sure

#!/bin/bash shopt -s nullglob for f in *.php do if [ 'find . -type f | wc -l $f == 1' ]; then echo "$f" fi 
find . -type f -name '*.php' -exec grep -Hcm2 $ <> + | sed -n '/:1$/' 

This one will find «one-liners» which happen to have more than one line, when all the lines except one are blank:

find . -type f -name '*.php' -exec grep -Hcm2 '[^[:space:]]' <> + | sed -n '/:1$/' 

The grep options -Hcm2 mean «Always print the filename, only print the count of the matches, and match at most two lines.» The pattern $ matches any line, while the pattern «[^[:space:]]» matches any line containing a non-whitespace character. Ending the -exec with <> + tells find to provide a list of files rather than triggering the exec on every file, which is a lot more efficient. Finally, sed prints the lines which end with :1 (after removing the :1 ), which will be the filenames of the files for which the count of lines containing a non-whitespace character was exactly one.

Читайте также:  Операционная система windows операционные системы unix linux

This is arguably more efficient than wc because it normally stops reading at the second line, rather than reading entire files just to check if they have more than one line.

(Also, with respect to wc : if the file happens to have exactly one line, but that line is not terminated with a newline character, then wc will report that it has 0 lines. So if you’re filtering wc output for equality to 1, you may miss a few files.)

If you have a reasonably recent bash, you can avoid find by enabling ** globs:

shopt -s globstar nullglob grep -Hcm2 "[^[:space:]]" **/*.php | sed -n '/:1$/' 

None of the above hacks work if you have files with newline characters in their filepaths. But you don’t, right? 🙂

 for f in *.php; do awk 'END< if(NR==1) print FILENAME>' $f; done 

for the recursive lookup you need to use find, one alternative can be

 find -name *.php -print | xargs -L1 awk 'NR>1 END' 

This will search for one-line PHP files recursively:

find -name '*.php' -exec bash -c '[[ "$(wc -l < "$0")" -eq 1 ]] && echo "$0"' '<>' ';' 

If you want to test for the success or failure of a command in an if statement, you don’t use the [ operator, but instead write it after the if directly:

if find . -type f | wc -l $f == 1; then echo "$f" fi 

However, what I wrote above still doesn’t make much sense and is not what I think you intended.

The [ itself is a command used to convert arithmetic and string comparison into an if -friendly form. You can combine it with output substitution (using the $(. ) syntax) to test whether the output of a command is equal to 1.

Bash 4 provides the «globstar» shell option to handle your recursive requirement:

shopt -s globstar wc -l **/*.php | awk '$1==1' | sed 's/^ *3* *//' 

Or if you don’t like sed, you can put more into the awk part of the pipeline:

Both of these solutions, along with (at this time) every other solution posted, suffers in that long files will be read in their entirety. You don’t really need to do that — you can skip any file if you see a second line in it. So, again relying on Bash 4’s «globstar» for recursion:

Expanded for easier explanation, this is:

awk ' # Upon reading the first line of a file, add its name to an array. FNR==1 # Upon reading the second line, delete it from the array and move on. FNR==2 # Once we've processed all files, print what's left in the array. END > ' **/*.php 

This should read at a maximum two lines from each file. Potentially much faster than wc -l or grep -c .

List all the files and folders in a Directory with PHP recursive function, Get all the files and folders in a directory, don’t call function when you have . or .. . Your code :

Shell Command to copy a directory to another location

In my web application I have to copy a directory and all its contents to another location. I want to use shell_exec function of PHP. But I can’t find the command for that.

Copies source and all contents of source inside dest.

Define «location» more precisely. In any event:

  • if the directory is local: cp -r sourcedir destdir ;
  • if it is remote, using scp: scp -r sourcedir user@targetmachine:destdir

The PHP manual has a page for the copy function where you can find a many recipes for recursive directory copy. Here is an adapted recipe:

function recurse_copy($srcdir, $dstdir) < $dir = opendir($srcdir); @mkdir($dstdir); while ($file = readdir($dir)) < if ($file != '.' && $file != '..') < $src = $srcdir . '/' . $file; $dst = $dstdir . '/' . $file; if (is_dir($src)) < recurse_copy($src, $dst); >else < copy($src, $dst); >> > closedir($dir); >

PHP list of specific files in a directory, PHP # The current directory $directory = dir(«. The $files array will get all files in the directory which the specified extension

Executing shell script in different directory using PHP

I have a shell script in the /home/ directory. I’d like to execute it by using a web page. The webserver is located in /var/www/. I’m wondering how would I execute the shell script from within the homepage using PHP?

However this will only work if safe_mode is disabled, php has permissions to such folder and file, or is defined in your basedir.

How to find path of user’s home directory in PHP Cli on linux?, The CLI version of PHP puts the environment variables in $_SERVER[] . The home directory of the current user can be found in $_SERVER[‘HOME’] .

Источник

35 Practical Examples of Linux Find Command

The Linux find command is one of the most important and frequently used command command-line utility in Unix-like operating systems. The find command is used to search and locate the list of files and directories based on conditions you specify for files that match the arguments.

find command can be used in a variety of conditions like you can find files by permissions, users, groups, file types, date, size, and other possible criteria.

Through this article, we are sharing our day-to-day Linux find command experience and its usage in the form of examples.

In this article, we will show you the most used 35 Find Commands Examples in Linux. We have divided the section into Five parts from basic to advance usage of the find command.

  • Part I: Basic Find Commands for Finding Files with Names
  • Part II: Find Files Based on their Permissions
  • Part III: Search Files Based On Owners and Groups
  • Part IV: Find Files and Directories Based on Date and Time
  • Part V: Find Files and Directories Based on Size
  • Part VI: Find Multiple Filenames in Linux

1. Find Files Using Name in Current Directory

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

# find . -name tecmint.txt ./tecmint.txt

2. Find Files Under Home Directory

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

# find /home -name tecmint.txt /home/tecmint.txt

3. Find Files Using Name and Ignoring Case

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

# find /home -iname tecmint.txt ./tecmint.txt ./Tecmint.txt

4. Find Directories Using Name

Find all directories whose name is Tecmint in / directory.

# find / -type d -name Tecmint /Tecmint

5. Find PHP Files Using Name

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

# find . -type f -name tecmint.php ./tecmint.php

6. Find all PHP Files in the Directory

Find all php files in a directory.

# find . -type f -name "*.php" ./tecmint.php ./login.php ./index.php

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 are set to 644.

10. Find Sticky Bit Files with 551 Permissions

Find all the Sticky Bit set files whose permission is 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 the 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 the 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 tecmint.txt and remove it.

# find . -type f -name "tecmint.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 find all empty files under a certain path.

# find /tmp -type f -empty

20. Find all Empty Directories

To file all empty directories under a certain path.

# find /tmp -type d -empty

21. File all Hidden Files

To find all hidden files, use the below command.

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

22. Find Single File Based on User

To find all or single files called tecmint.txt under / root directory of owner root.

# find / -user root -name tecmint.txt

23. Find all Files Based on User

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

# find /home -user tecmint

24. Find all Files Based on Group

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

# find /home -group developer

25. Find Particular Files of User

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

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

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 the last 1 hour.

# find / -cmin -60

30. Find Modified Files in Last 1 Hour

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

# find / -mmin -60

31. Find Accessed Files in Last 1 Hour

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

# find / -amin -60

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 / -type f -size +100M -exec rm -f <> \;

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 rm <> \;

That’s it, We are ending this post here, In our next article, we will discuss more other Linux commands in-depth with practical examples. Let us know your opinions on this article using our comment section.

Источник

Оцените статью
Adblock
detector