Useful bash commands

Search for files (for example filename.ext) from the root directory:

sudo find / -name filename.ext

Remove directory and its content, without prompts:

sudo rm –rf DIR_NAME

Recursively change the group of a file or folder:

sudo chgrp -R GROUP_NAME PATH/TO/FILE/FOLDER

Recursively change the owner of a file or folder:

sudo chown -R USER_NAME DIRECTORY/FILENAME

Recursively change the owner and the group of a file or folder:

sudo chown -R USER_NAME:GROUP_NAME DIRECTORY/FILENAME

Lists the content of the working directory in long format:

ls –l

...Results of the above command you look something like the following:

total 72
-rw-r--r-- 1 root root  9677 Apr 10 01:53 apache2.conf
drwxr-xr-x 2 root root  4096 Apr 25 23:08 conf.d
-rw-r--r-- 1 root root  1465 Mar  4 21:00 envvars
-rw-r--r-- 1 root root 31063 Oct 21  2012 magic
drwxr-xr-x 2 root root  4096 Apr 10 01:16 mods-available
drwxr-xr-x 2 root root  4096 Apr 10 14:35 mods-enabled
-rw-r--r-- 1 root root   773 Apr 28 21:15 ports.conf
drwxr-xr-x 2 root root  4096 Apr 26 16:20 sites-available
drwxr-xr-x 2 root root  4096 Apr 29 18:31 sites-enabled

The leading "d" signifies a directory, while "-" is a file and "l" is a link. The next 3 characters are the rights of the user (u), the next 3 are for the group (g) and the last 3 is that of others (o).

r = read, w = write, x = execute and - signifies no-rights (e.g. r-x is read, no write, execute).

The following removes execute rights from user:

chmod u-x DIRECTORY/FILENAME

The following gives others execute rights:

chmod o+x DIRECTORY/FILENAME

The following recursively gives others rights to write and execute:

chmod -R o+rw DIRECTORY/FILENAME

The following recursively removes read and execute rights for group:

chmod -R g-rx DIRECTORY/FILENAME

With the following All get read, write and execute rights:

chmod a=+rwx file

Use the following command to delete all lof files:

sudo rm `find /var/log -type f`

Using Nano, to delete entire content of a file, set cursor at the beginning of the file. Hold CTRL and 6 to set a mark. ALT and SHIFT and T (or try ALT and T) to cut to the end of the file.

The following restarts the cron daemon:

sudo /etc/init.d/cron restart