Linux: awesome helping commands

Some of the commands I often use:

rSearch all log files for the word debug case insensitive
$ grep -i debug /var/www/html/register_by_email/var/logs/*.log

Delete all files except one:
$ sudo ls | grep -v 000-default.conf | sudo xargs rm

The same but ignore errors:
$ sudo rm /etc/apache2/ssl/*.* &>/dev/null && cd /etc/apache2/sites-available/ && sudo ls | grep -v 000-default.conf | sudo xargs rm &>/dev/null && cd /etc/apache2/sites-enabled/ && sudo ls | grep -v 000-default.conf | sudo xargs rm &>/dev/null

Advanced copy of files using regular expressions: from a folder with csv files named using a date format like DK20170514.csv this command will copy only files with date from 14 may to 12 june.
$ sudo find -regextype posix-extended -regex '.*/??20170(5(1[4-9]|2[0-9])|6(0[0-9]|1[0-2]))\.csv' | xargs cp -t ../../../special/41393/ &>/dev/null

Note the .*/ in the beginning of the regex: find needs this to work!

Watch how crons work
watch -n 0.1 'ls -1 | wc -l'

Copy a log file from remote to local using ssh
scp -i ~/.ssh/abdel some-server:/var/www/html/myapp/var/logs/dev.log local_dev.log

Display end of file: useful for live debugging single or multiple log files
tail -n 4 -f var/logs/*.*

List all users
cut -d: -f1 /etc/passwd
List apache current user
ps aux | egrep '(apache|httpd)'

Change a user’s password
sudo passwd someuser

List user associated groups: ex. jenkins
grep '^jenkins:' /etc/group

List user groups
id aidrissi

List all groups
cut -d: -f1 /etc/group

Add a user to a group
usermod -a -G examplegroup exampleusername

Change owner of a folder to “me”
sudo chown -R $(whoami) /usr/local/lib/node_modules/
Use this form to change group aswell with -R for recrussive: chown {-R} [user]{:group} [file|directory]

To create a new symlink (will fail if symlink exists already):
ln -s /path/to/file /path/to/symlink

To create or update a symlink:
ln -sf /path/to/file /path/to/symlink

Test mail sending usin ‘mail’ Command:
mail -s "Test Subject" < /dev/null

Get current IP using an external server (you can use nslookup
curl ''; echo

Get current server time:
date '+%A %W %Y %X'

Chmod command: changing file permissions

chmod [who][+,-,=][permissions] filename
[who]: u=>user, g=>group, o=>others, a=>all
[+,-,=]: The + operator grants permissions whereas the – operator takes away permissions. The parameter g=u means grant group permissions to be same as the user’s.

Get the chmod numerical value for a file

stat --format '%a' file.txt

chmod -R +w,g=rw,o-rw, ~/group-project-files/
The -R option applies the modification to the permissions recursively to the directory specified and to all of its contents.

Here is an example of a file permission through octal notation that is equivalent to chmod u=rwx,go=rx.
chmod 750 ~/group-project.txt

List all environement variables

Find domain IP:
dig +noall +answer