Too often I figure out how to do this or that on Linux systems and in my haste to complete whatever task I've been assigned this "How To" knowledge gets lost. So here, I will begin posting little odds and ends in hopes of not needing to re-research past efforts for Linux tips, tricks, hacks, and other good to know things. If you find yourself here and know of smaller, faster, better ways of doing any of this please let me know - only through cooperation can we all get better.
Monday, August 25, 2014
Updating the default system editor - Debian
Friday, August 8, 2014
Change Directory and list contents in single command.
Using an editor of your choosing open your .bashrc or .bash_profile and add the following lines:
# User defined functionsNote: In the above I have aliased `ll` as -> alias ll='ls -ASlh'
function cdl { cd $1 && echo $PWD && ll; }
Note: You can name this whatever you choose, I just think `cdl` made sense.
Note: You can omit the echo $PWD if you don't care to see the directory path in the output. So you could do something as simple as:
# User defined functionsSave and close and then
function cdl { cd $1 && ls; }
bkarels@rev0:~$ source ~/.bashrcNow you can use cdl (or whatever you've named it to change directory and immediately ls the contents.
Example:
bkarels@rev0:~$ cdl foo/
/home/bkarels/fooThe above example was done on Debian 7 (Wheezy) but should work on all flavors of *nix and OSX.
total 328K
-rw------- 1 bkarels bkarels 156K Aug 8 09:39 sample0
-rw------- 1 bkarels bkarels 6.9K Aug 8 08:47 someFile27
-rw-r--r-- 1 bkarels bkarels 4.1K Aug 7 12:07 sayWhat
drwx------ 6 bkarels bkarels 4.0K Aug 8 07:37 killWindows.exe
FIN
Tuesday, April 1, 2014
Quick & Dirty Add User to SUDOERs (*nix)
[myuser@machine ~]$ su -
Password:
[root@machine ~]# visudo
This will open your sudoers file for editing. visudo will protect you from some, but not nearly all mistakes. So, for this quick dirty example search for the following section:
## Allow root to run any commands anywhereThen, add the following directly below it:
root ALL=(ALL) ALL
myuser ALL=(ALL) ALLThe end result should look like:
## Allow root to run any commands anywhereNOTE: The above is separated by TABS so you would type it as follows:
root ALL=(ALL) ALL
myuser ALL=(ALL) ALL
myuser[TAB]ALL=(ALL)[TAB]ALL
Save & close (:wq)
[root@machine ~]# exit
[myuser@machine ~]$ sudo someCommand
FIN
Wednesday, May 25, 2011
mkdir and cd to created directory with a single command - 11.04 Natty
user@machine:~$ mkdir -p /dir/needed/to/cd/to/immediately
user@machine:~$ cd /dir/needed/to/cd/to/immediately
user@machine:~$ pwd
/home/user/dir/needed/to/cd/to/immediately
Quick functional solution: Open ~/.bashrc (or .bash_profile) and add the following line:
function mkdircd () { mkdir -p "$@" && eval cd "\"\$$#\""; }
NOTE: You can name this function whatever you like, mkdircd just makes sense to me...
Source ~/.bashrc and now let's do this the easy way:
user@machine:~$ mkdircd dir/needed/to/go/to/immediately
user@machine:~/dir/needed/to/go/to/immediately$ pwd
/home/user/dir/needed/to/go/to/immediately
~Fin
Monday, April 11, 2011
"Remote" administration of Linux VirtualBox® Guest Virtual Machines
Here we will go over the process of setting up your virtual machine in such a way that you can "remote" administer it and connect to it like any server on your network. For this example I will be running and OS X host with a Ubuntu Server 10.10 guest.
1. Download and install Oracle VirtualBox® here.
2. With VirtualBox® installed, let's take a look at our network settings on the host machine with `ifconfig`. Here you will want to take note of the vboxnet0 address:

3. Add a new virtual machine using the Oracle VM VirtualBox Manager. Once the initial setup is complete select the new virtual machine can click on the network adapter to add a second:

4. Adapter #1 will be configured by default and does not need to be altered. Click on the tab for `Adapter 2`, enable, and configure as follows:

5. Click the green arrow in the VM Manger to start the new virtual machine and install the new guest. (the rest of this example assumes a guest of type Ubuntu Server 10.10 but should be very similar for all flavors of *nix.)
6. During the guest install be sure to select eth0 as the primary network adapter.

7. Also, to save the need to set up the OpenSSH Server later, you can elect to do so as part of the OS install.

8. Start up the new guest and login.
9. Configure eth1 by modifying /etc/network/interfaces Add the following lines:
(NOTE: The address below is made up from the VBoxnet0 address noted in Step 2 above. Since mine defaulted to 192.168.56.1 I was free to use any value beyond that as below.)
# Secondary network interface for VBox0
auto eth1
iface eth1 inet static
address 192.168.56.102
netmask 255.255.255.0
10. Save and close /etc/network/interfaces and bring up eth1 using:
sudo ifup eth1
Your terminal might look like this when done:

11. Open a terminal on your host machine and connect to your guest using SSH:

If all went well you should be connected via SSH to your guest system and be able to interact with it as you would any server on your network. So, let's fire up a remote MongoDB server on our guest and verify that we can reach the Http Interface for the instance.
Starting up mongod on port 27000 on mongo2 (Http Interface defaults to [port] + 1000 or 28000 in this case):

...and voilà! We are connected to our "remote" instance.

Fin
Thursday, March 31, 2011
Using custom shell scripts on Linux & OSX
1. Create a `bin` directory to store your script.
machine:~ user$ mkdir ~/bin
For Linux users this will create /home/[user]/bin, for OSX you'll have /Users/[user]/bin. You could name this directory whatever you'd like, but bin seems fitting.
2. Modify .bashrc or .bash_profile to put `bin` on your path.
If you're comfortable with vi you can: (Alternatively use any text editor you are comfortable with)
machine:~ user$ vi ~/.bash_profile [OSX]
user@machine:~$ vi ~/.bashrc [Linux]
Either way, add the following line:
PATH=$PATH:$HOME/bin
3. Save and exit.
4. Source your file for it to take effect:
machine:~ user$ . ~/.bash_profile [OSX]
user@machine:~$ . ~/.bashrc [Linux]
5. Now you can drop a shell script into your bin directory and have executable from anywhere on your filesystem (don't forget to make it executable!)
Example:
Last login: Thu Mar 31 11:07:30 on ttys004
machine:~ user$ pwd
/Users/user
machine:~ user$ touch example
machine:~ user$ vi example
...
#!/bin/sh
echo "Somewhere in Russia a little girl is warming up with your max."
...
machine:~ user$ mv example ~/bin/
machine:~ user$ chmod +x ~/bin/example
machine:~ user$ ls -l ~/bin | grep example
-rwxr-xr-x 1 user staff 80 Mar 31 14:25 example
machine:~ user$ cd /tmp/
machine:tmp user$ example
Somewhere in Russia a little girl is warming up with your max.
machine:tmp user$
Fin
Lift weights - Lift spirits
Monday, March 14, 2011
Simple command line encryption
(NOTE: With OS X Lion breaking TrueCrypt - this is simple replacement that, in a way, simplifies things.)
To encrypt the file:
openssl des3 -salt -in infile.txt -out encryptedfile.txtTo decrypt the file:
openssl des3 -d -salt -in encryptedfile.txt -out normalfile.txtThe -a option, stores the encrypted file in base64 instead of binary.
To encrypt the file stored in base64:
openssl des3 -a -salt -in infile.txt -out encryptedfile.txtTo decrypt the file stored in base64:
openssl des3 -d -a -salt -in encryptedfile.txt -out normalfile.txtTo see a list of all available ciphers and other command information:
openssl -h
Other examples(notes to self...):
openssl des3 -salt -in rev27_*****_2010.zip -out rev27_*****_2010.zip.des3
openssl des3 -salt -in pf_*****_2010.zip -out pf_*****_2010.zip.des3
Monday, August 2, 2010
View output in console and write to file using tee - Mac OS X 10.6.4
machine:dir user$ ./some_program | tee output_file.txt
Executing the above command will display the output of some_program immediately in the console and write the same contents to output_file.txt.
I use this command mostly for diffs against svn, saves a step. Also, though I'm posting this as a Mac OS X terminal option, I first discovered this on Ubuntu 8.04 though I sure the history or this command is much older. I recall from the documentation to think of this in plumbing terms: Send your output down a pipe to a "T" essentially sending the same stream/flow to two different places.
~Fin
Friday, January 15, 2010
Recursive SVN Directory Removal - Ubuntu 9.10 Karmic
user@machine:~/dev/sample1$ rm -rf `find . -type d -name .svn`
(Please note those are grave accent quotes - below the tilde on the key left of the one(1) key.)
This command should work for all flavours of Unix.
~Fin
Tuesday, January 12, 2010
Create a symbolic link - Ubuntu 9.10 Karmic
So you could do like:
user@machine:~/devtools/jdk$ ll
drwxr-xr-x 10 root root 4096 2010-01-12 11:11 jdk-1.6.0
user@machine:~/devtools/jdk$ ln -s jdk-1.6.0/ current
user@machine:~/devtools/jdk$ ll
lrwxrwxrwx 1 brad brad 10 2010-01-12 11:25 current -> jdk-1.6.0/
drwxr-xr-x 10 root root 4096 2010-01-12 11:11 jdk-1.6.0
user@machine:~/devtools/jdk$
~Fin
Wednesday, October 28, 2009
Start, stop, restart MySql - Ubuntu 9.04 Jaunty
~Fin
Monday, October 26, 2009
Reset root password for MySql 5.* - Ubuntu 9.04 Jaunty
This will pop a "dialog" in your terminal and advise you to set a root password. Verify, tab to <OK> and try logging in again:
user@computer:~$ mysql -uroot -p<password_you_just_set>
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 29
Server version: 5.0.75-0ubuntu10.2 (Ubuntu)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
~Fin
Thursday, October 22, 2009
Using Tabs in the terminal - Ubuntu 9.04 Jaunty
Open a terminal and create a new tab using:
<shift> + <ctrl> + T
To switch between tabs using the keyboard:
<ctrl> + <page down>
(Moves one tab right - will loop through all tabs from left to right.)
<ctrl> + <page up>
(Moves one tab left - will loop through all tabs from right to left.)
~Fin
shred a File - Ubuntu 9.04 Jaunty
The following command will shred a file, "zero" it (to hide shredding) and then remove it.
user@machine:~$ shred -zuv sensative.file
~Fin
Wednesday, October 21, 2009
Have more visual fun - Ubuntu 9.04 Jaunty
Then open in:
System --> Preferences --> CompizConfig Settings Manager
Then:
- Draw fire on the desktop
- Use the desktop cube
- Animate maximize, minimize, and other actions.
- Other fun stuff I've yet to play with! w00t!
Monday, October 19, 2009
Find files larger than a given size in Linux - Ubuntu 9.04 Jaunty
user@machine:~$ find /path/to/directory -type f -size +1024k
This will search for files larger than 1MB (1024k). Obviously you can adjust this number to suite your search requirements.
~Fin
Wednesday, October 14, 2009
How to Create & Extract tar.gz and tar.bz2 Files in Linux - Ubuntu 9.04 Jaunty
In order to create a compressed tar.gz archive from a folder/file we need to run the following tar command:
tar czf new-tar-file-name.tar.gz file-or-folder-to-archive
Here is the command explanation:
* tar - the tar command.
* c - create new archive.
* z - compress the archive using gzip.
* f - use archive file.
* new-tar-file-name.tar.gz - the name of the tar.gz to create.
* file-or-folder-to-archive - the name of the folder we want to archive.
How to create a compressed tar.gz file from multiple files and folders in Linux?
In order to create a compressed tar.gz file from multiple files or/and folders we need to run the same tar command we used when we archived a single file/folder and to append the rest of the files/folders' names to it.
tar -czf new-tar-file-name.tar.gz file1 file2 folder1 folder2
How to extract a compressed tar.gz file in Linux?
tar -xzf tar-file-name.tar.gz
Here is the command explanation:
* tar - the tar command.
* x - extract the archive.
* z - uncompress the archive using gzip.
* f - use archive file.
* tar-file-name.tar.gz - the name of the tar.gz to extract.
The tar command will extract all the files/folders in the archive to the current directory.
How to extract a compressed tar.bz2 file in Linux?
Extracting tar.bz2 (bzip2 file) is very similar to the way you extract tar.gz file. Instead of using the -z flag you need to use the -j flag for the bzip2 format
tar -xjf tar-file-name.tar.gz
Here is the command explanation:
* tar - the tar command.
* x - extract the archive.
* j - filter the archive through bzip2
* f - use archive file.
* tar-file-name.tar.gz - the name of the tar.gz to create.
The tar command will extract all the files/folders in the archive to the current directory.
~Fin
Monday, October 12, 2009
Show line numbers in VI - Ubuntu Linux 9.04 Jaunty/Mac OS X
Update - verified to work also with Mac OS X
From within VI
:set number
To make this behaviour the default:
user@machine:~$ cd ~
user@machine:~$ touch .exrc
user@machiner:~$ vi .exrc
Add the following line to the file:
set number
user@machine:~$ . .exrc
user@machine:~$ vi someTextFile.txt
Basically, if the file .exrc does not exist in your home directory create it.
Open .exrc for editing.
Add the line (w/o quotes) "set number"
Save .exrc
Source .exrc
Test to verify function.
Update (as I've had to look this up twice now...)
To turn off line numbers from withing VI
:set nonumber
~Fin
How to start, stop, & restart samba - Ubuntu Linux 9.04 Jaunty
sudo /ect/init.d/samba start
sudo /ect/init.d/samba restart
~Fin