We collect practical, well-explained Bash one-liners, and promote best practices in Bash shell scripting. To get the latest Bash one-liners, follow @bashoneliners on Twitter. If you find any problems, report a bug on GitHub.

Tags

0

Force the preferred language when downloading a web page with wget

 $ wget -–header='Accept-Language: en-us' http://www.timeanddate.com/calendar/index.html?year=2008&country=26 -O calendar.html

— by Janos on Aug. 21, 2011, 1:40 p.m.

Explanation

When downloading web pages with wget, some websites try to be smart and detect your preferred language based on geographical location of your IP address. This can be a problem if for example you are in Japan but you want to download a page in English.

0

Burn the contents of a directory to dvd without needing a gui application

 $ growisofs -dvd-compat -Z /dev/scd0 -R -J -pad /path/to/dir

— by Janos on Aug. 14, 2011, 9:35 p.m.

Explanation

Useful if you have a directory full of data to burn to DVD without using a GUI. The growisofs tool is in the "dvd+rw-tools" package in Ubuntu, install it with: sudo apt-get install dvd+rw-tools

0

Convert all flac files in the current directory to mp3 format using "lame"

 $ for i in *.flac; do flac -c -d "$i" | lame -m j -b 192 -s 44.1 - "${i%.flac}.mp3"; done

— by Janos on Aug. 9, 2011, 9:57 p.m.

Explanation

0

Halt the system in Linux without the halt command or gui

 $ echo o > /proc/sysrq-trigger

— by Janos on Aug. 9, 2011, 9:55 p.m.

Explanation

  • First you need to enable the sysrq interface with: echo 1 > /proc/sys/kernel/sysrq
  • echo o > /proc/sysrq-trigger halts
  • echo b > /proc/sysrq-trigger reboots

0

Change to the previous directory, a shortcut for "cd $OLDPWD"

 $ cd -

— by Janos on Aug. 9, 2011, 1:31 a.m.

Explanation

0

Create an encrypted tar file with openssl

 $ tar c paths_to_files_and_dirs | gzip -c | openssl des3 > encrypted.tar.gz

— by Janos on Aug. 9, 2011, 1:30 a.m.

Explanation

Decrypt with: openssl des3 -d < encrypted.tar.gz | tar zx

0

Make another user superuser in Ubuntu

 $ for i in $(grep :boss /etc/group | cut -f1 -d:); do adduser wife $i; done

— by Janos on Aug. 5, 2011, 8:57 p.m.

Explanation

In Ubuntu the first user (created during installation) has special privileges. The privileges come from the fact that the user was automatically added to various system groups. To make another user have the same privileges, all you need to do is add the user to the same groups.

  • grep :boss /etc/group gets the group records where the user boss is the first member.
  • cut -f1 -d: gets the first column, where ":" is the column separator.
  • for i in ...; do ... ; done for each group it adds the user to the group.

0

Change the label of a USB drive in Linux without a gui

 $ sudo mlabel -i /dev/sdd1 ::NewLabel

— by Janos on Aug. 5, 2011, 8:57 p.m.

Explanation

Replace /dev/sdd1 with whatever your USB stick is mounted on.

0

Mirror from one Subversion repository to another Subversion repository

 $ bzr co https://repo1/proj1/trunk proj1 && cd proj1 && bzr push https://repo2/vendor/proj1/trunk

— by Janos on Aug. 5, 2011, 8:57 p.m.

Explanation

  • The commit history in repo1 will be copied to repo2.
  • The temporary Bazaar repository (proj1) will have the full history of changes.
  • The command above initializes the mirror. To update the mirror, run this script, you can schedule it to run periodically to keep the mirror up to date: cd proj1 && bzr up && bzr push

Limitations

  • The author information will get lost in the process.

2

Rename all files in a directory to lowercase names

 $ paste <(ls) <(ls | tr A-Z a-z) | while read OLD NEW; do echo mv -v $OLD $NEW; done

— by Janos on Aug. 5, 2011, 8:57 p.m.

Explanation

  • <(cmd) is the filename of a named pipe (FIFO), where the named pipe is filled by the output of cmd
  • paste puts together the named pipes to form two columns: first column with the original filenames, second column with the lowercased filenames
  • ... | tr abc ABC transforms stdin by replacing any characters that appear in the first set of letters to the second set of letters
  • while read old new; do ...; done for each line it reads the first column into $old and the second column into $new

Limitations

  • Won't work if there are spaces in a filename.

0

Find Flash videos stored by browsers on a Mac

 $ find /private/ 2>/dev/null | grep /Flash

— by Janos on Aug. 5, 2011, 8:57 p.m.

Explanation

When you watch a flash video like youtube in a browser, the video file is saved on your harddisk at a temporary location. And, if you watch a video and then another video in the same window, the first one will be deleted.

Limitations

  • Might not work with all browsers.
  • Does not work with all websites (for example IMDB).
  • Does not work with an anonymous window in Chrome.