Posts Tagged ‘linux’

If you have a filename or list of filenames, you may want to strip the extension. There are a few ways of “detecting” which part of the filename is the extension, but may not work if your file has multiple extensions (e.g. .tar.gz), contains spaces or periods, or meets other weird criteria. If you know […]

Imagine you have two text files, one with a list of names and another with a list of birth dates, which each name corresponding to the date of birth on the same line number in the other file, like so: names.txt Anthony Kiedis Flea Chad Smith John Frusciante dob.txt 1-Nov-1962 16-Oct-1962 25-Oct-1961 5-Mar-1970 You would […]

If you’re using a linux terminal and need to convert all upper case characters to lowercase, there are a number of ways you can do it. One of the easiest is to use tr: tr ‘[:upper:]’ ‘[:lower:]’ For example: $ echo “HeLlo WOrlD” | tr ‘[:upper:]’ ‘[:lower:]’ hello world :upper: and :lower: can be reversed […]

Reading a file line by line in Bash

Posted: 26th July 2015 by Tim in Bash, Linux
Tags: , , , , , , ,

There are a few ways to read a file in bash, each with their own caveats. If you’re looking to read a file line-by-line verbatim, including blank lines, then using a simple while loop should do the trick. For example, the following code will print the contents of a file with line numbers: line_no=0 while […]

Pause in Bash

Posted: 27th June 2015 by Tim in Bash, Linux
Tags: , , , , , , ,

Sometimes it is useful to pause a script until the user is ready to proceed. In Windows Batch scripting, this can be done with the PAUSE command. In Bash, the same thing can be done using read with the right parameters: read -rn1 -p “Press any key to continue” This command is doing the following: […]

Awk is a useful language for many command line tasks. Often awk is used on the command line on its own or with strings piped to it, but it is possible to turn that awk code into an executable script. Consider the following script. This file contains awk code with a shebang of awk -f. […]

Removing Git Tags

Posted: 26th March 2015 by Tim in Git
Tags: , , , , , , , , , , , ,

If you’ve accidentally added a git tag, or if you want to remove old tags, these tags can be removed with the following three commands: git tag -d <my_tag> git push origin :<my_tag> git push –tags For example, if you want to remove the tag bad_tag, you would use these commands: git tag -d bad_tag […]

Linux Terminal Clock

Posted: 9th March 2015 by Tim in Bash, Linux
Tags: , , , , , , , , ,

Ever wanted to have a real-time clock on your linux terminal? We can create one with a single line of bash, like so: while echo -en “$(date)\r”; do sleep 1; done Let’s look at how this works: date is a common unix tool used to print the current date and time. The $(…) means that […]

Listing directories only in linux

Posted: 9th February 2015 by Tim in Linux
Tags: , , , , , ,

The ls command will list files and directories in the current directory. But what if you only want to list the directories? There are a few ways to do this. One of the easiest ways is this: ls -d */ The -d flag instructs ls to display directories instead of displaying directory contents. The */ […]

Test if a directory is empty in Bash

Posted: 29th January 2015 by Tim in Bash, Linux
Tags: , , , , , ,

Using Bash, there are a number of ways to test if a directory is empty. One of those ways is to use ls -A to list all files, including those starting with . and see if anything is printed. This can be done like so: if [ ! “$(ls -A <path>)” ] then     echo “<path> […]

Hide grep command from ps output

Posted: 28th October 2014 by Tim in Linux
Tags: , , , ,

When searching for a running process on a *nix machine, it is common to use ps together with grep, like so: ps -ef | grep vim This works well enough for quick searches, but will always return the grep command since grep vim will always contain the string “vim”. The pgrep utility can work around […]

If you have a JAR file and want to print the details from MANIFEST.MF, this can be done with one command in linux, using the unzip utility. For example: $ unzip -p /usr/share/java/hsqldb.jar META-INF/MANIFEST.MF Manifest-Version: 1.0 Created-By: 1.7.0_03-b147 (Oracle Corporation) Specification-Title: HSQLDB Implementation-Title: Standard runtime Class-Path: /usr/share/java/servlet-api-3.0.jar Main-Class: org.hsqldb.util.SqlTool Ant-Version: Apache Ant 1.8.2 Implementation-Vendor: buildd […]

Command Line Arguments in Bash

Posted: 14th June 2014 by Tim in Bash, Linux
Tags: , , , , ,

In Bash, arguments passed in on the command line are stored in numbered variables. For example, the first argument is $1, the second argument is $2, and so on. The total number of arguments passed to the program is stored in $# $0 contains the path to the program. This path may be an absolute […]

There are many linux tools available to do search and replace, with sed being one of the most commonly used. However, tools like sed work line-by-line. If you need to replace/remove newline characters then things get complicated. It can be done with sed, but it’s not pretty. The nicest solution I’ve seen is using awk. […]

Perl regular expressions are slightly different from grep (or egrep) regular expressions. grep is sufficient most of the time, but sometimes you may need the extra flexibility of Perl regular expressions, or may just want to test out a regular expression that you will use later in Perl code. This can be done on the […]

Do until script succeeds in bash

Posted: 12th September 2013 by Tim in Bash, Linux
Tags: , , , , , ,

Occasionally you want to continue retrying a script or program until it succeeds. In bash, this can be done using the until … do … done syntax, like so: until (<command>); do echo “FAIL!”; sleep 10; done For example, if you wanted to halt processing until a program.log file was created, you could do something […]

There are a few ways to find out if a string contains a substring using bash. Below are a couple of ways this can be done without invoking any other processes. Star Wildcard One very simple method is to match strings using the * character to denote any number of other characters. For example: if […]

If you need to find the UUID of a hard disk in Linux, to add an entry to fstab for example, you can use the blkid command. There are many other methods available, but blkid prints the UUID with the label and disk type, and doesn’t require you to enter the path to the disk. […]

Gnuplot can create candlestick charts out of the box. These charts are often used for graphing financial data, with the middle block representing the difference between the open and close, and the high and low represented with lines protruding from the top and bottom of this box. The bars are colored differently depending on if […]

Show line numbers in vi or vim

Posted: 7th January 2013 by Tim in Linux, Vim
Tags: , , , , , , ,

If you want line numbers to show up in vi or vim, this can be done by typing the following command from within the program: :set number or, in shorter form: :set nu Doing this from within vi[m] will show the numbers for that session, but they will be gone next time you start up […]

This is an asy one. Let’s say, for instance, you’re living in Japan and connect to the internet via PPPoE. Your service provider informs you that you need to set your MTU to 1454, but when you connect you see it’s set to 1492. You can change this using ifconfig like so: sudo ifconfig ppp0 […]

There are various reasons you may want to set your IP address to be static, one of these being to reduce boot time by removing DHCP discovery. Doing this in Linux is quite straight forward. If you want to make your current IP address your static address, you can find the details in a few […]

Formatting a usb drive in linux

Posted: 11th July 2012 by Tim in Linux
Tags: , , , , , , , ,

So you’ve got a USB drive, and you need to reformat it. You can do this all quite easily on the command line using the fdisk utility. First, get the device location. This will be something like /dev/sdd. You want the location of the device, not a partition on the device ie: /dev/sdd instead of […]

If you want to get the last n characters of a string in bash, you can simply mix bash substrings – ${string:offset[:length]} – with bash string lengths – ${#string}. For example: str=”abcde” n=3 echo ${str:${#str} – $n} will print: cde

If you’ve got a string of items in bash which are delimited by a common character (comma, space, tab, etc) you can split that into an array quite easily. Simply (re)define the IFS variable to the delimiter character and assign the values to a new variable using the array=($<string_var>) syntax. The new variable will now […]