Knowledgebase

Find files and folders using SSH Command and even delete them. Print

  • SSH Files, SSH Folders, Find and Delete
  • 0

Lets Say, You want to find the file name "testfile.php" then use command line below mentione and please use sudo -s as you may need to login with the root access. Also the directory structure as well. 

find /home/ -name "xkuda.php"

Now find and delete the file. 

find /home/ -name "xkuda.php" -delete 


And it will work like charm. 

I have used it when we had many of the accounts get infected.

Now to find text inside the file, you can use thiss command to find the file which contains the text. 

Find command syntax to delete dirs

Try:
find /dir/to/search/ -type d -name "dirName" -exec rm -rf {} +
OR
find /dir/to/search/ -type d -name "dirName" -exec rm -rf \;

Warning: Be careful with the rm command when using with find. You may end up deleting unwanted data.

Find will execute given command when it finds files or dirs. For example:
find . -type d -name "foo" -exec rm -rf {} +
OR
find . -type d -name "bar" -exec rm -rf "{}" \;
Sample outputs:

removed './daily.0/bar/.cache/motd.legal-displayed'
removed directory './daily.0/bar/root/.cache'
removed './daily.0/bar/.lesshst'
removed './daily.0/bar/.viminfo'
removed './daily.0/bar/.vim/.netrwhist'
removed directory './daily.0/bar/root/.vim'
removed './daily.0/bar/root/.bashrc'
removed './daily.0/bar/.ssh/authorized_keys'
removed directory './daily.0/bar/root'
removed directory './daily.0/bar/var/spool/cron/crontabs'

You can find directories that are at least four levels deep in the working directory /backups/:
find /backups/ -type d -name "bar" -depth +4 -print0 -exec rm -rf {} +




Finding a text string inside a file on a Linux server

It never fails that I find myself hunting for a way to search for a particular text string in files.  Usually I know the file, but often times I also find that I am completely unsure what file contains the string.  Or while I am writting some code I need to find how many files use a certain function.

I know that using grep is the best way to search on a Linux server, so I start there.  Here is the command syntax:

grep "text string to search for" /path/to/search

Examples
To search for a string called “myFunction” in all text files located in /var/www/html/*.php use:

grep "myFunction" /var/www/html/*.php

To search recursively in all sub-directories you would alter the command by adding the -r option:

grep -r "myFunction" /var/www/html

Now you have probably noticed that grep prints out the matching lines containing your string, but you may also need the filenames of the files containing the string instead. You can use the -H option to narrow the output the filename followed by the line containing your search string, like so:

grep -H -r "myFunction" /var/www/html

This would output something like:

...
your_file.php: line containing myFunction
..

To print out just the filename you can cut command like this to clean the output further: (Note the one after the f, not an L)

grep -H -r "myFunction" /var/www/html | cut -d: -f1

The new cleaner out put would be like:

...
your_file.php
...

Was this answer helpful?

« Back