//Browsing archives (Page 12 of 13)
Latest tips by RSS
Click here to subscribe
Follow Shell-Fu on Twitter
Click here to follow
Follow Shell-Fu on identi.ca
Click here to follow

The command below will append a number to a file to count events, minutes, actions etc.

awk '{x=NR+1}END{print x}' /path/to/file >> path/to/file

Example usage:

> cat example
> awk '{x=NR+1}END{print x}' example >> example
> awk '{x=NR+1}END{print x}' example >> example
> awk '{x=NR+1}END{print x}' example >> example
> cat example

View Comments »

Function to convert UTF-8 to WIN-1251 charset.

function iconv-win1251 {
    if [ ${1%\.*} == ${1##*\.} ]; then
    iconv $1 --from-code UTF-8 --to-code WINDOWS-1251  > $newfile
    ls -l $newfile $1

View Comments »

To combine multiple pdfs into one, for printing purposes or distribution:

gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=combine.pdf -dBATCH 1.pdf 2.pdf 3.pdf

View Comments »

Recently I needed several files of certain sizes for testing the transfer speed between two machines. Files like this can easily be created with the dd command which lets you create an empty file of desired size.

$ dd if=/dev/zero of=dummy_file bs=1k count=2048
2048+0 records in
2048+0 records out
2097152 bytes (2.1 MB) copied, 0.0190557 s, 110 MB/s

$ ls -lh dummy_file
-rw-r--r-- 1 user group 2.0M 2009-05-11 10:05 dummy_file

/dev/zero is a special file that provides as many null characters (ASCII NUL, 0×00) as are read from it. In the above example, the bs option sets both input and output block size to 1k. The count option specifies 2048 input blocks, so that command will create a file (named dummy_file) with a size of 2MB (2048k).

View Comments »

Use this command to find files of a given mime-type. For example to find all PNG images in a directory or below:

find . -exec file -i {} \; | grep image/png

View Comments »

Backup your MBR:
dd if=/dev/sda of=/root/mbr.img bs=1 count=512

Restore MBR:
dd if=/root/mbr.img of=/dev/sda bs=1 count=512

Restore only bootstrap (part of MBR):
dd if=/temp/mbr.img of=/dev/sda bs=1 count=446

Restore only partition table (part of MBR):
dd if=/temp/mbr.img of=/dev/sda skip=446 seek=446 bs=1 count=64

If you want to learn more about mbr check here: http://en.wikipedia.org/wiki/Master_boot_record

View Comments »

The command below will monitor open TCP connections:

watch -n 1 "netstat -tpanl | grep ESTABLISHED"

This will show connections by processes you can view, or if done as root ("sudo netstat") all processes.

Another option to perform the same task is:

sudo lsof -i -T -n

Again watch could be used with this, or adding -r as an option will make lsof refresh the output.

View Comments »

If you're working in bash and want to quit without saving your history, you can do so using:

kill -9 $$

$$ gives the pid of the current bash instance, and the kill ends the process. There are other ways of disabling history in bash, but this can be used with no forward planning and any time/place you like.

View Comments »

To find out what files a package generated during installation.

dpkg -L packagename

We can also check the other way and find out what package a file belongs to:

dpkg -S /path/to/file

For the second command we need to use the full path as 'dpkg -S' just matches the string you supply it, so 'dpkg -S ls' matches any package that has a file with 'ls' anywhere in the filename.

View Comments »

The command below will print a ascii art graph of connections from you are making to different hosts

netstat -an | grep ESTABLISHED | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c \
 | awk '{ printf("%s\t%s\t",$2,$1) ; for (i = 0; i < $1; i++) {printf("*")}; print ""}'

You may need to check the output from "netstat -an" to check the host is in the 5th column, if not change the first "awk" to the right column number.

View Comments »

'top' is a usful command which provides a dynamic real-time view of a running system and something most people use regularly. However, a lot of people don't know that the output can be limited on a per user basis. 'top -u username' will just give output for the given user.

The following can be useful for shell scripts etc to perform 'top' for the current user:

top -u $(whoami)

View Comments »

The awk command below will print out the first line of each paragraph in a file. Paragraphs must be separated by a blank line for this to work.

awk 'NR==1||cnt-->0;/^$/{cnt=1}' file.txt

View Comments »

The commmands below show the ten largest files/dirs in the working directory. Both commands give similar results, though handle things slightly differently. The 'du' option is good if you also need sizes of subdirectories, but the 'ls' option gives more detail.

ls -laSh | head -10

du -s * | sort -nr | head -10

I find both to be useful in situations where I need to make more free space.

View Comments »

Here are a couple of ways to follow additions to a log file.

tail -f

This tails the log file and the '-f' tells tail to follow the file, so anything new added to the file will also be printed to the screen.

Another option is:

less +F /var/log/messages

The +F option turns on less 'follow mode'. It is similar to tail -f but you will have the benefits of less, like scrolling up and down. To stop tailing, use Ctrl-C and to resume it, press Shift-F.

View Comments »

The script below can be used to convert between different currencies on the command line. In order to use the script, you would enter something like "[scriptname] 150 USD GBP" to give the value of 150 US dollars in British pounds.

Supported currencies are Euro, U.S. dollar British pound, Japanese yen, Swiss franc, Canadian dollar, Australian dollar, and Indian rupee.


toUpper() { echo $@ | tr "[:lower:]" "[:upper:]"; }

if [ $# -eq 2 ]
  NUM=1;CURRENCY1=$(toUpper "$1"); CURRENCY2=$(toUpper "$2")
elif [ $# -eq 3 ]
  NUM=$1;CURRENCY1=$(toUpper "$2"); CURRENCY2=$(toUpper "$3")
  echo "Usage: $0 [number] currency1 currency2"
  echo "Ex: $0 100 EUR USD"
  echo "Available currencies: EUR, USD, GBP, JPY, CHF, CAD, AUD, INR"
  exit 1

CONVERSION=`wget -nv -O - "http://finance.google.com/finance?q=$CURRENCY1$CURRENCY2" 2>&1 | \
        grep "&nbsp;1&nbsp;$CURRENCY1&nbsp;" | \
        sed -e "s/^.*<span class=bld>&nbsp;\(.*\)&nbsp;$CURRENCY2.*$/\1/"`

if [ ${CONVERSION:-1} == "1" ]
  echo "Network error"
  RESULT=$(echo $CONVERSION \* $NUM | bc)

exit 0

View Comments »

You probably use the ps command a lot, but sometimes there is too much info, and somewhat disordered. It can be easily ordered with the forest option, as the following example shows:
$ ps -e -o pid,args --forest

View Comments »

.DS_Store files are a hidden file created by OS X to store custom attributes of a folder such as the position of icons or the choice of a background image. By default, they are created in every folder accessed, even folders on remote systems (for example, folders shared over an SMB or AFP connection). This can soon leave your shared folders littered with these files.

The command below will remove all DS_Store files from the current directory and any sub directories.

find -type f -name .DS_Store -exec rm {} \;

View Comments »

Use this command to list files that have been updated today in the current directory.

ls -l|awk '/'$(date +%Y-%m-%d)'/{print $NF}'

This method is an alternative to using find with the mtime option (see tip 199) and can be a more intuitive way of locating files modified on a specific date, for example:

ls -l|awk '/2009-04-28/{print $NF}'

'ls --full-time' can also be used and the matching criteria modified to find files modified in a particular month, hour, day, between 09:00 and 17:00 each day or anything else as required.

View Comments »

Sometimes you need to install a large amount of packages on a fresh installed system. Of course, you can install each of them manually or let a script do that but there is a much more comfortable way.

You need a file containing all packages you want to install - one package per line.

cat packages | xargs aptitude install

You can also perform this action with multiple files. This is needed when there are packages that need to be installed before other ones are installable.

for FILE in to_install/*; do
    echo Installing from $FILE
    cat $FILE | xargs aptitude install

You can prepend a number to the file names to ensure the installation order goes right.

View Comments »

alias findrandom="find -type f -exec bash -c 'test \`echo \$((RANDOM%2))\` -eq 0' \; -print"

will give you a randomized find. Each file in all subdirs will be displayed with equal probability of 0.5.

View Comments »

find -type f  | xargs -d '\n' file | grep " executable" | sed 's#\(.*\):.*#\1#' | xargs -d '\n' rm -v

delete all executable files from current directory. Useful to keep only data.

View Comments »

The command below will give a list of the packages installed on a debian system sorted from smallest to largest (the order can be reveresed by adding an 'r' option to sort - 'sort -k2 -nr').

dpkg-query --show --showformat='${Package;-50}\t${Installed-Size}\n'  | sort -k2 -n

View Comments »

Give the finger to forensics.

dd if=/dev/zero of=DELETEME; sync; rm -f DELETEME

will zero all free space on your partition. (/dev/random can be used if you're paranoid)

View Comments »

Last time, I had to install a package with a configure Script. There are a lot of parameters to adjust the installation. Unfortunately the configure doesn't end with success. A dubios error message appears and I don't find out what's wrong.
Also the modern oracle 'google' doesen't give a hint. But after hours I find a quick and easy solution:

sh -x ./configure ... configure_options ...

First there is a long rolling of letters, but afterwards, when the error appears, you can easy find out what the mistake is, because the debug modus tells you.

View Comments »

A few quick ways to empty a file of text in vi (all of these are done in command mode):


View Comments »

Home Latest Browse Top 25 Random Hall Of Fame Contact Submit