home

Quickies, by Andrea Olivato

text

Du sort by size and human readable

Quick example to improve the du command output by displaying the biggest 10 subfolder of the current folder, each with its human redable size.

	
du -k * | sort -nr | cut -f2 | xargs -d '\n' du -sh | head -n 10

Found on ubuntu forums

2 months ago

December 7, 2011
Comments (View)
text

Simple PHP updating counter on a Bash shell

If you’re running a php script on a shell and want to know what it’s doing, at which point of the script it is you can simply use the chr function to print a backspace and updating a counter while running.

Below is a quick example

	
<?php
	$howmany = 20;
	echo "I have to do $howmany cycles\n";
	for($a=0;$a<$howmany;$a++) {
		$bkspacestring = "";
		for($k=0;$k<strlen(($a-1));$k++)
			$bkspacestring .= chr(8);
		echo $bkspacestring.$a;
		sleep(1);
	}
	echo "\nDid it!\n";
?>

6 months ago

July 28, 2011
Comments (View)
text

Recursively download a #Ftp tree via #Wget

This shows how to download recursively a complete ftp tree using wget command line tool

wget -r 'ftp://USER:PASS@HOST/PATH'

2 years ago

December 30, 2009
Comments (View)
text

Bash tricks & tips

The following examples are not difficult coding examples, but just quick reminders to make working with bash easier and quicker.


# Use last argument of previous command
touch somefile
vim !$

# Monitor code execution
/bin/bash -x command 
# or declare it at the top of the script
#!/bin/bash -x

# Repeat previous command changing a world with an other
for i in $(seq 0 10); do mv somefile.somext somefile.newext; done;
^ somext ^ somotherext^

# Rename a file to file.bak
mv file{,.bak}


# Create a directory and all the needed path if not exists yet
mkdir -p /non-existing/path/to/dir

# Lists all backgrounded processes in this shell
jobs

2 years ago

August 3, 2009
Comments (View)
text

Brace expansions examples in bash

A bunch of self-explaining examples using brace

# Just echo
echo {John,Jane,Baby,Precious}
	#John Jane Baby Precious

# Combine
echo {John,Jane,Baby,Precious}Doe
	#JohnDoe JaneDoe BabyDoe PreciousDoe

# Combine with with spaces
echo {"John ","Jane ","Baby ","Precious "}Doe
	#John Doe Jane Doe Baby Doe Precious Doe

# Combine with with spaces and commas
echo {"John ","Jane ","Baby ","Precious "}Doe,
	#John Doe, Jane Doe, Baby Doe, Precious Doe,

# Double Combined
echo Dear{" John "," Jane "," Baby "," Precious "}Doe,
	#Dear John Doe, Dear Jane Doe, Dear Baby Doe, Dear Precious Doe,

# Triple multiple combination ( :) )
echo Hello{{" Mr "," Dr "}John,{" Ms "," sweet "}Jane,{" little "," cute "}Baby," Precious"}" Doe,"			
	#Hello Mr John Doe, Hello Dr John Doe, Hello Ms Jane Doe, Hello sweet Jane Doe, Hello little Baby Doe, Hello cute Baby Doe, Hello Precious Doe,

2 years ago

July 29, 2009
Comments (View)
text

Send a message to all active users on system with wall

Wall is a great utility for sysadm which allows them to send a message to all currently opened shells. Here’s a common use :

wall Rebooting server in 5 mins, please save works and get the hell out of here

Any currently connected user will receive a message like

Broadcast message from sysadm (pts/5) (Wed Jul 29 11:50:59 2009):

Rebooting server in 5 mins, please save works and get the hell out of here

2 years ago

July 29, 2009
Comments (View)
text

Split big files in bash with … split

Example on how to use split to phisically split large files in smaller ones.

ls -lh bigfile
# This is our initial situation: a large file we want to split
#-rw-r--r-- 1 foo foo 10M Jul 29 11:30 bigfile

# This will do the dirty job
# we're going to split the big file in many little ones, each of 2mb
split -b 2m bigfile part_

# Let's check what we got
ls -lh part_* 
# Will output the list of new files
#-rw-r--r-- 1 foo foo 2.0M Jul 29 11:31 part_aa
#-rw-r--r-- 1 foo foo 2.0M Jul 29 11:31 part_ab
#-rw-r--r-- 1 foo foo 2.0M Jul 29 11:31 part_ac
#-rw-r--r-- 1 foo foo 2.0M Jul 29 11:31 part_ad
#-rw-r--r-- 1 foo foo 2.0M Jul 29 11:31 part_ae

2 years ago

July 29, 2009
Comments (View)
text

Count multiple files lines with wc

Ok this is probably granted, but I didn’t know wc was able to read and count from multiple files at once.

# From standard input
cat *.pl | wc -l

# From list
wc -l file1.pl file2.pl

2 years ago

July 29, 2009
Comments (View)

Shell Commands ( via suso.suso.org )

photo Shell Commands ( via suso.suso.org )

2 years ago

July 13, 2009
Comments (View)
text

Folder size in shell using du

Simple way to retrieve a folder size in human readable format using du utility.

du -sh folder_name

2 years ago

July 13, 2009
Comments (View)