Linux

rsh

rsh is a different command in Unix and Linux. In Unix this is restrictive shell.

Lines in file that do not match

http://www.uccs.edu/~ahitchco/grep/

grep -vn "boo" a_file

The number of arguments supplied to a script

http://www.tutorialspoint.com/unix/unix-special-variables.htm

$#

File that runs command when login

The file /etc/profile is maintained by the system administrator of your UNIX machine and contains shell initialization information required by all users on a system.

/etc/profile

http://www.tutorialspoint.com/unix/unix-environment.htm

Send content of a file

http://www.cyberciti.biz/faq/email-howto-send-text-file-using-unix-appleosx-bsd/

mail -s 'Subject-Here' you@cyberciti.biz < input.file

Select an arbitrary column of text in UNIX

http://www.tech-recipes.com/rx/161/select-an-arbitrary-column-of-text-in-unix/

cut -d : -f 5 /etc/passwd

-d is for delimeter.

Global Bash Variables

http://www.thegeekstuff.com/2010/05/bash-variables/

Global variables are also called as environment variables, which will be available to all shells. printenv command is used to display all the environment variables.

Find and delete files in a given directory recursively

find . -name '*.pyc' -delete

Make user sudoer (admin)

adduser <username> sudo

Ignore errors in Makefile

http://www.gnu.org/software/make/manual/html_node/Errors.html#Errors

- in the beginning of the line to ignore errors @ to suppress output

     clean:
             -rm -f *.o

Crontab to pull wiki each hour

To edit the list of cronjobs you can run:

crontab -e

My crontab to pull my wiki every 15th minute of each hour:

15 * * * * cd $HOME/projects/wiki/ && git pull > /dev/null 2>&1

http://kevin.vanzonneveld.net/techblog/article/schedule_tasks_on_linux_using_crontab/

To list all scheduled commands:

chrontab -l

Note: not sudo crontab -e, since it is for a root user. I am not sure if it has permissions to pull from Github.

Kill several processes

/grepwww.cyberciti.biz/faq/kill-process-in-linux-or-terminate-a-process-in-unix-or-linux-systems/

kill -9 `ps ux | awk ‘/java/ && !/awk/ {print $2}’`

Find and remove

Find and remove all files that have (1).jpg in them with confirmation from a user.

find -name *(1).jpg -exec rm -i {} \;

Compress files under Linux

http://www.cyberciti.biz/howto/question/general/compress-file-unix-linux-cheat-sheet.php

tar -zcvf {.tgz-file} {files}
tar -zcvf pics.tar.gz *.jpg *.png

Redirect stdout and stderr to file

http://www.cyberciti.biz/faq/redirecting-stderr-to-stdout/

  $ command-name &>file

Shutdown (turn off) computer over SSH

http://www.cyberciti.biz/faq/shutdown-ubuntu-linux-computer/

sudo shutdown -h now

Symbolic link a new app

ln -s ../registration/ registration

Ruby on Bluehost

http://giangnd.wordpress.com/2010/10/25/install-and-use-your-custom-gems-on-bluehost/

In ~/.bashrc:

export GEM_HOME=$HOME/ruby/gems
export GEM_PATH=$GEM_HOME:/usr/lib64/ruby/gems/1.8
export GEM_CACHE=$GEM_HOME/cache
export PATH=$PATH:$HOME/ruby/gems/bin

Compare files

http://tldp.org/LDP/abs/html/filearchiv.html

cmp $1 $2 &> /dev/null  # /dev/null buries the output of the "cmp" command.
#   cmp -s $1 $2  has same result ("-s" silent flag to "cmp")
#   Thank you  Anders Gustavsson for pointing this out.
#
# Also works with 'diff', i.e.,   diff $1 $2 &> /dev/null

if [ $? -eq 0 ]         # Test exit status of "cmp" command.
then
  echo "File \\"$1\\" is identical to file \\"$2\\"."
else  
  echo "File \\"$1\\" differs from file \\"$2\\"."
fi

Add a user on Ubuntu

http://www.howtogeek.com/howto/ubuntu/add-a-user-on-ubuntu-server/

You can use the -d option to set the home directory for the user. The -m option will force useradd to create the home directory. We’ll try creating a user account with those options, and then use the passwd command to set the password for the account. You can alternatively set a password using -p on the useradd command, but I prefer to set the password using passwd.

sudo useradd -d /home/testuser -m testuser
sudo passwd testuser

Skype in Ubuntu 11.10

echo foreign-architecture i386 | sudo tee /etc/dpkg/dpkg.cfg.d/multiarch
sudo apt-get install libxss1:i386 libqtcore4:i386 libqt4-dbus:i386
sudo apt-get remove --purge skype
sudo apt-get install skype:i386

ok, here's an interesting twist.... Scribus works when I run it as root with gksudo scribus-ng

I used sudo skype.

SSH login without password

via http://linuxproblem.org/art_9.html

To login from A to B without a password:

a@A:~> ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/a/.ssh/id_rsa): 
Created directory '/home/a/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/a/.ssh/id_rsa.
Your public key has been saved in /home/a/.ssh/id_rsa.pub.
The key fingerprint is:
3e:4f:05:79:3a:9f:96:7c:3b:ad:e9:58:37:bc:37:e4 a@A

Now use ssh to create a directory ~/.ssh as user b on B. (The directory may already exist, which is fine):

a@A:~> ssh b@B mkdir -p .ssh
b@B's password: 

Finally append a's new public key to b@B:.ssh/authorized_keys and enter b's password one last time:

a@A:~> cat .ssh/id_rsa.pub | ssh b@B 'cat >> .ssh/authorized_keys'
b@B's password: 

Depending on your version of SSH you might also have to do the following changes:

  • Put the public key in .ssh/authorized_keys2
  • Change the permissions of .ssh to 700
  • Change the permissions of .ssh/authorized_keys2 to 640

From now on you can log into B as b from A as a without password:

a@A:~> ssh b@B hostname
B
Last edited by Artem Dudarev, 2013-10-13 02:01:37. Edit