MySQL service not running on CentOS 7 system
When I issued the
mysql command on a CentOS 7 system,
I received the error message below:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111)
It took me awhile to figure out that since I was using
MariaDB, a fork of MySQL, that I needed to enter the following 3 commands
to enable, run, and secure the MariaDB service.
systemctl start mariadb.service
systemctl enable mariadb.service
mysql_secure_installation
[ More Info ]
[/software/database/mysql]
permanent link
Checking the creation and modified times for files with python
If I want to display each line in a file with a python script and at
the end of the output print the number of lines in the file, I can use
the python code below:
filename = raw_input("Enter file name: ")
with open(filename) as input_file:
for i, line in enumerate(input_file):
print line,
print "{0} line(s) printed".format(i+1)
The script will prompt me for the name of the file to be checked and
will loop through that file displaying each line from it, printing the
total number of lines at the end of the file.
The comma after the print line statement prevents a
newline from being printed,
so each file name will be printed immediately below the preceding one without
a blank line between them. If the comma was not there, a blank line would be
printed between each line containing a file name.
The {0} references the first positional argument in the
format statement, which in the case above refers to "i+1". The
.format(value) at the end of the line tells python how to format
the output. So the count of the number of lines in the file, which will be i
plus 1, will be printed after the for loop completes.
If each line in the file is a directory path and file name, e.g.,:
./security/vulnerabilities/windows/wmf-vulnerability-exploited.php
./security/vulnerabilities/windows/kb908519_embedded-web-font.php
./security/antivirus/avast/avast-ie9/index.php
./network/Internet/domains/domain-reputation-check.php
then I can use import os.path, time to import modules that
that will will allow me to obtain the time stamps for the files.
import os.path, time
filename = raw_input("Enter file name: ")
with open(filename) as input_file:
for i, line in enumerate(input_file):
print line,
line = line.rstrip('\r\n')
print "last modified: %s" % time.ctime(os.path.getmtime(line)),
print "created: %s" % time.ctime(os.path.getctime(line))
print "{0} line(s) printed".format(i+1)
Since the input file was created on a Linux system each line ends with a
newline character, which is represented by "\n". So I have to strip off the
trailing newline at the end of each file name in the input file with the
rstrip function. If the input file was created on a
Windows system, I would have to strip off a carriage return, which is
represented by "\r". By using rstrip('\r\n'), any carriage
return or newline characters will be stripped from the end of each line in
the input file, so the script will work on Mac OS, Mac OS X, Microsoft Windows,
or Unix/Linux systems.
I see output such as the following when I run the python script:
$ python checkfile.py
Enter file name: checkfiles2_php.txt
./security/vulnerabilities/windows/wmf-vulnerability-exploited.php
last modified: Mon Jan 9 15:45:00 2006 created: Tue Oct 14 10:21:03 2014
./security/vulnerabilities/windows/kb908519_embedded-web-font.php
last modified: Wed Jan 11 23:42:00 2006 created: Tue Oct 14 10:21:03 2014
./security/antivirus/avast/avast-ie9/index.php
last modified: Sat Aug 11 17:22:14 2012 created: Tue Oct 14 10:21:05 2014
./network/Internet/domains/domain-reputation-check.php
last modified: Sun Oct 6 13:30:27 2013 created: Tue Oct 14 10:21:12 2014
The creation times displayed above are the time I copied files from an old
drive to a new drive.
References:
-
python looping through input file
Date: July 30, 2013
stackoverflow
-
Python trailing comma after print executes next instruction
Date: October 24, 2010
stackoverflow
-
6.1. string — Common string operations
Python 3.4.2 documentation
-
Python string formatting: % vs. .format
Date: February 22, 2011
stackoverflow
-
How can I remove (chomp) a newline in Python?
Date: November 8, 2008
stackoverflow
-
How to get file creation & modification date/times in Python?
Date: October 25, 2008
stackoverflow
[/languages/python]
permanent link
Finding a particular string in files
On a Linux/Unix or Mac OS X system, you can use the
find and
grep commands to search for a specific text string in files.
E.g., if you wished to search all files with a ".php" file extension for
the occurrence of the word "noindex", you could use the following command:
find . -name "*.php" -exec grep "noindex" {} /dev/null \;
Since "*" has a special meaning for the shell, you will need to include it
within quotes or precede it with the backslash
escape character
as shown below:
$ find . -type f -name \*.php -exec grep -l "noindex" {} \;
If I wished to search all files, not just those ending with ".php", I
can use a command similar to the following one.
find . -type f -exec grep "noindex" {} \;
The -type f instructs find to only check regular files and
not other objects such as directory names.
If I want to send the results to an output file, I could just append a
>outputfile_name to the end of the line, but that will
also produce output indicating that the output file itself is being checked.
$ find . -type f -exec grep "noindex" {} \; >checkfiles.txt
grep: input file ‘./checkfiles.txt’ is also the output
To avoid that issue, you can use the --exclude argument.
$ find . -type f -exec grep -l --exclude checkfiles.txt "noindex" {} \; >checkfiles.txt
I can also use just the grep command, as shown belown:
$ grep -rwl . -e "noindex" --include=\*.php
The -r option tells grep to search recursively; the "." is
indicating that the search should be started in the current directory.
The -w indicates that I want exact word matches, e.g., " noindex ",
not "nonindex" or "noindexes". The -l option indicates that I
don't want to see the lines on which the word occurs, just the file names for
those files in which it is found. The -e option provides the
pattern that grep should search on and the --include option
tells grep which files it should search within.
-r, --recursive
Read all files under each directory, recursively, following
symbolic links only if they are on the command line. This is
equivalent to the -d recurse option.
-w, --word-regexp
Select only those lines containing matches that form whole
words. The test is that the matching substring must either be
at the beginning of the line, or preceded by a non-word
constituent character. Similarly, it must be either at the end
of the line or followed by a non-word constituent character.
Word-constituent characters are letters, digits, and the
underscore.
-l, --files-with-matches
Suppress normal output; instead print the name of each input
file from which output would normally have been printed. The
scanning will stop on the first match. (-l is specified by
POSIX.)
-e PATTERN, --regexp=PATTERN
Use PATTERN as the pattern. This can be used to specify
multiple search patterns, or to protect a pattern beginning with
a hyphen (-). (-e is specified by POSIX.)
--include=GLOB
Search only files whose base name matches GLOB (using wildcard
matching as described under --exclude).
References:
-
Finding a String with a Recursive Grep
Date: March 10, 2007
MoonPoint Support
-
How can I use grep to show just filenames (no in-line matches) on linux?
Date: July 9, 2011
stackoverflow
[/os/unix/commands]
permanent link