Archive for the 'Linux' Category

How to Peek at Top, Middle, or Bottom of Text File Without Opening It in Linux

Let’s say you have a really large text file. By large, I mean it has a lot of lines of text in the file. I frequently work with files that have millions or billions of lines of text in them. If I try to open the file in a text editor, it usually takes a [...] Read more »

How to echo a Tab or Other Special Characters in Linux

I’m working on a bash script that I want to insert some text into a file. So I’m using the echo command to do this, like so:

echo “hello, world” > myfile.txt

This works just great. Now I want to insert a special character into the file. So I need to do two things: 1) escape special [...] Read more »

Generate a Random String in Linux

It’s not too hard to generate a random number at the command line in Linux. You can use $RANDOM. But if you want to generate a random string, it is a bit more obscure. The best solution I found (after searching for longer than expected) was the following:

openssl rand -base64 30

This technique uses the openssl [...] Read more »

How to Extract Substring in Bash

Let’s say you have a bash variable:

x=abc123def

And you want to get everything in the substring that comes before “def.” You can use awk to do this, but it’s a bit “awk”ward (though very flexible and powerful in general). An easy solution is to do the following:

y=${x%def*}
echo $y # Should print "abc123"

This approach should work on [...] Read more »

Splitting a String into Array and Accessing Contents by Index in bash

Say you have a variable in a bash script that looks like this:

x="abc_123_def"

Now say you want to split this variable into an array. The character you will use to split it is the underscore (_). So you want to end up with an array of three objects: abc, 123, and def. Then you want to [...] Read more »

How to Suppress Error Message When Attempting to Recreate Directory in Linux

Let’s say you have a script that creates a directory as a preliminary step to some type of task that you are performing. If the directory doesn’t exist, you want the script to create the directory. You don’t want to have to modify the script after the directory has been created. If the directory already [...] Read more »

Troubleshooting Password-less SSH Login on UNIX Systems

There’s a nice way of setting up a UNIX-based server so you can log in without a password. This technique uses public/private key encryption. You set it up one time on your client machine and one time on each server you want to connect to. Then you can SSH into the server without using a [...] Read more »

Creating a Shortcut to Open Terminal in Specific Directory in Ubuntu

I finally made the leap to doing most of my research on computers that run on Linux. The distribution I’m using is Ubuntu. So far I’ve found it quite good. One thing I do frequently is open a Terminal (command) window in a specific directory. However, when you open the Terminal, it by default opens [...] Read more »

Solving “cannot find xml2-config” on Ubuntu 9.10

I was trying to install some packages in R recently that depended on the XML package. I used the nifty install.packages() command, but I was getting an error:
“cannot find xml2-config”
This error was preventing these other packages from being installed properly. It turns out that I needed to install a library called libxml2-dev. In my case, [...] Read more »

Overcoming cygwin “Error: no display specified”

I installed cygwin, which allows you to execute Linux-based applications on your Windows machines. But what I wanted to do was connect (using ssh) to a Linux box and run an application that has a GUI from there. In this case, I wanted to be able to connect to server and run firefox from the [...] Read more »

Troubleshooting Cygwin Error: “ssh command not found”

Cygwin is a software tool that allows you to run Linux programs in Windows. I have had good success in using it to run commands locally and to connect to other servers via ssh, scp, etc. I just got a new computer and installed the latest version of Cygwin, but when I tried to ssh [...] Read more »

Read Contents of Text File Into a Variable in a Bash Script

I’m learning something new about Linux every day, so this is something that will probably be obvious to many of you out there. But I needed today to read the contents of a file into a variable in a bash script. The simple way to go about doing this is to use the following command:

myvar=`cat [...] Read more »

Execute a Command-line Script in Another Directory in Linux

I am running an application in one location on the file system, and I need to be able to invoke an application that is in a different directory on the file system. Due to personal preference, this other directory is not specified in the PATH variable. So I’m wondering how I can get Linux to [...] Read more »

How to Run a Simple Command at Startup in Linux

In Linux, you sometimes want to run a command when the server first starts up. I was doing this recently where I would have to manually go in and run a command each time the server got rebooted. This was a pain, so with a little help from my friend Martin, I learned how to [...] Read more »

Passing Command-Line Arguments to Bash Scripts

I’m sure this one will be obvious to many readers, but it wasn’t to me, so I’m going to share it. A bash script is a file that you can run at the command line in Linux/Unix environments to automate something. In my case, I have a Java program that I need to run over [...] Read more »

Invoke External Application in Java

I recently had a scenario where part of what I wanted to do was in Java, and the other part was in an application that was written in Python. Rather than than rewrite my entire code base in one language or the other, I wanted to find a (quick and dirty) way to invoke the [...] Read more »

How to Increase the Priority of a Process in Linux

In Linux, you have the ability to determine which processes are running at a given time on the server. This post explains how to do this using the top command.
Let’s say you have a process that is not getting as much CPU time as you would like, maybe because it is getting beat out by [...] Read more »

Which Linux Operating System Version Are You Running?

Linux comes in many flavors. If you did not perform the installation (or forgot), you may need to find out which flavor is being run on a given box. In Linux, it is pretty easy to do this. Simply type the following command at the command line, and it will give detail about it:

cat /proc/version Read more »

How to Encrypt and Decrypt Files in Python

If you want to be able to encrypt and decrypt files with Python, you might check out the following sites:
http://www.dlitz.net/software/pycrypto/
http://www.amk.ca/python/code/crypto.html
However, if you know you’re going to only be using Linux, I recommend doing file encryption as explained in this post. You can invoke commands at the command line by following the explanation here. Read more »

Login as a Different User in Linux

Say you log in to a Linux machine but then want to switch to a different user. You can do this without logging out as the first user. A time when this might come in handy is when you are logged in as a regular user but want to install some software as the root [...] Read more »