Recently in Technical: Linux Category
Sometimes you would like a quick and easy way to export all MySQL databases from the command line to avoid all of the hassles of web interfaces and an array of tools.
From the command line you can just run this:
mysqldump —user=mysqlusername —password=yourpassword —all-databases > filename.sql
Easy.
Remember - this with everything on this site is for reference only and I’m not responsible for you etc…
Sometimes you have a csv that is massive on your server and all you want to know is the number of rows without crashing your PC’s spreadsheet package. Try this:
wc -l filename
This will output the number of lines of the file.
Try man wc for more things you can do with this simple and useful command.
This useful command will find all files where a given string, word, value etc is contained within the file. It will search recursively through all directories from where you are presently within the file system:
find RANGE -iname ‘RULES’ | xargs grep ‘STRING’ -GREP_ARGUMENTS
- RANGE = where you are searching. Using a . (full stop) will search all from where you presently are
- RULES = elements of a filename. For instance, ‘*.doc’ will only look through files ending in .doc - * is used as a wildcard
- STRING = the needle in the haystack that we are looking for
Typical Grep Arguments Used
- -s is the same as —no-messages. From man grep: “Suppress error messages about nonexistent or unreadable files.”
- -l, —files-with-matches. From man grep: “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.” Basically this just outputs the filename.
- See man grep for more
The Final Result Example (I tend to use this a lot):
find . -iname ‘*php’ | xargs grep ‘needle’ -sl
This looks for the value ‘needle’ in php files only and will only display the file names.
You may want to add the day’s date to a backup file, especially if you are running it as a cron job. The following code example will Gzip Tar the files into a compressed archive with the date inserted:
tar czvf /backups/backup-date +%y%m%d.tgz /files-to-backup/.
where:
%d = day (2 digit) %m = month (2 digit) %y = year (2 digit)
Just a shot note as this is something that I’ve been asked about recently. Hopefully the distinction I make below will help a beginner somewhere!
To restart apache from the command line, the command is (typically):
sudo /etc/init.d/apache2 restart
There are some errors that you may get whilst running various commands. Below are a list of commands, associated errors and their explanations;
/etc/init.d/apache2 restart
You may get an error for not using ‘sudo’
sudo /etc/init.d/apache2 restart
Not in sudoers file
Your user must have appropriate sudo priveledges
apache2 restart
or sudo apache2 restart
Usage: apache2 [-D name] [-d directory] [-f file]
[-C “directive”] [-c “directive”]
…etc…
Typically this error will occur if you have navigated to the /etc/init.d directory. *This will not work!*
apache2 -k restart
apache2: bad user name ${APACHERUNUSER}
You don’t need to put the -k in. Also see above if you are trying this having navigated to the /etc/init.d directory