We aim to collect practical, well-explained bash one-liners, and promote best practices in shell scripting. To get the latest bash one-liners, follow @bashoneliners on Twitter, or subscribe to our rss feed. If you find any problems, report a bug on GitHub.

Tags

1

Remove spaces recursively from all subdirectories of a directory

 $ find /path/to/dir -type d | tac | while read LINE; do target=$(dirname "$LINE")/$(basename "$LINE" | tr -d ' '); echo mv "$LINE" "$target"; done

— by openiduser3 on Sept. 20, 2011, 4:52 p.m.

Explanation

  • find path_to_dir -type d finds all the subdirectories
  • tac reverses the order. This is important to make "leaf" directories come first!
  • target=... stuff constructs the new name, removing spaces from the leaf component and keeping everything before that the same
  • echo mv ... for safety you should run with "echo" first, if the output looks good then remove the "echo" to really perform the rename

Limitations

In UNIX or BSD there is no tac. There you can use tail -r instead.

1

Replace a regexp pattern in many files at once

 $ vi +'bufdo %s/pattern/replacement/g | update' +q $(grep -rl pattern /path/to/dir)

— by openiduser3 on Sept. 15, 2011, 11:50 p.m.

Explanation

  • The inner grep will search recursively in specified directory and print the filenames that contain the pattern.
  • All files will be opened in vi, one buffer per file.
  • The arguments starting with + will be executed as vi commands:
    • bufdo %s/pattern/replacement/g | update = perform the pattern substitution in all files and save the changes
    • q = exit vi

Limitations

The :bufdo command might not be there in old versions of vim.

0

The first command you should run right after you login to a remote server ;-)

 $ screen

— by openiduser3 on Sept. 15, 2011, 10:24 p.m.

Explanation

With screen you can have multiple shells in the same window. You don't need to open new windows for a second connection to the same remote server, you can simply press C-a c inside the screen session. And if your computer crashes, you screen session on the remote server survives, you can re-attach to a previous screen session with screen -R.

0

Find video files cached by the flash plugin in browsers

 $ file /proc/*/fd/* 2>/dev/null | grep Flash | cut -f1 -d:

— by openiduser3 on Aug. 27, 2011, 1:46 p.m.

Explanation

Recent versions of the flash plugin hide the temporary file by marking it deleted. Practically the video stream is downloaded to a "deleted file". However, even when a file is deleted, if the file is opened by a process then you can find its file descriptor and consequently the file contents.

This simple script prints out the file descriptors of opened Flash videos:

file /proc/*/fd/* 2>/dev/null | grep Flash | cut -f1 -d:

And, you probably want to create a regular file from the file descriptor, for example:

cp $(file /proc/*/fd/* 2>/dev/null | grep Flash | cut -f1 -d: | head -n 1) video.avi

Otherwise the file descriptor is not very convenient (remember, it's a deleted file!)

The method should work regardless of your browser.

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 openiduser3 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 openiduser3 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 openiduser3 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 openiduser3 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 openiduser3 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 openiduser3 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 openiduser3 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 openiduser3 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 openiduser3 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.
0

An elegant way to 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 openiduser3 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 openiduser3 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.