Grep from files matching a name in a directory

To look only at files matching a certain name (or part of a name) -name or -iname can be used.

-name pattern Base of file name (the path with the leading directories removed) matches shell pattern pattern. Because the leading directories are removed, the file names considered for a match with -name will never include a slash, so `-name a/b’ will never match anything (you probably need to use -path instead). The metacharacters (`*’, `?’, and `[]’) match a `.’ at the start of the base name (this is a change in findutils-4.2.2; see section STANDARDS CONFORMANCE below). To ignore a directory and the files under it, use -prune; see an example in the description of -path. Braces are not recognised as being special, despite the fact that some shells including Bash imbue braces with a special meaning in shell patterns. The filename matching is performed with the use of the fnmatch(3) library function. Don’t forget to enclose the pattern in quotes in order to protect it from expansion by the shell.

-iname pattern Like -name, but the match is case insensitive. For example, the patterns `fo*’ and `F??’ match the file names `Foo’, `FOO’, `foo’, `fOo’, etc. The pattern `*foo*` will also match a file called ‘.foobar’.

$ find /path/to/search/in/ -iname '201512*' -type f -print0 | xargs -0 grep "LookForString"
/path/to/search/in/20151209-000855.LOG:2015-12-09 08:01:15 LookForString is in this file
/path/to/search/in/20151219-001855.LOG:2015-12-19 23:15:47 LookForString is also in this file

Kill all processes with a given name

suppose we wish to kill all instances of rsync that is running
Simplest given that we know the process name

pkill rsync
ps aux|awk '/sleep/ {print "kill -9 " $1}'

Or a sligtly longer version that might(?) be more verbose as to what it does

kill -9 $(ps aux | grep '[r]sync' | awk '{print $2}')

un tar and ownership of the extracted files

If the user extracting is a “ordinary” user, the files will be owned by that user (by default).
If the user extracting is a super user, then the files ownership will be preserved (by default).

Note: This is by default and can be overridden when needed:

From the manual page of tar:

       try extracting files with the same ownership as exists in the archive (default for superuser)
       extract files as yourself (default for ordinary users)

.forward to several recipients

On *nix environments it is possible to forward the mail to one account to another user (or another email address if external mail is supported).
This is done by adding the recipient in a file called .forward in the home directory.

Example To simply forward to another address:

user@server$ echo > ~/.forward
user@server$ chmod go-w ~/.forward

In order to forward to several addresses coma (,) is used as a delimiter between the different recipients.

Example To forward to two addresses at the same time:

user@server$ echo, > ~/.forward
user@server$ chmod go-w ~/.forward

Pine: Flags

To mark a message as unread in pine this can be done using flags.
First flags have to be turned on in setup
[M]ain [S]etup [C]onfig
Then find “enable-flag-cmd” and turn it on.
After this marking a message as unread is done by “* N” in the list view or when looking at the message.

All the flags are:

  • [N]New
  • [D]Deleted
  • [*] Important
  • [A] Answered

[^T] Can be used to see all the flags and set/unset them one by one (or all).

OpenSSH and password-less logins

Using SSH you can run commands at remote servers.

By default the remote server will ask for credentials but for instance when writing a script it is not a good idea to store the login credentials in a script.
The solution to this is to create a key pair at the origin machine (where the script is) and then send this key to the remoteserver (where the script needs to login).

  • First create a key pair (only has to be done once for this machine; it can be reused for other machines if you wish to be able to login to several computers)
     jonas@jonas-desktop:~$ ssh-keygen
    Generating public/private rsa key pair.
    Enter file in which to save the key (/home/jonas/.ssh/id_rsa): 
    Enter passphrase (empty for no passphrase): 
    Enter same passphrase again: 
    Your identification has been saved in /home/jonas/.ssh/id_rsa.
    Your public key has been saved in /home/jonas/.ssh/
    The key fingerprint is:
    11:f9:5a:8f:7d:74:e4:68:3b:4b:22:1c:78:e6:be:2d jonas@jonas-desktop
    The key's randomart image is:
    +--[ RSA 2048]----+
    |        ..       |
    |        ..      .|
    |        .o     + |
    |        ..*   + o|
    |        SB = o o |
    |        . = + =  |
    |         . . + o |
    |          E.  .  |
    |          .o.    |

    Passphrase is needed to “unlock” this key before being able to use it. Leaving passphrase blank means that the key can be used without any inputs.

  • Next send the key to the server that you wish to be able to login to.
     jonas@jonas-desktop:~$ ssh-copy-id -i .ssh/ user@remoteserver
    Warning: Permanently added 'remoteserver,11.222.333.222' (RSA) to the list of known hosts.
    user@remoteserver's password: 
    Now try logging into the machine, with "ssh 'user@remoteserver'", and check in:
    to make sure we haven't added extra keys that you weren't expecting.

    -i is the identity file we created with ssh-keygen

  • This is it. Now you are able to login to remoteserver as user without a password.