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

Find the most recently modified files in a directory and all subdirectories

 $ find /path/to/dir -type f | perl -ne 'chomp(@files = <>); my $p = 9; foreach my $f (sort { (stat($a))[$p] <=> (stat($b))[$p] } @files) { print scalar localtime((stat($f))[$p]), "\t", $f, "\n" }' | tail

— by janos on Oct. 4, 2011, 10:25 p.m.

Explanation

  • find path_to_dir -type f prints all the files in the directory tree
  • chomp(@files = <>); reads all the lines into an array
  • stat($a) is an array of interesting info about a file. Index 7 is size, 8 is access time, 9 is modification time, etc. (See man perlfunc for details and search for stat EXPR.)
  • sort { (stat($a))[9] <=> (stat($b))[9] } @files sorts the files by modification time
  • print scalar localtime((stat($f))[9]), "\t", $f, "\n" - prints the modification time formatted nicely, followed by a tab and the filename

0

View specific column of data from a large file with long lines

 $ cat /tmp/log.data |colrm 1 155|colrm 60 300

— by versorge on Oct. 4, 2011, 9:55 p.m.

Explanation

  • cat: prints the file to standard output
  • colrm: removes selected columns from a file

0

Delete unversioned files in a Subversion checkout directory and all subdirectories

 $ svn st | grep ^? | sed -e 's/^? *//' | xargs -i{} echo rm -fr "{}"

— by janos on Oct. 3, 2011, 8:22 p.m.

Explanation

If there are no spaces in the file names, a simpler command will be enough. This one-liner works even if there are spaces and certain special characters in the file names.

  • svn st shows the changes in the Subversion checkout
  • | grep ^? matches only lines starting with question mark (= unversioned files)
  • | sed -e 's/^? *//' removes the question mark at the beginning of the line and the space characters following it
  • | xargs -i{} echo rm -fr "{}" executes an echo command for each line in the input, where the command is formed by inserting the input line in the {} placeholder. Confirm the result looks good and remove the echo to perform the rm.

Limitations

The command will not work with files that have " (double quote) in the name...

0

Get the available space on a partition as a single numeric value

 $ df /path/to/dir | sed -ne 2p | awk '{print $4}'

— by Janos on Oct. 2, 2011, 5:41 p.m.

Explanation

  • sed -ne 2p prints the 2nd line
  • awk '{print $4}' prints the 4th column

Limitations

The output of the df command might be different depending on the system, and the available space might not be the 4th column. Make the necessary adjustments depending on your system.

0

Schedule a one-time task using "at" command and intuitive date specifications

 $ at now + 30 minutes

— by Janos on Sept. 25, 2011, 11:30 a.m.

Explanation

  • The example will run something 30 minutes from now.
  • Another example: at 0815 wed -- run something at 8:15am on the next Wednesday.
  • With atq you can see the list of scheduled jobs, this is good to confirm that you specified the date correctly and the job will run when you want.
  • With atrm you can cancel scheduled jobs, using the job id as parameter, which you can find in the atq output.

0

Aliases the ls command to display the way I like it

 $ alias ls='ls -lhGpt --color=always'

— by versorge on Sept. 22, 2011, 12:25 a.m.

Explanation

alias: allows you to define a shortcut to a longer command. In this case 'ls' with the flags set for long listing, human readable no group and in color, not displaying the group, appending a slash to directories and color coding the output.

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 Janos 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 Janos 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 Janos 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 Janos 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 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.

0

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.