Linux Format

Real-world one liners

-

You can make practicall­y any Bash script a oneliner, even though it would be hard to read it. But there are a lot of useful yet short Bash scripts created by the people from around the world. Even if you feel like you don’t need any more practice at writing scripts looking at other’s best practices will not hurt. A one-liner means that you can use it directly as a command.

The first example script we’re going to show off is for music lovers, and it converts all .flac files that it finds in the current directory to MP3 files at a good quality setting (320 kbps) using FFmpeg. $ for FILE in *.flac; do ffmpeg -i "$ FILE" -b:a 320k "${ FILE[@]/%flac/mp3}"; done; Another tip is to copy something to the X.org clipboard from the command line. First make sure you have the xclip package installed, then try the following: $ xclip -in -selection c script.sh # or $ echo "hi" | xclip -selection clipboard The first command will copy the contents of your script to the clipboard, while the second one will put the word ‘hi’ there.

The next examples shows 10 large files opened by currently running processes in your system: # lsof / | awk '{ if($7 > 1048576) print $7/1048576 "MB" "" $9 "" $1 }' | sort -n -u | tail It’s extremely useful when you need to identify the origin of the high load and standard system monitor doesn’t clear things up. You have to be root to run this command.

Newspapers in English

Newspapers from Australia