January 1, 2010

Some useful bash scripts

svn ls -R | egrep -v -e "\/$" | xargs svn blame | awk '{print $2}' | sort | uniq -c | sort -r

Prints total line count contribution per user for an SVN repository.

I'm working in a group project currently and annoyed at the lack of output by my teammates. Wanting hard metrics of how awesome I am and how awesome they aren't, I wrote this command up.

It will print a full repository listing of all files, remove the directories which confuse blame, run svn blame on each individual file, and tally the resulting line counts. It seems quite slow, depending on your repository location, because blame must hit the server for each individual file. You can remove the -R on the first part to print out the tallies for just the current directory.

man -P cat ls > man_ls.txt

save a manpage to plaintext file. Output manpage as plaintext using cat as pager: man -P cat commandname

And redirect its stdout into a file: man -P cat commandname > textfile.txt

ls /var/lib/dpkg/info/*.list -lht |less

Find the dates your debian/ubuntu packages were installed.

ls -drt /var/log/* | tail -n5 | xargs sudo tail -n0 -f

Follow the most recently updated log files. This command finds the 5 (-n5) most frequently updated logs in /var/log, and then does a multifile tail follow of those log files. Alternately, you can do this to follow a specific list of log files:

sudo tail -n0 -f /var/log/{messages,secure,cron,cups/error_log}


find / | xargs ls -l | tr -s ' ' | cut -d ' ' -f 1,3,4,9

Command for getting the list of files with perms, owners, groups info. Useful to find the checksum of 2 machines/images.

ls -1 |grep -v .jpg |xargs rm

Delete files if not have some extension. Delete files in a directory, if those not have some extension. Useful if we want to maintain only one kind of file in our directories.

ls *tgz | xargs -n1 tar xzf

extract all tgz in current dir

man -Tps ls >> ls_manpage.ps && ps2pdf ls_manpage.ps

Convert man page to PDF. Creates a PDF (over ps as intermediate format) out of any given manpage. Other useful arguments for the -T switch are dvi, utf8 or latin1.

ls /mnt/badfs &

Check if filesystem hangs. When a fs hangs and you've just one console, even # ls could be a dangerous command. Simply put a trailing "&" and play safe

ls -1t | head -n10

find the 10 latest (modified) files
order the files by modification time, one file per output line and filter first 10

ls -s | sort -nr | more

find large files


man ls | col -b > ~/Desktop/man_ls.txt

Convert "man page" to text fil. You can convert any UNIX man page to .txt

ls -la | sort -k 5bn


Huh? Where did all my precious space go ? Sort ls output of all files in current directory in ascending order

Just the 20 biggest ones:
ls -la | sort -k 5bn | tail -n 20

A variant for the current directory tree with subdirectories and pretty columns is:
find . -type f -print0 | xargs -0 ls -la | sort -k 5bn | column -t

And finding the subdirectories consuming the most space with displayed block size 1k:
du -sk ./* | sort -k 1bn | column -t


ls | curl -F 'sprunge=<-' http://sprunge.us | xclip Run a command, store the output in a pastebin on the internet and place the URL on the xclipboard The URL can then be pasted with a middle click. This is probably useful when trying to explain problems over instant messaging when you don't have some sort of shared desktop. find ./* -ctime -1 | xargs ls -ltr --color files and directories in the last 1 hour added alias in ~/.bashrc alias lf='find ./* -ctime -1 | xargs ls -ltr --color' less -Rf <( cat <(ls -l --color=always) <(ls -ld --color=always .*) ) Scrollable Colorized Long Listing - Hidden Files Sorted Last To sort hidden files first, simply switch the two inner `ls` commands. I have this aliased to `dira` `dir` is aliased to the simpler version with no hidden files: for dir in $(ls -l | grep ^d | awk -F" " '{ print $9 }') ; do cd $dir; echo script is in $dir ; cd .. ; done perform an action in each subdirectory of the current working directory Will find directory names via `ls -l | grep ^d | awk -F" " '{ print $9 }'`, cd in them one at a time for every directory, and perform some action ( `echo script is in $dir` in the example ). The `awk -F" " '{ print $9 }'` can be written in the shorter form `awk '{ print $9 }' `, forgoing the -F" " which tells awk that the columns are separated by a single space, which is the default for awk. I like to include the -F" " to remind myself of the syntax. find / \( -name "*.log" -o -name "*.mylogs" \) -exec ls -lrt {} \; | sort -k6,8 | head -n1 | cut -d" " -f8- | tr -d '\n' | xargs -0 rm Find and delete oldest file of specific types in directory tree This works on my ubuntu/debian machines. I suspect other distros need some tweaking of sort and cut. I am sure someone could provide a shorter/faster version. for files in $(ls -A directory_name); do sed 's/search/replaced/g' $files > $files.new && mv $files.new $files; done;

Search and replace in multiple files and save them with the same names - quickly and effectively!

Yeah, there are many ways to do that.

Doing with sed by using a for loop is my favourite, because these are two basic things in all *nix environments. Sed by default does not allow to save the output in the same files so we'll use mv to do that in batch along with the sed.


p=$(netstat -nate 2>/dev/null | awk '/LISTEN/ {gsub (/.*:/, "", $4); if ($4 == "4444") {print $8}}'); for i in $(ls /proc/|grep "^[1-9]"); do [[ $(ls -l /proc/$i/fd/|grep socket|sed -e 's|.*\[\(.*\)\]|\1|'|grep $p) ]] && cat /proc/$i/cmdline && echo; done

netstat -p recoded (totaly useless..)

Ok so it's rellay useless line and I sorry for that, furthermore that's nothing optimized at all...

At the beginning I didn't managed by using netstat -p to print out which process was handling that open port 4444, I realize at the end I was not root and security restrictions applied ;p

It's nevertheless a (good ?) way to see how ps(tree) works, as it acts exactly the same way by reading in /proc

So for a specific port, this line returns the calling command line of every thread that handle the associated socket


ls | sed -n -r 's/banana_(.*)_([0-9]*).asc/mv & banana_\2_\1.asc/gp' | sh


Smart renaming

A powerfull way to rename file using sed groups.

& stand for the matched expression.

\1 referes to the first group between parenthesis. \2 to the second.

ls -S -lhr
list and sort files by size in reverse order (file size in human readable output)

This command list and sort files by size and in reverse order, the reverse order is very helpful when you have a very long list and wish to have the biggest files at the bottom so you don't have scrool up.

The file size info is in human readable output, so ex. 1K..234M...3G

Tested with Linux (Red Hat Enterprise Edition)

for img in $( ls *.CR2 ); do convert $img $img.jpg; done

Convert Raw pictures to jpg

ls /home | head -64 | barcode -t 4x16 | lpr

printing barcodes

64 elements max on 16 rows, 4 cols.

GNU Barcode will adapt automagically the width and the eight of your elements to fill the page.

Standard output format is PostScript.


find -type f -printf '%P\000' | egrep -iz '\.(avi|mpg|mov|flv|wmv|asf|mpeg|m4v|divx|mp4|mkv)$' | sort -z | xargs -0 ls -1


Show all video files in the current directory (and sub-dirs).


sed -n '/^function h\(\)/,/^}/p' script.sh

Extract a bash function

I often need to extract a function from a bash script and this command will do it.

geoip(){curl -s "http://www.geody.com/geoip.php?ip=${1}" | sed '/^IP:/!d;s/<[^>][^>]*>//g' ;}
geoip lookup

echo start > battery.txt; watch -n 60 'date >> battery.txt ; acpi -b >> battery.txt'

Battery real life energy vs predicted remaining plotted. This time I added a print to reemaining energy, every minute, time stamped.

The example shown here is complete and point to large discrepancies as time passes, converging to accuracy near the end.

for i in $(netstat --inet -n|grep ESTA|awk '{print $5}'|cut -d: -f1);do geoiplookup $i;done

Localize provenance of current established connections. Sample command to obtain a list of geographic localization for established connections, extracted from netstat. Need geoiplookup command ( part of geoip package under CentOS)


echo start > battery.txt; watch -n 60 'date >> battery.txt'

Do you really believe on Battery Remaining Time? Confirm it from time to time!

Fully recharge your computer battery and start this script.

It will create or clean the file named battery.txt, print a start on it and every minute it will append a time stamp to it.

Batteries last few hours, and each hour will have 60 lines of time stamping. Really good for assuring the system was tested in real life with no surprises.

The last time stamp inside the battery.txt file is of interest. It is the time the computer went off, as the battery was dead!

Turn on your computer after that, on AC power of course, and open battery.txt. Read the first and last time stamps and now you really know if you can trust your computer sensors.

If you want a simple line of text inside the battery.txt file, use this:

watch -n 60 'date > battery.txt'

The time of death will be printed inside

weather() { lynx -dump "http://mobile.weather.gov/port_zh.php?inputstring=$*" | sed 's/^ *//;/ror has occ/q;2h;/__/!{x;s/\n.*//;x;H;d};x;s/\n/ -- /;q';}

Show current weather for any US city or zipcode

Scrape the National Weather Service.


ord() { printf "%d\n" "'$1"; }
Get decimal ascii code from character

printf treats first char after single ' as numeric equivalent


for file in *.iso; do mkdir `basename $file | awk -F. '{print $1}'`; sudo mount -t iso9660 -o loop $file `basename $file | awk -F. '{print $1}'`; done

Make directories for and mount all iso files in a folder.

No comments:

Post a Comment