The domain we use is yourdomainname.com
login to shell as root
cd /usr/local/src
wget http://prdownloads.sourceforge.net/awstats/awstats-7.4.tar.gz
tar zxvf awstats-7.4.tar.gz
mv awstats-7.4 /usr/local/apache/htdocs
cd /usr/local/apache/htdocs/awstats-7.4/tools
perl awstats_configure.pl
-----> Running OS detected: Linux, BSD or Unix
Warning: AWStats standard directory on Linux OS is '/usr/local/awstats'.
If you want to use standard directory, you should first move all content
of AWStats distribution from current directory:
/usr/local/apache/htdocs/awstats-7.4
to standard directory:
/usr/local/awstats
And then, run configure.pl from this location.
Do you want to continue setup from this NON standard directory [yN] ? y
-----> Check for web server install
Found Web server Apache config file '/usr/local/apache/conf/httpd.conf'
-----> Check and complete web server config file '/usr/local/apache/conf/httpd.conf'
Add 'Alias /awstatsclasses "/usr/local/apache/htdocs/awstats-7.4/wwwroot/classes/"'
Add 'Alias /awstatscss "/usr/local/apache/htdocs/awstats-7.4/wwwroot/css/"'
Add 'Alias /awstatsicons "/usr/local/apache/htdocs/awstats-7.4/wwwroot/icon/"'
Add 'ScriptAlias /awstats/ "/usr/local/apache/htdocs/awstats-7.4/wwwroot/cgi-bin/"'
Add '<Directory>' directive
AWStats directives added to Apache config file.
Do you want me to setup Apache to write 'combined' log files [y/N] ? --> N
(No, because we use custom varnish+Apache2+CloudFlare installed, so keep using varnishcombined as log format)
see this posting about varnish+cloudflare+apache2 before
-----> Update model config file '/usr/local/apache/htdocs/awstats-7.4/wwwroot/cgi-bin/awstats.model.conf'
File awstats.model.conf updated.
-----> Need to create a new config file ?
Do you want me to build a new AWStats config/profile
file (required if first install) [y/N] ? y
-----> Define config file name to create
What is the name of your web site or profile analysis ?
Example: www.mysite.com
Example: demo
Your web site, virtual server or profile name:
> yourdomainname.com
-----> Define config file path
In which directory do you plan to store your config file(s) ?
Default: /etc/awstats
Directory path to store config file(s) (Enter for default): Enter!
>
-----> Create config file '/etc/awstats/awstats.yourdomainname.com.conf'
Config file /etc/awstats/awstats.yourdomainname.com.conf created.
-----> Restart Web server with '/sbin/service httpd restart'
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
-----> Add update process inside a scheduler
Sorry, configure.pl does not support automatic add to cron yet.
You can do it manually by adding the following command to your cron:
/usr/local/apache/htdocs/awstats-7.4/wwwroot/cgi-bin/awstats.pl -update -config=yourdomainname.com
Or if you have several config files and prefer having only one command:
/usr/local/apache/htdocs/awstats-7.4/tools/awstats_updateall.pl now
Press ENTER to continue...
A SIMPLE config file has been created: /etc/awstats/awstats.yourdomainname.com.conf
You should have a look inside to check and change manually main parameters.
You can then manually update your statistics for 'yourdomainname.com' with command:
> perl awstats.pl -update -config=yourdomainname.com
You can also read your statistics for 'yourdomainname.com' with URL:
> http://localhost/awstats/awstats.pl?config=yourdomainname.com
Press ENTER to finish...
cd /usr/local/apache/htdocs
chown -R nobody:nobody awstats-7.4
mkdir -p /home/roy/awstats
chown roy:roy /home/roy/awstats
cd /etc/awstats
nano awstats.yourdomainname.com.conf
Find DirData="/var/lib/awstats" and replace this with DirData="/home/roy/awstats"
Find LogFile="/var/log/httpd/mylog.log" and replace it with LogFile="/usr/local/apache/logs/access_log"
Find LogFormat, we use varnish modified apache logs (varnishcombine), so change this to:
LogFormat = "%host, %host %other %logname %time1 %methodurl %code %bytesd %refererquot %uaquot"
rm -rf /usr/local/apache/logs/access_log
service httpd restart
perl /usr/local/apache/htdocs/awstats-7.4/wwwroot/cgi-bin/awstats.pl -update -config=yourdomainname.com
check :
http://yourserverip/awstats/awstats.pl?config=yourdomainname.com
http://118.199.118.61/awstats/awstats.pl?config=yourdomainname.com
Admin Books
DOWNLOAD Free e-Books for Linux Admin Servers :
Browse » Home » All posts
SSD server with 2GB of RAM and 50GB of storage for $3.40 per month (€2.99)
Online.net’s Scaleway is an interesting beast in the cloud hosting world. Instead of building a virtual cloud hosting infrastructure that competes directly with Amazon Web Services, DigitalOcean and other VPS providers, the company designed its own ARM-based servers.
And that’s why the company can drive the prices down so much. You can now get a BareMetal SSD server with 2GB of RAM and 50GB of storage for $3.40 per month (€2.99) — that’s 70 percent cheaper than Scaleway’s previous pricing of €9.99 per month.
The company has stated on Twitter that the new pricing applies to existing users as well. As a reminder, as ARM v7 chipsets were first designed for smartphones, it’s very easy to run many of them with very little power, cooling and space. Scaleway managed to squeeze 912 separate computers in a single server rack.
The company also kept the best of both worlds — dedicated servers with the flexibility of virtualization as you get 4 dedicated ARM cores, a dedicated IP and 200Mbit/s of unmetered bandwidth. Contrarily to many popular VPS providers, you won’t share your CPU raw power with other users.
And that’s why the company can drive the prices down so much. You can now get a BareMetal SSD server with 2GB of RAM and 50GB of storage for $3.40 per month (€2.99) — that’s 70 percent cheaper than Scaleway’s previous pricing of €9.99 per month.
The company has stated on Twitter that the new pricing applies to existing users as well. As a reminder, as ARM v7 chipsets were first designed for smartphones, it’s very easy to run many of them with very little power, cooling and space. Scaleway managed to squeeze 912 separate computers in a single server rack.
The company also kept the best of both worlds — dedicated servers with the flexibility of virtualization as you get 4 dedicated ARM cores, a dedicated IP and 200Mbit/s of unmetered bandwidth. Contrarily to many popular VPS providers, you won’t share your CPU raw power with other users.
Get Real IP Address in server traffic from CLOUDFLARE-->VARNISH-->APACHE2
How can I log the client IP address on the backend?
All I see is the IP address of the varnish server. How can I log the client IP address?
We will need to add the IP address to a header used for the backend request, and configure the backend to log the content of this header instead of the address of the connecting client (which is the varnish server).
Varnish configuration:
sub vcl_recv {
# Add a unique header containing the client address
remove req.http.X-Forwarded-For;
set req.http.X-Forwarded-For = client.ip;
# [...]
}
For the apache configuration, we copy the “combined” log format to a new one we call “varnishcombined”, for instance, and change the client IP field to use the content of the variable we set in the varnish configuration:
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" varnishcombined
And so, in our virtualhost, you need to specify this format instead of “combined” (or “common”, or whatever else you use):
<VirtualHost *:80>
ServerName www.example.com
# [...]
CustomLog /var/log/apache2/www.example.com/access.log varnishcombined
# [...]
</VirtualHost>
Getting Varnish, CloudFlare and Apache to play nicely with X-Forwarded-For can be a pain. However not setting this up right can cause serious PHP session problems with HTTPS requests passed through Varnish and CloudFlare to Apache. It also makes it hard to consistently get the real IP of users connecting via HTTP. This tutorial is a quick run-down on getting all of these things to play nicely and relay the real IP.
Now lets move on the Varnish. Edit your /etc/varnish/default.vcl to contain the following:
sub vcl_recv {
remove req.http.X-Forwarded-For;
if (req.http.cf-connecting-ip) {
set req.http.X-Forwarded-For = req.http.cf-connecting-ip;
} else {
set req.http.X-Forwarded-For = client.ip;
}
remove req.http.X-Forwarded-For;
if (req.http.cf-connecting-ip) {
set req.http.X-Forwarded-For = req.http.cf-connecting-ip;
} else {
set req.http.X-Forwarded-For = client.ip;
}
Disable Wordpress Auto Update In the Middle of The Night
In case you haven’t noticed, WordPress auto-updates its core files by default. Neat but not always desired. Here is how to disable auto-updates with a few lines of code.
Rationale
Although WP’s auto updates are real neat, such functionality is not always desired. Like in the dead of night some script is changing stuff on your site with no humans to verify proper results. If something goes awry, it could leave your site down or even vulnerable for who knows how long until someone actually checks in to see what’s up. Avoid this scenario? Yes please. Read on to learn how (without a plugin)..
Disable auto-updates for the WordPress core
Add the following line to your site’s
wp-config.php file:define('WP_AUTO_UPDATE_CORE', false);
add_filter('auto_update_plugin', '__return_false');
add_filter('auto_update_theme', '__return_false');
Testing Debug Exim Installation Error
Test that Exim has been installed by running:
$ /usr/exim/bin/exim -bV
which should tell you Exim's version number and some other information about which features are included. Replace Sendmail with Exim All the MUAs call /usr/sbin/sendmail to pass messages to the MTA. We want them to call Exim instead of Sendmail.
---> We want programs to call exim rather than sendmail, but leave sendmail installed so that if there is any existing mail in sendmail's queue, it can be flushed later. Red Hat lets us choose a new MTA by setting a symlink in the /etc/alternatives directory. We redirect the programs 'sendmail' and 'mailq' to point to exim.
# cd /etc/alternatives
# rm mta
# ln -s /usr/exim/bin/exim mta
# rm mta-mailq
# ln -s /usr/exim/bin/exim mta-mailq
---> Now try that basic test again, but this time using the standard path name:
$ /usr/sbin/sendmail -bV
You should get the same output as before, which shows that Exim is now being used instead of Sendmail.
Testing Exim
If you are doing a real installation on a live system, you might want to work on the configuration and do lots of testing before removing Sendmail and replacing it with Exim. Test the standard installation and configuration
---> To save typing, adjust your PATH variable so that the command exim can be used to run the Exim binary. Take great care when you do this, because messing up your PATH will make many commands ``vanish''. Type this command exactly, taking care with the colon and dollar in the middle:
$ export PATH=/usr/exim/bin:$PATH
Make sure you substitute a real local user name for localuser in what follows. Remember, you should not be root when running these tests.
First, check what Exim will do with a local address:
$ exim -bt localuser
This tests the delivery routing for a local account. See what output you get.
Try with a non-existent local user and see what happens:
$ exim -bt junkjunkjunk
---> Try something that is in /etc/aliases: $ exim -bt postmaster Exim will not normally deliver mail to a root mailbox (for security reasons) so what people usually do is to make root an alias for the sysadmin. In Red Hat, all the default aliases point to root. Therefore, you should add a new alias to /etc/aliases. Add this line at the end: # Person who should get root's mail root: yourname Now try this again: $ exim -bt postmaster ---> Now we are going to try a real local delivery. You can pass a message directly to Exim without using an MUA: $ exim -v -odf localuser This is a test message. . Note: the message is terminated by a line that just contains a dot. Be sure to type it! (Alternatively, you can send ``end of file'' by pressing CTRL-D.) The -v option turns on user verification output, which shows you copies of Exim's log lines. The -odf option requests `foreground' delivery, which means that the exim command won't return until the delivery is complete. (This avoids your shell prompt getting mixed up with Exim's output.) ---> Check what is in Exim's logs: $ cat /var/spool/exim/log/mainlog $ cat /var/spool/exim/log/paniclog The panic log should normally be empty, and if nothing has ever been written to it, it will not even exist. Tip: On a live system it is helpful to set up a cron job that mails you a warning if it ever finds a non-empty panic log. If you get a permission error, make sure that your username is in the 'exim' group, then logout and login again to become a member of that group. If the delivery succeeded, you should see two lines in the main log, one containing <= for the message arriving, and one containing => for the delivery. ---> Now go check the local user's mailbox: $ ls -l /var/spool/mail/localuser $ cat /var/spool/mail/localuser If the delivery didn't succeed, you need to find out why. If the information in the log doesn't help, you can try the delivery again, with debugging turned on: $ exim -d -odf localuser <there will be output from Exim here> This is another test message. . The -d option turns on debugging, which gives a lot more information than -v. You need to be an Exim administrator to use -d. If you get a `Permission denied' error, check that you are a member of the Exim group. ---> If you are logged on as localuser, you can use the mail command to read the mail in the usual way. You could also try sending a message from the mail command. The next thing is to test whether Exim can send to a remote host. The speed of this may vary, depending on the state of the network connection. In what follows, replace user@remote.host with your home email address. ---> First, check that Exim can route to the address: $ exim -bt user@remote.host ---> Now send a message to the remote address: $ exim -v -odf user@remote.host This is a test message. . This time, the -v option causes Exim to display the SMTP dialogue as well as the log lines. If you can, check that the message arrived safely. If there are problems, see if you can figure out what went wrong and why. ---> You won't be able to receive messages from a remote host until you start the Exim daemon: $ /usr/exim/bin/exim -bd -q20m The -bd option causes the daemon to listen for incoming SMTP calls, and the -q20m option causes it to start a queue runner process every 20 minutes. On a live system, starting the daemon should happen automatically on a reboot, by putting this command in /etc/rc.local . ---> Use telnet to check that the daemon is accepting SMTP calls: $ telnet localhost 25 You should see an Exim greeting message. Use QUIT to exit. ---> Now check that a remote host can send a message to your host, and see how Exim logs what happens. If that succeeds, you have a working basic installation correctly installed. ---> Try sending to an invalid address from a remote host, and see what error message you get, and how Exim logs this case. Look in bothmainlog and rejectlog. Starting the Exim Monitor You need to have an X-windows session running to run the monitor. ---> Start the monitor: $ /usr/exim/bin/eximon The upper window shows a `tail' of the main log; the lower window shows the messages that are waiting in the queue. Expect both to be empty to start with. Send a few messages and watch what the monitor displays. Queue management tests There are several command line options (and equivalent menu items in the monitor) for doing things to messages. ---> To put a message on the queue without its being delivered, run $ exim -odq address1 address2 ... Test message. . The message stays on the queue until a queue runner process notices it. ---> List the messages on the queue: $ exim -bp ---> Do a manual queue run, with minimal verification output: $ exim -v -q (Without -v you won't see any output at all on the terminal, but there will be entries in the log.) Checking relay control ---> To demonstrate that Exim will relay by default via the loopback interface, try the following sequence of SMTP commands. Wait for Exim to respond to each command before typing the next one. Substitute the number of your pc for nn: $ telnet 127.0.0.1 25 ehlo localhost mail from:<localuser@pcnn.taller.nsrc.org> rcpt to:<localuser@pcnn.taller.nsrc.org> rcpt to:<user@some.remote.domain> You should get an OK response to all the SMTP commands. Type `quit' to end the SMTP session without actually sending a message. ---> Now try the same thing, but use your host's IP address instead of 127.0.0.1. $ telnet xx.xx.xx.nn 25 ehlo localhost mail from:<localuser@pcnn.taller.nsrc.org> rcpt to:<localuser@pcnn.taller.nsrc.org> rcpt to:<user@some.remote.domain> In this case, you should get the error message 550 relay not permitted for the second RCPT command, which is the one that is trying to relay. The first RCPT command should be accepted, because it specifies a local delivery. You could also try telnetting from an external host and running the same check. Processing log data ---> Run exigrep to extract all information about a certain message, or a certain user's messages, or messages for a certain domain. For example: $ exigrep localuser /var/spool/exim/log/mainlog That extracts all the log information for all messages that have any log line containing `localuser'. It's a Perl pattern match, so you can use Perl regular expressions. ---> To extract simple statistics from a log, run $ eximstats /var/spool/exim/log/mainlog | more There are options for selecting which bits you don't want. Details are in the manual. If you have time, experiment with the options for outputting the statistics as HTML. 3. Changing the configuration To change Exim's runtime configuration, you must edit /usr/exim/configure and then HUP the Exim daemon (as root). The daemon stores its process id (pid) in a file, in order to make this easy. This command restarts the daemon: # cat /var/spool/exim/exim-daemon.pid # kill -HUP nnnn where nnnn is the pid from the previous line. You can confirm that the daemon has restarted by checking the main log. You are going to be restarting Exim a lot, so make yourself a script to save typing. Use vi to create a file called /usr/local/bin/hupexim, containing these lines: #!/bin/bash kill -HUP $(cat /var/spool/exim/exim-daemon.pid) Note that the # character in the first line is part of the file (it's not a prompt). Now make the new file into an executable script: # chmod a+x /usr/local/bin/hupexim [!] If you are using the C-shell (csh) you must also run this command: # rehash This causes the internal hash table of the contents of the directories in the PATH variable to be recomputed. This is not necessary if you are using bash. Now you can restart Exim just by running: # hupexim The following sections contain some suggestions for configuration modifications that you can try, just to get a feel for how the configuration file works. You do not have to stick rigidly to these examples; use different domain names or user names if you want to. Adding more local domains ---> Edit the configuration, and change the local_domains setting so that it looks like this: domainlist local_domains = @ : testnn.nsrc.org where nn is the number of your host. Remember to HUP the daemon afterwards. Now you have a new local domain. Try sending it some mail: $ mail yourname@testnn.afnog.org Check that it arrives in your mailbox. [!] Note: The domains that we are adding now can only be used from your own host, because there are no DNS records for them. When you are adding domains to a production host, you must of course also add MX records for them. If you want to add a lot of domains, or if you want to keep changing them, it is easier to keep the list of domains in a file instead of in the Exim configuration. (You can also keep them in several different kinds of database, such as LDAP or MySQL, but we don't cover that in this workshop.) We are now going to add some domains like this, and then make them into virtual domains. ---> Use vi to create a file called /usr/exim/vdomains that contains a list of domains (as many as you like): vdom1.nsrc.org vdom2.nsrc.org ... ---> Edit /usr/exim/configure to change the local domains setting: domainlist local_domains = @ : testnn.nsrc.org : \ lsearch;/usr/exim/vdomains Note: There is no space following the semicolon. This change makes all the new domains into local domains. ---> Now we add a new router to handle these domains as virtual domains. Put this router first, before all the other routers, immediately after the line ``begin routers'': virtual_domains: driver = redirect domains = lsearch;/usr/exim/vdomains data = ${lookup{$local_part}lsearch{/usr/exim/aliases-$domain}} no_more There must be no space after the semicolon in the ``domains'' line. (Remember to HUP the daemon.) ---> Create an alias file for the first virtual domain -- use vi to make the file /usr/exim/aliases-vdom1.nsrc.org containing these lines: brian: b.candler@pobox.com yourname: your email address The local parts brian and yourname should now be valid for the first virtual domain. ---> Test that Exim recognizes the virtual addresses: $ exim -bt philip@vdom1.afnog.org Please don't actually send test mail to that address -- I get too much junk already! ---> Now create a different alias file for the second virtual domain, with brian aliased to somebody else, and check (with -bt) that Exim treats that address differently. Note: It is always important to test that incorrect addresses are handled the way you want. So you need to run this test: $ exim -bt unknown@vdom1.nsrc.org Catching undeliverable mail ---> Add a redirect router that sends all undeliverable mail in your domain to the postmaster. Where in the list of routers should this go? See if you can work out how to do this on your own without looking at the answer below. Do you think that having a router like this is a good idea on a busy host? Here is a sample router that does this job: unknown_to_postmaster: driver = redirect data = postmaster It should be placed last, after all the other routers. Test it by sending mail to an unknown user. 4. Relaying from another host In section 2 above, there is test to demonstrate that relaying is blocked if you connect to your host's IP address. ---> We are now going to remove this block by changing a line in the configuration to let all the classroom hosts relay through your host. Change this line: hostlist relay_from_hosts = 127.0.0.1 to hostlist relay_from_hosts = 127.0.0.1 : xx.xx.xx.xx/mm where xx.xx.xx.xx/mm is the classroom network. (Don't forget to HUP the daemon.) Then try the telnet test from section 2 again. This time it should accept the request to relay. Ask one of the other students to try relaying through your host -- it should work. If you can, telnet from a host outside the classroom network, and confirm that relaying is still blocked. Allowing relaying to specific domains The default configuration contains the line domainlist relay_to_domains = This defines domains to which your host will relay, wherever the message comes from. As you can see, the default list is empty, so no domains match. ---> Add some domains to this line. For example, add the domain of your home email. In my case, this would be: domainlist relay_to_domains = pobox.com Now we need to test that Exim will indeed relay to those domains (but not to others) from a host that does not matchrelay_from_hosts. Exim has a testing facility that lets you simulate an SMTP call from a remote host. Run it like this: $ exim -bh 192.168.1.1 You will see some debugging output, and then an SMTP greeting line. Now type SMTP commands, waiting for a response between each one: ehlo testhost mail from:<localuser@pcnn.taller.nsrc.org> rcpt to:<user@your.home.domain> rcpt to:<user@some.other.domain> You will see the tests that Exim is making as it runs the ACL after each RCPT command. Check that it allows relaying to the right domains, and not to any others. End the SMTP session with QUIT. 5. More advanced configuration These are ideas for things to do for those who have time. Don't worry if you do not get to this part. Not everything here is covered in the lectures, but it is all in the manual and Philip Hazel's Exim 4 book. Demonstrate retry mechanisms The easiest way to demonstrate what happens when Exim cannot deliver a message is to force connections to remote hosts to fail. ---> Edit the configuration, and change the remote_smtp transport to be like this: remote_smtp: driver = smtp port = 3456 (Remember to HUP the daemon.) This makes Exim try port 3456 instead of the SMTP port (25) when delivering, causing the remote host to refuse the connection (assuming you've chosen an unused port!) ---> Send a message to a remote address and see what happens. ---> Start a queue run $ exim -q and see what happens and what gets logged. Have a look at the message's own msglog file, which you can do from the monitor or by using the -Mvl option. For example: $ exim -Mvl 19EdUm-00016A-IA (That is an example message ID; you must use the real one for the message that is on your queue.) ---> Use exinext to see when Exim is next scheduled to deliver to the host that failed: $ exinext remote.domain ---> Remember to remove the setting of port when you have finished playing with retries (and HUP the daemon). Add a system filter ---> Use vi to create a test system filter file in /usr/exim/system.filter, containing these lines: # Exim filter if $h_subject: is "spam" then save /dev/null endif ---> Arrange for Exim to use the system filter by adding these lines to the configuration (somewhere near the beginning, before the first ``begin'' line): system_filter = /usr/exim/system.filter system_filter_file_transport = address_file ---> Now send yourself a message with the subject `spam' and see what happens. Configure some address rewriting ---> Find the rewriting section of the configuration (the part that starts with ``begin rewrite''). Then add this line: othernameotherdomain.com postmasteryour.domain ---> Now send a message to othername@otherdomain.com and see what happens. ---> You can test rewriting rules with the -brw command line option: $ exim -brw othername@otherdomain.com Common mistakes These are the most common mistakes that have been made: Doing everything as root. You only need to be root to install Exim and change its configuration. Otherwise, you should do everything (including building Exim) under your normal login. In the sample commands, the command prompt is shown as # for commands that must be run as root, and $ otherwise.In particular, running email tests as root is a bad idea, because root has privileges. You want to test that Exim is working when an ordinary, unprivileged user calls it. Forgetting the dot that terminates a message. When you type a message directly into Exim, it needs a line containing just a dot to terminate it. Until you type that line, all input is taken as part of the message. Setting PATH incorrectly. Whenever you change your PATH setting, be sure to check what you have typed carefully before pressing RETURN. If you mess up with PATH, you'll find that lots of commands ``disappear''. Adding dots to email domains. You should now have got used to inserting trailing dots in fully qualified domains in DNS zones. Unfortunately, in email configurations, trailing dots are not used and will cause problems if you use them.
exim 550 relay not permitted Webuzo Squirrel Fix
exim 550 relay not permitted error can be happened when you set squirrel mail config wrong , especially if you set 'localhost' as imap / smtp server.
Solution: edit config.php to be your true dns host domain.
Change SQUIRREL MAIL CONFIG:
/home/soft/www/squirrelmail/config/config.php
edit :
to become :
$smtpServerAddress = 'smtp.yourdomain.com';
$smtpPort = 25;
$use_smtp_tls = false;
$smtp_auth_mech = 'none';
$smtp_sitewide_user = '';
$smtp_sitewide_pass = '';
$pop_before_smtp = false;
$pop_before_smtp_host = '';
$imapServerAddress = 'imap.yourdomain.com';
$imapPort = 143;
$imap_server_type = 'dovecot';
$use_imap_tls = false;
$imap_auth_mech = 'login';
whereas 192.6.162.148 is your public ip of your vps.
Solution: edit config.php to be your true dns host domain.
Change SQUIRREL MAIL CONFIG:
/home/soft/www/squirrelmail/config/config.php
edit :
to become :
$smtpServerAddress = 'smtp.yourdomain.com';
$smtpPort = 25;
$use_smtp_tls = false;
$smtp_auth_mech = 'none';
$smtp_sitewide_user = '';
$smtp_sitewide_pass = '';
$pop_before_smtp = false;
$pop_before_smtp_host = '';
$imapServerAddress = 'imap.yourdomain.com';
$imapPort = 143;
$imap_server_type = 'dovecot';
$use_imap_tls = false;
$imap_auth_mech = 'login';
Then check your DNS entry for your main domain.
should have:
imap 14400 IN A 192.6.162.148
smtp 14400 IN A 192.6.162.148
whereas 192.6.162.148 is your public ip of your vps.
Also, set Email Settings in Webuzo admin interface to use Exim smtp server
username: arealuseraccount@yourdomain.com
password: 12345678
port: 25
host: smtp.yourdomain.com
Bash find syntax
Find syntax
[icon]
This section requires expansion.(August 2008)
find [-H] [-L] [-P] path... [expression]
The three options control how the find command should treat symbolic links. The default behaviour is never to follow symbolic links. This can be explicitly specified using the -P flag. The -L flag will cause the find command to follow symbolic links. The -H flag will only follow symbolic links while processing the command line arguments. These flags are not available with some older versions of find.
At least one path must precede the expression. find is capable of interpreting wildcards internally and commands must be constructed carefully in order to control shell globbing.
Expression elements are whitespace-separated and evaluated from left to right. They can contain logical elements such as AND (-a) and OR (-o) as well as more complex predicates.
The GNU find has a large number of additional features not specified by POSIX.
[edit]POSIX protection from infinite output
Real-world filesystems often contain looped structures created through the use of hard or soft links. The POSIX standard requires that
The find utility shall detect infinite loops; that is, entering a previously visited
directory that is an ancestor of the last file encountered. When it detects an infinite
loop, find shall write a diagnostic message to standard error and shall either recover
its position in the hierarchy or terminate.
[edit]Examples
[edit]From current directory
find . -name 'my*'
This searches in the current directory (represented by the dot character) and below it, for files and directories with names starting with my. The quotes avoid the shellexpansion — without them the shell would replace my* with the list of files whose names begin with my in the current directory. In newer versions of the program, the directory may be omitted, and it will imply the current directory.
Note that for RedHat Linux Version 9: find . -name my* returns this error find: paths must precede expression. Double quotes find . -name "my*" works fine.
[edit]Files only
find . -name "my*" -type f
This limits the results of the above search to only regular files, therefore excluding directories, special files, pipes, symbolic links, etc. my* is enclosed in quotes as otherwise the shell would replace it with the list of files in the current directory starting with my...
[edit]Commands
The previous examples created listings of results because, by default, find executes the '-print' action. (Note that early versions of the find command had no default action at all; therefore the resulting list of files would be discarded, to the bewilderment of users.)
find . -name "my*" -type f -ls
This prints extended file information.
[edit]Search all directories
find / -name "myfile" -type f -print
This searches every file on the computer for a file with the name myfile and prints it to the screen. It is generally not a good idea to look for data files this way. This can take a considerable amount of time, so it is best to specify the directory more precisely. Some operating systems may mount dynamic filesystems that are not congenial to find.
[edit]Search all but one directory subtree
find / -path excluded_path -prune -o -type f -name myfile -print
This searches every folder on the computer except the subtree excluded_path (pull path including the leading /), for a file with the name myfile. It will not detect directories, devices, links, doors, or other "special" filetypes.
[edit]Specify a directory
find /home/weedly -name "myfile" -type f -print
This searches for files named myfile in the /home/weedly directory, the home directory for userid weedly. You should always specify the directory to the deepest level you can remember.
[edit]Search several directories
find local /tmp -name mydir -type d -print
This searches for directories named mydir in the local subdirectory of the current working directory and the /tmp directory.
[edit]Ignore errors
If you're doing this as a user other than root, you might want to ignore permission denied (and any other) errors. Since errors are printed to stderr, they can be suppressed by redirecting the output to /dev/null. The following example shows how to do this in the bash shell:
find / -name "myfile" -type f -print 2>/dev/null
If you are a csh or tcsh user, you cannot redirect stderr without redirecting stdout as well. You can use sh to run the find command to get around this:
sh -c find / -name "myfile" -type f -print 2>/dev/null
An alternate method when using csh or tcsh is to pipe the output from stdout and stderr into a grep command. This example shows how to suppress lines that contain permission denied errors.
find . -name "myfile" |& grep -v "Permission denied"
[edit]Find any one of differently named files
find . \( -name "*jsp" -o -name "*java" \) -type f -ls
The -ls option prints extended information, and the example finds any file whose name ends with either 'jsp' or 'java'. Note that the parentheses are required. Also note that the operator "or" can be abbreviated as "o". The "and" operator is assumed where no operator is given. In many shells the parentheses must be escaped with a backslash, "\(" and "\)", to prevent them from being interpreted as special shell characters. The -ls option and the -or operator are not available on all versions offind.
[edit]Execute an action
find /var/ftp/mp3 -name "*.mp3" -type f -exec chmod 644 {} \;
This command changes the permissions of all files with a name ending in .mp3 in the directory /var/ftp/mp3. The action is carried out by specifying the option -execchmod 644 {} \; in the command. For every file whose name ends in .mp3, the command chmod 644 {} is executed replacing {} with the name of the file. The semicolon (backslashed to avoid the shell interpreting it as a command separator) indicates the end of the command. Permission 644, usually shown as rw-r--r--, gives the file owner full permission to read and write the file, while other users have read-only access. In some shells, the {} must be quoted.
Note that the command itself should *not* be quoted; otherwise you get error messages like
find: echo "mv ./3bfn rel071204": No such file or directory
which means that find is trying to run a file called 'echo "mv ./3bfn rel071204"' and failing.
If running under Windows, don't include the backslash before the semicolon:
find . -exec grep blah {} ;
If you will be executing over many results, it is more efficient to pipe the results to the xargs command instead. xargs is a more modern implementation, and handles long lists in a more intelligent way. The print0 option can be used with this.
The following command will ensure that filenames with whitespaces are passed to the executed COMMAND without being split up by the shell. It looks complicated at first glance, but is widely used.
find . -print0 | xargs -0 COMMAND
The list of files generated by find (whilst it is being generated) is simultaneously piped to xargs, which then executes COMMAND with the files as arguments. Seexargs for more examples and options.
[edit]Delete files and directories
Delete empty files and directories and print the names
find /foo -empty -delete -print
Delete empty files
find /foo -type f -empty -delete
Delete empty directories
find /foo -type d -empty -delete
Delete files and directories (if empty) named bad
find /foo -name bad -delete
Warning: -delete should be use with other operators such as -empty or -name.
find /foo -delete (this deletes all in foo)
[edit]Search for a string
This command will search for a string in all files from the /tmp directory and below:
find /tmp -exec grep "search string" '{}' /dev/null \; -print
The /dev/null argument is used to show the name of the file before the text that is found. Without it, only the text found is printed. An equivalent mechanism is to use the "-H" or "--with-filename" option to grep:
find /tmp -exec grep -H "search string" '{}' \; -print
GNU grep can be used on its own to perform this task:
grep -r "search string" /tmp
Example of search for "LOG" in jsmith's home directory
find ~jsmith -exec grep "LOG" '{}' /dev/null \; -print
/home/jsmith/scripts/errpt.sh:cp $LOG $FIXEDLOGNAME
/home/jsmith/scripts/errpt.sh:cat $LOG
/home/jsmith/scripts/title:USER=$LOGNAME
Example of search for the string "ERROR" in all XML files in the current directory and all sub-directories
find . -name "*.xml" -exec grep "ERROR" '{}' \; -print
The double quotes (" ") surrounding the search string and single quotes (' ') surrounding the braces are optional in this example, but needed to allow spaces and other special characters in the string.
[edit]Search for all files owned by a user
find . -user <userid>
[edit]Search in case insensitive mode
find . -iname "MyFile*"
If the -iname switch is not supported on your system then workaround techniques may be possible such as:
find . -name "[mM][yY][fF][iI][lL][eE]*"
This uses Perl to build the above command for you:
echo "'MyFile*'" |perl -pe 's/([a-zA-Z])/[\L\1\U\1]/g;s/(.*)/find . -name \1/'|sh
[edit]Search files by size
Example of searching files with size between 100 kilobytes and 500 kilobytes.
find . -size +100k -a -size -500k
Example of searching empty files.
find . -size 0k
Example of searching non-empty files.
find . -not -size 0k
[edit]Search files by name and size
find /usr/src -not \( -name "*,v" -o -name ".*,v" \) '{}' \; -print
This command will search in the /usr/src directory and all sub directories. All files that are of the form '*,v' and '.*,v' are excluded. Important arguments to note are:
-not means the negation of the expression that follows
\( means the start of a complex expression.
\) means the end of a complex expression.
-o means a logical or of a complex expression.
In this case the complex expression is all files like '*,v' or '.*,v'
for file in `find /opt \( -name error_log -o -name 'access_log' -o -name 'ssl_engine_log' -o -name 'rewrite_log' -o
-name 'catalina.out' \) -size +300000k -a -size -5000000k`; do cat /dev/null > $file; done
The units should be one of [bckw], 'b' means 512-byte blocks, 'c' means byte, 'k' means kilobytes and 'w' means 2-byte words. The size does not count indirect blocks, but it does count blocks in sparse files that are not actually allocated.
[edit]Operators
Operators can be used to enhance the expressions of the find command. Operators are listed in order of decreasing precedence:
( expr ) Force precedence.
! expr True if expr is false.
-not expr Same as ! expr.
expr1 expr2 And (implied); expr2 is not evaluated if expr1 is false.
expr1 -a expr2 Same as expr1 expr2.
expr1 -and expr2 Same as expr1 expr2.
expr1 -o expr2 Or; expr2 is not evaluated if expr1 is true.
expr1 -or expr2 Same as expr1 -o expr2.
expr1 , expr2 List; both expr1 and expr2 are always evaluated. The value of expr1 is discarded; the value of the list is the value of expr2.
find . -name 'fileA_*' -or -name 'fileB_*'
This command searches files whose name has a prefix of "fileA_" or "fileB_" in the current directory.
find . -name 'foo.cpp' -not -path '.svn'
This command searches for files with the name "foo.cpp" in all subdirectories of the current directory (current directory itself included) other than ".svn".
[icon]
This section requires expansion.(August 2008)
find [-H] [-L] [-P] path... [expression]
The three options control how the find command should treat symbolic links. The default behaviour is never to follow symbolic links. This can be explicitly specified using the -P flag. The -L flag will cause the find command to follow symbolic links. The -H flag will only follow symbolic links while processing the command line arguments. These flags are not available with some older versions of find.
At least one path must precede the expression. find is capable of interpreting wildcards internally and commands must be constructed carefully in order to control shell globbing.
Expression elements are whitespace-separated and evaluated from left to right. They can contain logical elements such as AND (-a) and OR (-o) as well as more complex predicates.
The GNU find has a large number of additional features not specified by POSIX.
[edit]POSIX protection from infinite output
Real-world filesystems often contain looped structures created through the use of hard or soft links. The POSIX standard requires that
The find utility shall detect infinite loops; that is, entering a previously visited
directory that is an ancestor of the last file encountered. When it detects an infinite
loop, find shall write a diagnostic message to standard error and shall either recover
its position in the hierarchy or terminate.
[edit]Examples
[edit]From current directory
find . -name 'my*'
This searches in the current directory (represented by the dot character) and below it, for files and directories with names starting with my. The quotes avoid the shellexpansion — without them the shell would replace my* with the list of files whose names begin with my in the current directory. In newer versions of the program, the directory may be omitted, and it will imply the current directory.
Note that for RedHat Linux Version 9: find . -name my* returns this error find: paths must precede expression. Double quotes find . -name "my*" works fine.
[edit]Files only
find . -name "my*" -type f
This limits the results of the above search to only regular files, therefore excluding directories, special files, pipes, symbolic links, etc. my* is enclosed in quotes as otherwise the shell would replace it with the list of files in the current directory starting with my...
[edit]Commands
The previous examples created listings of results because, by default, find executes the '-print' action. (Note that early versions of the find command had no default action at all; therefore the resulting list of files would be discarded, to the bewilderment of users.)
find . -name "my*" -type f -ls
This prints extended file information.
[edit]Search all directories
find / -name "myfile" -type f -print
This searches every file on the computer for a file with the name myfile and prints it to the screen. It is generally not a good idea to look for data files this way. This can take a considerable amount of time, so it is best to specify the directory more precisely. Some operating systems may mount dynamic filesystems that are not congenial to find.
[edit]Search all but one directory subtree
find / -path excluded_path -prune -o -type f -name myfile -print
This searches every folder on the computer except the subtree excluded_path (pull path including the leading /), for a file with the name myfile. It will not detect directories, devices, links, doors, or other "special" filetypes.
[edit]Specify a directory
find /home/weedly -name "myfile" -type f -print
This searches for files named myfile in the /home/weedly directory, the home directory for userid weedly. You should always specify the directory to the deepest level you can remember.
[edit]Search several directories
find local /tmp -name mydir -type d -print
This searches for directories named mydir in the local subdirectory of the current working directory and the /tmp directory.
[edit]Ignore errors
If you're doing this as a user other than root, you might want to ignore permission denied (and any other) errors. Since errors are printed to stderr, they can be suppressed by redirecting the output to /dev/null. The following example shows how to do this in the bash shell:
find / -name "myfile" -type f -print 2>/dev/null
If you are a csh or tcsh user, you cannot redirect stderr without redirecting stdout as well. You can use sh to run the find command to get around this:
sh -c find / -name "myfile" -type f -print 2>/dev/null
An alternate method when using csh or tcsh is to pipe the output from stdout and stderr into a grep command. This example shows how to suppress lines that contain permission denied errors.
find . -name "myfile" |& grep -v "Permission denied"
[edit]Find any one of differently named files
find . \( -name "*jsp" -o -name "*java" \) -type f -ls
The -ls option prints extended information, and the example finds any file whose name ends with either 'jsp' or 'java'. Note that the parentheses are required. Also note that the operator "or" can be abbreviated as "o". The "and" operator is assumed where no operator is given. In many shells the parentheses must be escaped with a backslash, "\(" and "\)", to prevent them from being interpreted as special shell characters. The -ls option and the -or operator are not available on all versions offind.
[edit]Execute an action
find /var/ftp/mp3 -name "*.mp3" -type f -exec chmod 644 {} \;
This command changes the permissions of all files with a name ending in .mp3 in the directory /var/ftp/mp3. The action is carried out by specifying the option -execchmod 644 {} \; in the command. For every file whose name ends in .mp3, the command chmod 644 {} is executed replacing {} with the name of the file. The semicolon (backslashed to avoid the shell interpreting it as a command separator) indicates the end of the command. Permission 644, usually shown as rw-r--r--, gives the file owner full permission to read and write the file, while other users have read-only access. In some shells, the {} must be quoted.
Note that the command itself should *not* be quoted; otherwise you get error messages like
find: echo "mv ./3bfn rel071204": No such file or directory
which means that find is trying to run a file called 'echo "mv ./3bfn rel071204"' and failing.
If running under Windows, don't include the backslash before the semicolon:
find . -exec grep blah {} ;
If you will be executing over many results, it is more efficient to pipe the results to the xargs command instead. xargs is a more modern implementation, and handles long lists in a more intelligent way. The print0 option can be used with this.
The following command will ensure that filenames with whitespaces are passed to the executed COMMAND without being split up by the shell. It looks complicated at first glance, but is widely used.
find . -print0 | xargs -0 COMMAND
The list of files generated by find (whilst it is being generated) is simultaneously piped to xargs, which then executes COMMAND with the files as arguments. Seexargs for more examples and options.
[edit]Delete files and directories
Delete empty files and directories and print the names
find /foo -empty -delete -print
Delete empty files
find /foo -type f -empty -delete
Delete empty directories
find /foo -type d -empty -delete
Delete files and directories (if empty) named bad
find /foo -name bad -delete
Warning: -delete should be use with other operators such as -empty or -name.
find /foo -delete (this deletes all in foo)
[edit]Search for a string
This command will search for a string in all files from the /tmp directory and below:
find /tmp -exec grep "search string" '{}' /dev/null \; -print
The /dev/null argument is used to show the name of the file before the text that is found. Without it, only the text found is printed. An equivalent mechanism is to use the "-H" or "--with-filename" option to grep:
find /tmp -exec grep -H "search string" '{}' \; -print
GNU grep can be used on its own to perform this task:
grep -r "search string" /tmp
Example of search for "LOG" in jsmith's home directory
find ~jsmith -exec grep "LOG" '{}' /dev/null \; -print
/home/jsmith/scripts/errpt.sh:cp $LOG $FIXEDLOGNAME
/home/jsmith/scripts/errpt.sh:cat $LOG
/home/jsmith/scripts/title:USER=$LOGNAME
Example of search for the string "ERROR" in all XML files in the current directory and all sub-directories
find . -name "*.xml" -exec grep "ERROR" '{}' \; -print
The double quotes (" ") surrounding the search string and single quotes (' ') surrounding the braces are optional in this example, but needed to allow spaces and other special characters in the string.
[edit]Search for all files owned by a user
find . -user <userid>
[edit]Search in case insensitive mode
find . -iname "MyFile*"
If the -iname switch is not supported on your system then workaround techniques may be possible such as:
find . -name "[mM][yY][fF][iI][lL][eE]*"
This uses Perl to build the above command for you:
echo "'MyFile*'" |perl -pe 's/([a-zA-Z])/[\L\1\U\1]/g;s/(.*)/find . -name \1/'|sh
[edit]Search files by size
Example of searching files with size between 100 kilobytes and 500 kilobytes.
find . -size +100k -a -size -500k
Example of searching empty files.
find . -size 0k
Example of searching non-empty files.
find . -not -size 0k
[edit]Search files by name and size
find /usr/src -not \( -name "*,v" -o -name ".*,v" \) '{}' \; -print
This command will search in the /usr/src directory and all sub directories. All files that are of the form '*,v' and '.*,v' are excluded. Important arguments to note are:
-not means the negation of the expression that follows
\( means the start of a complex expression.
\) means the end of a complex expression.
-o means a logical or of a complex expression.
In this case the complex expression is all files like '*,v' or '.*,v'
for file in `find /opt \( -name error_log -o -name 'access_log' -o -name 'ssl_engine_log' -o -name 'rewrite_log' -o
-name 'catalina.out' \) -size +300000k -a -size -5000000k`; do cat /dev/null > $file; done
The units should be one of [bckw], 'b' means 512-byte blocks, 'c' means byte, 'k' means kilobytes and 'w' means 2-byte words. The size does not count indirect blocks, but it does count blocks in sparse files that are not actually allocated.
[edit]Operators
Operators can be used to enhance the expressions of the find command. Operators are listed in order of decreasing precedence:
( expr ) Force precedence.
! expr True if expr is false.
-not expr Same as ! expr.
expr1 expr2 And (implied); expr2 is not evaluated if expr1 is false.
expr1 -a expr2 Same as expr1 expr2.
expr1 -and expr2 Same as expr1 expr2.
expr1 -o expr2 Or; expr2 is not evaluated if expr1 is true.
expr1 -or expr2 Same as expr1 -o expr2.
expr1 , expr2 List; both expr1 and expr2 are always evaluated. The value of expr1 is discarded; the value of the list is the value of expr2.
find . -name 'fileA_*' -or -name 'fileB_*'
This command searches files whose name has a prefix of "fileA_" or "fileB_" in the current directory.
find . -name 'foo.cpp' -not -path '.svn'
This command searches for files with the name "foo.cpp" in all subdirectories of the current directory (current directory itself included) other than ".svn".
Webuzo Squirrel Mail, Exim and Dovecot Problem Fix
Webuzo Free Control Panel Notes.
Create a new email user first.
Squirrel Mail , try not using https first.
If you can not login, try to check dovecot was installed or not. If Dovecot was installed, but still can not login, try to add new DNS Zone records like this:
imap 14400 IN A 192.3.111.111
smtp 14400 IN A 192.3.111.111
save and restart named and dovecot services.
You should be able to login to Squirrel now.
Try to test sending a mail, to outside domain's mail and cc to your email too.
If you get error SMTP, SMTP server in webuzo use Exim.
Set the Email Settings menu in webuzo admin first. Use SMTP , not PHP Mail. Guides here: http://webuzo.com/wiki/Add_Email_Account
In SMTP setting:
set them like this:
username:you@yourdomain.com that you have created before
password:the password of you@yourdomain.com that you have created before
port:25
host:localhost
shoot a test mail again from inside of squirrel mail. Should have done now.
This is just a quick note when I try to install webuzo in centos 6 x64. Sorry I can not write better.
If you any questions, feel free to ask me here.
Create a new email user first.
Squirrel Mail , try not using https first.
If you can not login, try to check dovecot was installed or not. If Dovecot was installed, but still can not login, try to add new DNS Zone records like this:
imap 14400 IN A 192.3.111.111
smtp 14400 IN A 192.3.111.111
save and restart named and dovecot services.
You should be able to login to Squirrel now.
Try to test sending a mail, to outside domain's mail and cc to your email too.
If you get error SMTP, SMTP server in webuzo use Exim.
Set the Email Settings menu in webuzo admin first. Use SMTP , not PHP Mail. Guides here: http://webuzo.com/wiki/Add_Email_Account
In SMTP setting:
set them like this:
username:you@yourdomain.com that you have created before
password:the password of you@yourdomain.com that you have created before
port:25
host:localhost
shoot a test mail again from inside of squirrel mail. Should have done now.
This is just a quick note when I try to install webuzo in centos 6 x64. Sorry I can not write better.
If you any questions, feel free to ask me here.
Bash find command usage examples
One way of estimating the relative importance of the tasks that folk use Linux for would be to count the number of different applications that have been written to perform each of those tasks. Given the rather large number of programs that exist for "finding stuff", we might conclude that the thing users do most often is to lose it in the first place!
In this tutorial you'll learn how to find files on the command line by specifying all sorts of different search criteria.
Searches based on file name
The simplest kinds of search are those based on file name, and the shell's filename wildcard matching provides a starting point for this. For example, the command
$ ls *invoice*
will list all file names in the current directory containing the string invoice. Not too impressive? Why not try something like:
$ ls */*invoice*
which will list files with invoice in the name in any subdirectories of your current directory? Then you can extend the idea to whatever level you want, maybe using something like this:
$ ls *invoice* */*invoice* */*/*invoice*
If you want to search the entire file system for a file based on a file name, the slocate command provides a solution. For example,
$ slocate invoice
will find all files with names that contain the string invoice. You'll find that slocate is lightning fast because it uses a pre-built index of filenames. This index is built using the program updatedb (the slocate command with the -u option does the same thing) which is usually run once a day via cron or anacron.
On Ubuntu installations, the slocate database is /var/lib/slocate/slocate.db. This is the only down-side of slocate - it won't find files that were created since updatedb was last run.
Don't give me bad news...
Among the output from find you'll often notice a bunch of error messages relating to directories we don't have permission to search. Sometimes, there can be so many of these messages that they entirely swamp the 'good' output. You can easily suppress the error messages by redirecting them to the 'black hole' device, /dev/null. To do this, simply append 2> /dev/null to the command line.
S for secure
In case you were wondering, the s in slocate stands for 'secure'. Here's the scoop on this: the updatedb program (the one that builds the index) runs with root privilege, so it can be sure of seeing all the files. This means that potentially there will be files listed in the slocate.db index that ordinary users should not be able to see. These might be system files, or they might be private files belonging to other users.
The slocate index also keeps a record of the ownership and permissions on the files, and the slocate program is careful not to show you file names that you shouldn't be able to see. There was (I think) an older program called locate that wasn't this smart, but on a modern Linux distribution, slocate and locate are links to the same program.
Specialised search: which and whereis
There are a couple of more specialised search tools, whereis and which, that should be mentioned for the sake of completeness. The program whereis searches for the executable, source code and documentation (manual page) for a specified command. It looks in a pre-defined list of directories. For example:
$ whereis ls ls: /bin/ls /usr/share/man/man1/ls.1.gz
tells us the location the executable (binary) and the man page for the ls command. The which command is even more specialised. It simply looks up a specified command on our search path, reporting where it would first find it. For example:
$ which vi /usr/bin/vi
tells us that the vi command is in /usr/bin/vi. Effectively, this command answers the question "If I entered the command vi, which program would actually get run?"
Searching on steroids: find
At the other end of the scale is the top-of-the-range search tool, find. In addition to filename-based searching, find is able to locate files based on ownership, access permissions, time of last access, size, and much else besides. Of course, the price you pay for all this flexibility is a rather perplexing command syntax. We'll dive into the details later, but here's an example to give you the idea:
$ find /etc -name '*.conf' -user cupsys -print find: /etc/ssl/private: Permission denied find: /etc/cups/ssl: Permission denied /etc/cups/cupsd.conf /etc/cups/printers.conf
In this example, find is searching in (and below) the directory /etc for files whose name ends in .conf and that are owned by the cupsys account.
Generally, the syntax of the find command is of the form:
$ find <where to look> <what to look for> <what to do with it>
The "where to look" part is simply a space-separated list of the directories we want find to search. For each one, find will recursively descend into every directory beneath those specified. Our table below, titled Search Criteria For Find, lists the most useful search criteria (the "what to look for" part of the command).
Search criteria for find
| -name string | File name matches string (wildcards are allowed) | -name '*.jpg' |
| -iname string | Same as -name but not case sensitive | -iname '*tax*' |
| -user username | File is owned by username | -user chris |
| -group groupname | File has group groupname | -group admin |
| -type x | File is of type 'x', one of:
f - regular file
d - directory
l - symbolic link
c - character device
b - block device
p - named pipe (FIFO)
| -type d |
| -size +N | File is bigger than N 512-byte blocks (use suffix c for bytes, k for kilobytes, M for megabytes) | -size +100M |
| -size -N | File is smaller than N blocks (use suffix c for bytes, k for kilobytes, M for megabytes) | -size -50c |
| -mtime -N | File was last modified less than N days ago | -mtime -1 |
| -mtime +N | File was last modified more than N days ago | -mtime +14 |
| -mmin -N | File was last modified less than N minutes ago | -mmin -10 |
| -perm mode | The files permissions exactly match mode. The mode can be specified in octal, or using the same symbolic notation that chmod supports | -perm 644 |
| -perm -mode | All of the permission bits specified by mode are set. | -perm -ugo=x |
| -perm /mode | Any of the permission bits specified by mode is set | -perm /011 |
And the smaller table Actions For Find, below, lists the most useful actions (the "what to do with it" part of the command). Neither of these is a complete list, so check the manual page for the full story.
Actions for find
| Print the full pathname of the file to standard output | |
| -ls | Give a full listing of the file, equivalent to running ls -dils |
| -delete | Delete the file |
| -exec command | Execute the specified command. All following arguments to find are taken to be arguments to the command until a ';' is encountered. The string {} is replaced by the current file name. |
If no other action is specified, the -print action is assumed, with the result that the pathname of the selected file is printed (or to be more exact, written to standard output). This is a very common use of find. I should perhaps point out that many of the search criteria supported by find are really intended to help in rounding up files to perform some administrative operation on them (make a backup of them, perhaps) rather than helping you find odd files you happen to have mislaid.
Why is this not a command?
The which command can - occasionally - give a misleading answer, if the command in question also happens to be a built-in command of the bash shell. For example:
$ which kill /bin/kill
tells us that the kill command lives in /bin. However, kill is also a built-in bash command, so if I enter a command like
$ kill -HUP 1246
it will actually run the shell's built-in kill and not the external command.
To find out whether a command is recognised as a shell built-in, an alias, or an external command, you can use the type command, like this:
$ type kill kill is a shell builtin
Learning by Example
It takes a while to get your head around all this syntax, so maybe a few examples would help ...
Example 1 This is a simple name-based search, starting in my home directory and looking for all PowerPoint (.ppt) files. Notice we've put the filename wildcard expression in quotes to stop the shell trying to expand it. We want to pass the argument '*.ppt' directly and let find worry about the wildcard matching.
$ find ~ -name '*.ppt'
Example 2 You can supply multiple "what to look for" tests to find and by default they will be logically AND-ed, that is, they must all be true in order for the file to match. Here, we look for directories under /var that are owned by daemon:
$ find /var -type d -user daemon
Example 3 This shows how you can OR tests together rather than AND-ing them. Here, we're looking in /etc for files that are either owned by the account cupsys or are completely empty:
$ find /etc -user cupsys -or -size 0
Example 4 This uses the '!' operator to reverse the sense of a test. Here, we're searching /bin for files that aren't owned by root:
$ find /usr/bin ! -user root
Example 5 The tests that make numeric comparisons are especially confusing. Just remember that '+' in front of a number means 'more than', '-' means 'less than', and if there is no '+' or '-', find looks for an exact match. These three example search for files that have been modified less than 10 minutes ago, more than 1 year ago, and exactly 4 days ago. (This third example is probably not very useful.)
$ find ~ -mmin -10 $ find ~ -mtime +365 $ find ~ -mtime 4
Example 6 Perhaps the most confusing tests of all are those made on a file's access permissions. This example isn't too bad, it looks for an exact match on the permissions 644 (which would be represented symbolically by ls -l as rw-r--r--:
$ find ~ -perm 644
Example 7 Here we look for files that are writeable by anybody (that is, either the owner, the group, or rest-of-world). The two examples are equivalent; the first uses the traditional octal notation, the second uses the same symbolic notation for representing permissions that chmod uses:
$ find ~ -perm -222 $ find ~ -perm -ugo=w
Example 8 Here we look for files that are writeable by everybody (that is, by the owner and the group and the rest-of-world):
$ find ~ -perm /222 $ find ~ -perm /ugo=w
Example 9 So far we've just used the default -print action of find to display the names of the matching files. Here's an example that uses the -exec option to move all matching files into a backup directory. There are a couple of points to note here. First, the notation {} gets replaced by the full pathname of the matching file, and the ';' is used to mark the end of the command that follows -exec. Remember: ';' is also a shell metacharacter, so we need to put the backslash in front to prevent the shell interpreting it.
$ find ~ -mtime +365 -exec mv {} /tmp/mybackup \;
Never mind the file name, what's in the file?
As we've seen, tools such as find can track down files based on file name, size, ownership, timestamps, and much else, but find cannot select files based on their content. It turns out that we can do some quite nifty content-based searching using grep in conjunction with the shell's wildcards. This example is taken from my personal file system:
$ grep -l Hudson */* Desktop/suse_book_press_release.txt google-earth/README.linux Mail/inbox.ev-summary Mail/sent-mail.ev-summary snmp_training/enterprise_mib_list
Here, we're asking grep to report the names of the files containing a match for the string Hudson. The wildcard notation */* is expanded by the shell to a list of all files that are one level below the current directory. If we wanted to be a bit more selective on the file name, we could do something like:
$ grep -l Hudson */*.txt Desktop/search_tools.txt Desktop/suse_book_press_release.txt
which would only search in files with names ending in .txt. In principal you could extend the search to more directory levels, but in practice you may find that the number of file names matched by the shell exceeds the number of arguments that can appear in the argument list, as happened when I tried it on my system:
$ grep -l Hudson */* */*/* bash: /bin/grep: Argument list too long
A more powerful approach to content-based searching is to use grep in conjunction with find. This example shows a search for files under my home directory ('~') whose names end in .txt, that contain the string Hudson.
$ find ~ -name '*.txt' -exec grep -q Hudson {} \; -print
/home/chris/Desktop/search_tools.txt
/home/chris/Desktop/suse_book_press_release.txt
This approach does not suffer from the argument list overflow problem that our previous example suffered from. Remember, too, that find is capable of searching on many more criteria that just file name, and grep is capable of searching for regular expressions not just fixed text, so there is a lot more power here than this simple example suggests.
If you're unclear about the syntax of this example, read The truth about find, below left. In this example, the predicate -exec grep -q Hudson {} \; returns true if grep finds a match for the string Hudson in the specified file, and false if not. If the predicate is false, find does not continue to evaluate any following expressions, that is, it does not execute the -print action.
Finding a File containing a particular text string in Linux server
grep command form (syntax)
grep “text string to search” directory-path
Examples
For example search for a string called redeem reward in all text files located in /home/tom/*.txt directory, use
$ grep "redeem reward" /home/tom/*.txtTask: Search all subdirectories recursively
You can search for a text string all files under each directory, recursively with -roption:
$ grep -r "redeem reward" /home/tomTask: Only print filenames
By default, grep command prints the matching lines You can pass -H option to print the filename for each match.
Output:
$ grep -H -r “redeem reward” /home/tomOutput:
... filename.txt: redeem reward ...
To just print the filename use cut command as follows:
Output:
$ grep -H vivek /etc/* -R | cut -d: -f1Output:
... filename.txt ...
Find Files Containing Text Find files that contain a text string:
grep -lir "some text" *
The -l switch outputs only the names of files in which the text occurs (instead of each line containing the text), the -i switch ignores the case, and the -r descends into subdirectories.
Find syntax
| This section requires expansion.(August 2008) |
find [-H] [-L] [-P] path... [expression]
The three options control how the
find command should treat symbolic links. The default behaviour is never to follow symbolic links. This can be explicitly specified using the -P flag. The -L flag will cause the find command to follow symbolic links. The -H flag will only follow symbolic links while processing the command line arguments. These flags are not available with some older versions of find.
At least one path must precede the expression.
find is capable of interpreting wildcards internally and commands must be constructed carefully in order to control shell globbing.
Expression elements are whitespace-separated and evaluated from left to right. They can contain logical elements such as AND (-a) and OR (-o) as well as more complex predicates.
The GNU
find has a large number of additional features not specified by POSIX.[edit]POSIX protection from infinite output
Real-world filesystems often contain looped structures created through the use of hard or soft links. The POSIX standard requires that
Thefindutility shall detect infinite loops; that is, entering a previously visited directory that is an ancestor of the last file encountered. When it detects an infinite loop,findshall write a diagnostic message to standard error and shall either recover its position in the hierarchy or terminate.
[edit]Examples
[edit]From current directory
find . -name 'my*'
This searches in the current directory (represented by the dot character) and below it, for files and directories with names starting with my. The quotes avoid the shellexpansion — without them the shell would replace my* with the list of files whose names begin with my in the current directory. In newer versions of the program, the directory may be omitted, and it will imply the current directory.
Note that for RedHat Linux Version 9: find . -name my* returns this error find: paths must precede expression. Double quotes find . -name "my*" works fine.
[edit]Files only
find . -name "my*" -type f
This limits the results of the above search to only regular files, therefore excluding directories, special files, pipes, symbolic links, etc. my* is enclosed in quotes as otherwise the shell would replace it with the list of files in the current directory starting with my...
[edit]Commands
The previous examples created listings of results because, by default,
find executes the '-print' action. (Note that early versions of the find command had no default action at all; therefore the resulting list of files would be discarded, to the bewilderment of users.)find . -name "my*" -type f -ls
This prints extended file information.
[edit]Search all directories
find / -name "myfile" -type f -print
This searches every file on the computer for a file with the name myfile and prints it to the screen. It is generally not a good idea to look for data files this way. This can take a considerable amount of time, so it is best to specify the directory more precisely. Some operating systems may mount dynamic filesystems that are not congenial to
find.[edit]Search all but one directory subtree
find / -path excluded_path -prune -o -type f -name myfile -print
This searches every folder on the computer except the subtree excluded_path (pull path including the leading /), for a file with the name myfile. It will not detect directories, devices, links, doors, or other "special" filetypes.
[edit]Specify a directory
find /home/weedly -name "myfile" -type f -print
This searches for files named myfile in the /home/weedly directory, the home directory for userid weedly. You should always specify the directory to the deepest level you can remember.
[edit]Search several directories
find local /tmp -name mydir -type d -print
This searches for directories named mydir in the local subdirectory of the current working directory and the /tmp directory.
[edit]Ignore errors
If you're doing this as a user other than root, you might want to ignore permission denied (and any other) errors. Since errors are printed to stderr, they can be suppressed by redirecting the output to /dev/null. The following example shows how to do this in the bash shell:
find / -name "myfile" -type f -print 2>/dev/null
If you are a csh or tcsh user, you cannot redirect stderr without redirecting stdout as well. You can use sh to run the
find command to get around this:sh -c find / -name "myfile" -type f -print 2>/dev/null
An alternate method when using csh or tcsh is to pipe the output from stdout and stderr into a grep command. This example shows how to suppress lines that contain permission denied errors.
find . -name "myfile" |& grep -v "Permission denied"
[edit]Find any one of differently named files
find . \( -name "*jsp" -o -name "*java" \) -type f -ls
The
-ls option prints extended information, and the example finds any file whose name ends with either 'jsp' or 'java'. Note that the parentheses are required. Also note that the operator "or" can be abbreviated as "o". The "and" operator is assumed where no operator is given. In many shells the parentheses must be escaped with a backslash, "\(" and "\)", to prevent them from being interpreted as special shell characters. The -ls option and the -or operator are not available on all versions offind.[edit]Execute an action
find /var/ftp/mp3 -name "*.mp3" -type f -exec chmod 644 {} \;
This command changes the permissions of all files with a name ending in .mp3 in the directory /var/ftp/mp3. The action is carried out by specifying the option
-execchmod 644 {} \; in the command. For every file whose name ends in .mp3, the command chmod 644 {} is executed replacing {} with the name of the file. The semicolon (backslashed to avoid the shell interpreting it as a command separator) indicates the end of the command. Permission 644, usually shown as rw-r--r--, gives the file owner full permission to read and write the file, while other users have read-only access. In some shells, the {} must be quoted.
Note that the command itself should *not* be quoted; otherwise you get error messages like
find: echo "mv ./3bfn rel071204": No such file or directory
which means that
find is trying to run a file called 'echo "mv ./3bfn rel071204"' and failing.
If running under Windows, don't include the backslash before the semicolon:
find . -exec grep blah {} ;
If you will be executing over many results, it is more efficient to pipe the results to the xargs command instead. xargs is a more modern implementation, and handles long lists in a more intelligent way. The print0 option can be used with this.
The following command will ensure that filenames with whitespaces are passed to the executed COMMAND without being split up by the shell. It looks complicated at first glance, but is widely used.
find . -print0 | xargs -0 COMMAND
The list of files generated by
find (whilst it is being generated) is simultaneously piped to xargs, which then executes COMMAND with the files as arguments. Seexargs for more examples and options.[edit]Delete files and directories
Delete empty files and directories and print the names
find /foo -empty -delete -print
Delete empty files
find /foo -type f -empty -delete
Delete empty directories
find /foo -type d -empty -delete
Delete files and directories (if empty) named
badfind /foo -name bad -delete
Warning:
-delete should be use with other operators such as -empty or -name.find /foo -delete (this deletes all in foo)
[edit]Search for a string
This command will search for a string in all files from the /tmp directory and below:
find /tmp -exec grep "search string" '{}' /dev/null \; -print
The /dev/null argument is used to show the name of the file before the text that is found. Without it, only the text found is printed. An equivalent mechanism is to use the "-H" or "--with-filename" option to grep:
find /tmp -exec grep -H "search string" '{}' \; -print
GNU grep can be used on its own to perform this task:
grep -r "search string" /tmp
Example of search for "LOG" in jsmith's home directory
find ~jsmith -exec grep "LOG" '{}' /dev/null \; -print
/home/jsmith/scripts/errpt.sh:cp $LOG $FIXEDLOGNAME
/home/jsmith/scripts/errpt.sh:cat $LOG
/home/jsmith/scripts/title:USER=$LOGNAME
Example of search for the string "ERROR" in all XML files in the current directory and all sub-directories
find . -name "*.xml" -exec grep "ERROR" '{}' \; -print
The double quotes (" ") surrounding the search string and single quotes (' ') surrounding the braces are optional in this example, but needed to allow spaces and other special characters in the string.
[edit]Search for all files owned by a user
find . -user <userid>
[edit]Search in case insensitive mode
find . -iname "MyFile*"
If the
-iname switch is not supported on your system then workaround techniques may be possible such as:find . -name "[mM][yY][fF][iI][lL][eE]*"
This uses Perl to build the above command for you:
echo "'MyFile*'" |perl -pe 's/([a-zA-Z])/[\L\1\U\1]/g;s/(.*)/find . -name \1/'|sh
[edit]Search files by size
Example of searching files with size between 100 kilobytes and 500 kilobytes.
find . -size +100k -a -size -500k
Example of searching empty files.
find . -size 0k
Example of searching non-empty files.
find . -not -size 0k
[edit]Search files by name and size
find /usr/src -not \( -name "*,v" -o -name ".*,v" \) '{}' \; -print
This command will search in the /usr/src directory and all sub directories. All files that are of the form '*,v' and '.*,v' are excluded. Important arguments to note are:
-not means the negation of the expression that follows
\( means the start of a complex expression.
\) means the end of a complex expression.
-o means a logical or of a complex expression.
In this case the complex expression is all files like '*,v' or '.*,v'
for file in `find /opt \( -name error_log -o -name 'access_log' -o -name 'ssl_engine_log' -o -name 'rewrite_log' -o -name 'catalina.out' \) -size +300000k -a -size -5000000k`; do cat /dev/null > $file; done
The units should be one of [bckw], 'b' means 512-byte blocks, 'c' means byte, 'k' means kilobytes and 'w' means 2-byte words. The size does not count indirect blocks, but it does count blocks in sparse files that are not actually allocated.
[edit]Operators
Operators can be used to enhance the expressions of the find command. Operators are listed in order of decreasing precedence:
- ( expr ) Force precedence.
- ! expr True if expr is false.
- -not expr Same as ! expr.
- expr1 expr2 And (implied); expr2 is not evaluated if expr1 is false.
- expr1 -a expr2 Same as expr1 expr2.
- expr1 -and expr2 Same as expr1 expr2.
- expr1 -o expr2 Or; expr2 is not evaluated if expr1 is true.
- expr1 -or expr2 Same as expr1 -o expr2.
- expr1 , expr2 List; both expr1 and expr2 are always evaluated. The value of expr1 is discarded; the value of the list is the value of expr2.
find . -name 'fileA_*' -or -name 'fileB_*'
This command searches files whose name has a prefix of "fileA_" or "fileB_" in the current directory.
find . -name 'foo.cpp' -not -path '.svn'
This command searches for files with the name "foo.cpp" in all subdirectories of the current directory (current directory itself included) other than ".svn".
The truth about find
The individual components of a find command are known as expressions, (or more technically, as predicates). For example, -uname cupsys is a predicate. The find command operates by examining each and every file under the directory you ask it to search and evaluating each of the predicates in turn against that file.
Each predicate returns either true or false, and the results of the predicates are logically AND-ed together. If one of the predicates returns a false result, find does not evaluate the remaining predicates. So for example in a command such as:
$ find . -user chris -name '*.txt' -print
if the predicate -user chris is false (that is, if the file is not owned by chris) find will not evaluate the remaining predicates. Only if -user chris and -name '*.txt' both return true will find evaluate the -print predicate (which writes the file name to standard output and also returns the result 'true').
Subscribe to:
Comments (Atom)
Category
vps
bash
csf
security
backup
virtualmin
webmin
browser
ConfigServer Security Firewall
apache2
centos
control panel
dns
dovecot
firewall
hack
import firefox into linux
installation
klogd
nginx
passwordless ssh
promo vps
rsync backup
search bash find
ssh
syslog
tips
varnish
webuzo
wordpress
Suspicious Process
apache
awstats
bash pdf search
cache server
centos server
centos web panel
cheapest vps
cloudflare
compare area
compare region
cwp
date
deb
debian
discount vps
domain name
exim
file management
find and replace
google map
hosting
how to hack vps phpmyadmin 2012
ip address
ipt_recent
linux mint
map
misleading
mysql
perl
php vps info script
plugin
postfix
proxy
rdiff-backup
recursive
red hat
relay email not permitted
repo
seo
server monitor
special offer
squirrel mail
time
timezone
ubutu
vps hosting
vps info
vps monitor
vps special
vzctl
web hosting
xt_connlimit
yum