//Browsing archives (Page 11 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

This command will convert all .pdf files in a directory into a 800px (wide or height, whichever is smaller) image (with the aspect ratio kept) .jpg.

for file in `ls *.pdf`; do convert -verbose -colorspace RGB -resize 800 -interlace none \
 -density 300 -quality 80 $file `echo $file | sed 's/\.pdf$/\.jpg/'`; done

If the file is named 'example1.pdf' it will be named 'example1.jpg' when it is complete.

View Comments »

You can directly edit a file on a remote host with the following:

vim scp://username@host//path/to/file

Of course you can use a different editor if you *really* want to ;)

View Comments »

On my home network I often need to know which IP addresses have devices connected to them. The following one-liner will scan a given range and let you know whether each IP can be pinged.

for IP in 192.168.1.{1..10}; do if ping $IP -c 1 > /dev/null; then echo $IP alive; else echo $IP dead; fi; done

View Comments »

I often open a file and start editing only to realize later that I don't have write permissions when I get the old "E212: Can't open file for writing" error.

The vim command below can be user to save the file without the need to save it to a temp file and then copy it back again.

:w !sudo tee %

View Comments »

The command below uses the debian device driver check and report service at http://kmuto.jp/debian/hcl/index.cgi to give a report on your PCI devices.

As it mentions on the site:
* This database uses the PCI map of Debian kernel 2.6.29-1-686.
* The result does NOT guarantee your hardware works perfectly.
* This database only verifies the PCI devices at this time. X drivers, ISA, USB, IEEE1394 or any other devices are out of the focus.

curl --silent -d "check=Check&lspci=$(lspci -n)" http://kmuto.jp/debian/hcl/index.cgi | \
sed -n '/<table>/,/<\/table>/ p' | lynx -stdin

View Comments »

To push a command onto a stack for later retrieval, add this to your shell config.

bindkey '^J' push-line

This will push your command down and allow you to run something else, and after that command is completed your original one will return to the prompt.

Tested on zsh.

View Comments »

If you have a series of actions that you perform with the command line web browser lynx, it is possible to record these and then run them all again later. This can be used to record downloading a file, logging in and checking details or pretty much anything else you can do in lynx.

To record your session do the following:

$ lynx -cmd_log logfilename

This will stop recording when you quit lynx. And then to play back those actions:

lynx -accept_all_cookies -cmd_script=/path/to/logfilename

View Comments »

Ever wanted to look at a long HTML URL more easily, say to investigate a parameter. Here is a search from MapQuest.


We can split this using:

$ echo "[insert url here]" | tr "&?" "\n"

This produced for the above URL the following output.


View Comments »

The following command will allow you to copy a table from the MySQL database on a remote host, to a different MySQL database on your local host.

mysqldump -h [remote IP] -u[user] -p[pass] from_db from_table | mysql -u[user] -p[pass] target_db

The first username and password are for the remote database and the last ones are for your local database.

View Comments »

The command below can be used to convert a string into a valid filename. Uppercase letters are converted to lowercase, all difficult characters are removed and spaces are changed to underscores.

echo "#cLEa:n (%FILE%)" | tr [:upper:] [:lower:] | tr -d "[]()&~@#%^&*()_+=;:,\$" | tr " " "_"

This example will output 'clean_file'.

View Comments »

The following command will concatenate pairs of lines from a file, with a comma separating each line.

awk 'ORS=NR%2?",":"\n"' FILE

For example, if FILE is of the form
the command would print

This works by changing ORS (Output Record Seperator) using a ternary construct ( expr ? iftrue : iffalse ).
NR in awk is the current line number; % is the modulo operator, and 2 is the argument to it. Thus, if the current line number is modulo 2, the ORS is ",", and if the line number is not modulo 2, ORS is "\n".

View Comments »

The command below can be used to backup all files in a directory

IFS=$'\n';for file in *; do cp ${file}{,.bak}; done

Quick explanation:
1. Set IFS - This allows for files with spaces in their name.
2. Use all files (*) - Change this to only backup certain files.
3. Use expansion to copy each file from 'filename' to 'filename.bak'

View Comments »

This is the general form; it will run 'mycommand' once for each line of text you enter with that line of text supplying the arguments to the command:

cat | xargs -L1 mycommand

Changing to ledit adds readline support:

ledit | xargs -L1 mycommand

Example: A simple REPL for sending commands to a DBus object:

ledit | xargs -L1 -ILINE dbus-send --print-reply --dest=mydest myobj myiface.LINE

View Comments »

Use this to find core files and remove them:

find . | egrep "\/core\.[0-9]+$" | xargs rm -f

This works well as it finds only core files.

View Comments »

YouTube is great, but the embedded flash player doesn't offer a lot in the way of customizing your viewing experience. There are quite a few options for downloading flv video files from YouTube, but adding the following function to your bashrc will let you stream them directly to your choice of media player.

function mtube {
  video_id=$(curl -s $1 | sed -n "/watch_fullscreen/s;.*\(video_id.\+\)&title.*;\1;p");
  mplayer -fs $(echo "http://youtube.com/get_video.php?$video_id");

This should work with any URL of the form http://www.youtube.com/watch?v=[video id here], for example:

mtube http://www.youtube.com/watch?v=evQ0QNi2CzA

View Comments »

Tip 205 is an interesting way to show file permissions in octal using sed, but this can be done much easier using the Linux stat command.

From man page of Linux stat(1) command:
%A Access rights in human readable form
%a Access rights in octal
%n File name

stat -c '%A %a %n' *

Sample output:
-rwxr-xr-x 755 netp.sh
drwxr-xr-x 755 old_dump
-rw-r--r-- 644 tdiff.sh

View Comments »

The tar command can be used to make a quick incremental backup as follows:

tar -Pvuf backup.tar /home/user/username

This will backup the directory /home/user/username. If any changes are made to the files in this directory and the above command is run again, the files will be added/changed in the tar file. Deleted files are not removed from the archive.

An alternative method is to use find to get a list of files newer than the backup and add these to the tar using the '-r' (append) option.

find /home/user/username ! -type d -newer backup.tar -exec tar -rvf backup.tar {} \;

View Comments »

This is how we can find the length of the longest line in a file.

awk ' { if ( length > L ) { L=length} }END{ print L}' file.txt

Sample output:

We can also print the longest line along with the length:

awk ' { if ( length > L ) { L=length ;s=$0 } }END{ print L,"\""s"\"" }' file.txt

Sample output:
36 "longest line in the file is this one"

View Comments »

The command below can be used to sort the lines of a file by ascending order (shortest first). Change 'sort -n' to 'sort -nr' to sort in descending order (longest first).

cat /etc/passwd | awk '{print length, $0}' | sort -n | awk '{$1=""; print $0 }'

View Comments »

I've often seen people using the find command on a directory to get all the files, then using these with grep to find files containing a particular word. This can however, be done better just using grep:

grep --color=auto -inHR "search_for" /in/dir

greps for occurances of 'search_for' in the specified directory and below, or the current directory if none specified.


-i case insensitive
-n shows line number
-H shows file name
-R recursive

View Comments »

I often use 'screen' to run a command that I want to leave running when I logout but sometimes I forget and kick off a long job without screen. If you want to keep these things running, the following command will close the shell keeping all subprocess running

disown -a && exit

More information on the bash man page:

man bash | grep -A9 "disown \["

View Comments »

Tip 669 gave a command to copy a table between remote MySQL databases, but how about if you want to copy the whole DB to a new host?

The following command will copy the database 'old_db' from the local host to 'new_db' on the specified remote host.

mysqldump --add-drop-table --extended-insert --force --log-error=err.log -u[user] \
-p[pass] old_db | ssh -C user@host "mysql -u[user] -p[pass] new_db"

View Comments »

I wrote a small script, I named it "tw" to update twitter from terminal:

echo "Your message please..."
read MSG
echo $MSG > characters
echo "Message length"
wc -c characters
echo "Password please..."
read -s PW
wget --keep-session-cookies --http-user=your.email@address.here --http-password=$PW \
    --post-data="status=$MSG" \
echo "Message posted."

This can also be used to update identi.ca (the open twitter alternative) by replacing the twitter url with 'http://identi.ca/api/statuses/update.xml'.

View Comments »

When I read the tip yesterday (Twitter from the terminal), I thought it would be fun to set something up to tweet all the commands I enter. After a bit of playing around I have come up with the following:

First you'll need to create a script, it doesn't matter what you call it, just remember the name and path to it for later.

read MSG
echo $MSG > characters
length=$(wc -c characters | sed 's/^[^0-9]*\([0-9]*\)[^0-9]*$/\1/')
if [ $length -le 140 ]; then
  wget -q --keep-session-cookies --http-user=YOURUSER --http-password=YOURPASS \
    --post-data="status=$MSG" \

You'll need to replace YOURUSER and YOURPASS as appropriate, and if you prefer to use identi.ca then change the twitter url to https://identi.ca/api/statuses/update.xml

Next run the following command, changing details at the end for the script you just created (note this does require you to use bash):

export PROMPT_COMMAND='echo "$(history 1)" | sed "s/^ *[0-9]* *//" | /path/to/script.sh'

Now every command you type will be sent to twitter! To turn this off, just run "export PROMPT_COMMAND=''" or logoff. Of course this isn't really a good idea to do all the time, though it is fun! Also remember that Twitter will block accounts that post over 100 updates per hour.

An example of the results can be seen here: https://twitter.com/ShellStream

View Comments »

Tip 550 gives a script for a '..' command which will go up n directory levels, however the version there messes up your previous directory so "cd -" will not work. The version below fixes this problem:

.. v1.1
Usage .. [n]
Go up n-levels.
i.e.: .. 3 will go up 3 levels

function .. (){
  local arg=${1:-1};
  local dir=""
  while [ $arg -gt 0 ]; do
    arg=$(($arg - 1));
  cd $dir >&/dev/null

Another possible task is to go up several levels until we find the directory we need. The function below will do this:

... v0.1
Usage ... Thing/Some
Go up until you encounter Thing/Some, then go there
i.e.: I'm in /usr/share/X11
... src will go up to /usr, then change to /usr/src

function ... (){
  if [ -z "$1" ]; then
  local maxlvl=16
  local dir=$1
  while [ $maxlvl -gt 0 ]; do
      maxlvl=$(($maxlvl - 1));
      if [ -d "$dir" ]; then 
        cd $dir >&/dev/null

View Comments »

Home Latest Browse Top 25 Random Hall Of Fame Contact Submit