Archive

Archive for the ‘bash’ Category

ménage de printemps (spring cleaning)

March 22, 2014 Leave a comment

Problem
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?

Solution

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.

Samples

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

Credits

  • 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

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

Solution
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

Install:

sudo apt-get install transmission-cli

Usage:

$ 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?

Solution

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

Frustration
Has it ever happened to you?

$ mkdir something
$ cd something

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

Solution

$ pwd
/home/jabba
$ mdgo something    # mdgo stands for: mkdir-go
$ pwd
/home/jabba/something

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:

mkdir_go()
{
if [[ -z "$1" ]]
then
    echo "Usage: mdgo <dir>"
else
    mkdir $1; cd $1
fi
}
alias mdgo=mkdir_go
Categories: bash Tags: ,

alias with an argument

September 21, 2013 Leave a comment

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

Solution
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:

func_workon()
{
if [[ -z "$1" ]]
then
    echo "Usage: workon <venv>"
else
    . $1/bin/activate
fi
}
alias workon=func_workon
Categories: bash, python Tags: ,

a cool colorized prompt for bash

September 21, 2013 1 comment

Problem
You want a cool colorized bash prompt.

Solution
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 :)

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

source ~/.bash_prompt

Screenshot
workon

Categories: bash Tags: ,
Follow

Get every new post delivered to your Inbox.

Join 62 other followers