Archive for the ‘bash’ Category

ménage de printemps (spring cleaning)

March 22, 2014 Leave a comment

My Dropbox folder was at 98.5%, so it was time to do some cleanup. Which directories are the largest? Which files are the largest?


alias top10dirs='du -hsx * | sort -rh | head -10'
alias top10files='find . -type f -print0 | du -h --files0-from=- | sort -hr | head -n 10'

The first one shows the top 10 largest directories, while the second one prints the top 10 largest files. Directory and file sizes are shown in a human-readable format.


$ top10dirs 
60M     20090629-deploy
60M     20090327-deploy
56M     kgm
55M     exist-deploy-v3-20100710
55M     exist-deploy-v3-20100521
$ top10files 
60M     ./20090629-deploy/
60M     ./20090327-deploy/
55M     ./exist-deploy-v3-20100710/
55M     ./exist-deploy-v3-20100521/
49M     ./exist-deploy-v3-20100409/


  • top10dirs is from here
  • for top10files I wrote a Python script, but reddit user farsass pointed out that it can be solved easier in the shell

Find the largest subdirectories

March 21, 2014 Leave a comment

The free space on your HDD is low. Which directories are the largest? What consumes so much space?

Install “ncdu“, which stands for NCurses Disk Usage.

ncdu (NCurses Disk Usage) is a curses-based version of the well-known ‘du’, and provides a fast way to see what directories are using your disk space.” (source: man)

For a command line solution, check out this post: How Do I Find The Largest Top 10 Files and Directories On a Linux / UNIX / BSD?

Categories: bash Tags: , , , ,

extract .tar.gz

February 15, 2014 Leave a comment
$ tar xvJf file.tar.xz
# or
$ tar xvf file.tar.xz
# or
$ tar --xz -xvf file.tar.xz

Why is *.tar.gz still much more common than *.tar.xz?

Categories: bash Tags:

bittorrent client from the command line

January 14, 2014 Leave a comment


sudo apt-get install transmission-cli


$ transmission-cli  -w <download_dir>  <file|url|magnet>
Categories: bash Tags: ,

print the content of a file with line numbers

November 4, 2013 Leave a comment
cat -n file.txt
Categories: bash Tags: ,

run a script as another user without password

October 8, 2013 Leave a comment

You can run a program/script/command as another user the following way (example):

sudo -u www-data /bin/date

That is: /bin/date is executed in the name of www-data and you get the output. However, it asks for your password.

Question: how to execute the command above without a password check?


Create the file /etc/sudoers.d/date_test :

jabba ALL=(www-data) NOPASSWD: /bin/date

Meaning: allow the user “jabba” to execute “/bin/date” in the name of “www-data” and ask no password.

You should read /etc/sudoers.d/README, it contains important pieces of information:

  • the file you create cannot contain ‘~‘ or ‘.
  • the file must have 0440 rights
  • the command at the end of the lines must have absolute path

Tip from here.

Categories: bash, security Tags: , ,

setfacl / getfacl

October 8, 2013 Leave a comment

With ACL (Access Control List) you can set exactly who can access your files and directories. With ACL you can set things like “nobody can read this file except user XY”, or “no one can write this directory except the user Z”.

With setfacl you can set the ACL rights. With getfacl you can ask the ACL rights of a file/folder.

Example #1
You have a pmwiki installation that runs on an Apache webserver. PmWiki has a directory called “wiki.d” that must be writable too, otherwise you cannot edit your wiki from a browser. Behind the scenes it’s Apache’s www-data user who wants to write in this directory.

A naive approach is to “chmod 777 pmwiki/wiki.d”. In this case anyone with a shell access to the server can modify the content of this folder.

A better way is to give the necessary grants to Apache’s www-data user:

setfacl -R -m u:www-data:rwx $HOME/public_html/pmwiki/wiki.d

Thanks to Jeszy for the tip.

Example #2
You have a web application that uses an SQLite database. Again, the www-data user would like to write into it. In addition, www-data must be able to write to the directory too that contains the database file.

$ cd /home/jabba/public_html/myapp
# say we have here an sqlite.db file
$ setfacl -m u:www-data:rw sqlite.db
$ setfacl -m u:www-data:rwx .

To grant rights to a group, use “g:groupid:rights” instead of “u:userid:rights“.

Categories: bash, security, ubuntu Tags: , ,

mkdir && cd

September 24, 2013 Leave a comment

Has it ever happened to you?

$ mkdir something
$ cd something

Damn, that’s 2 commands. How to simplify it?


$ pwd
$ mdgo something    # mdgo stands for: mkdir-go
$ pwd

That is, the command “mdgo” creates a directory and enters in it. An alternative name could be “mdcd“.

If you want this alias, add the following lines to your ~/.bashrc:

if [[ -z "$1" ]]
    echo "Usage: mdgo <dir>"
    mkdir $1; cd $1
alias mdgo=mkdir_go
Categories: bash Tags: ,

alias with an argument

September 21, 2013 Leave a comment

In bash, you want to write an alias that receives an argument.

Well, you can’t do that in bash. But there is a workaround: call a bash function, and the function can treat the argument.

Let’s see a concrete example. While working with Python virtual environments, I wanted to be able to activate a virtual env. easily with the command “workon <DIR>“, where “workon” would be an alias and “<DIR>” is the name of a directory. (Detailed post is here.)

Add the following lines to your ~/.bashrc:

if [[ -z "$1" ]]
    echo "Usage: workon <venv>"
    . $1/bin/activate
alias workon=func_workon
Categories: bash, python Tags: ,

a cool colorized prompt for bash

September 21, 2013 1 comment

You want a cool colorized bash prompt.

While hunting for a prompt that I could use with virtualenv (post here), I found a prompt that is way cooler than the default bash prompt. I made a fork of it, you can find it here.

Note that it’s not me who wrote it. I just changed one line :)

Save the file above as ~/.bash_prompt and add the following line to the end of your ~/.bashrc:

source ~/.bash_prompt


Categories: bash Tags: ,

Get every new post delivered to your Inbox.

Join 62 other followers