Linux
Re-Use A Bash Command With Different Parameters
Suppose you have typed and executed this command in your Linux shell:
1 | ./script_a.sh 1.23 && ./script_b.sh 1.23 && ./script_c.sh 1.23.45 |
Now you want to run the same command, but with 2.34 instead of 1.23
A nice way to do it is this:
1 | !!:gs/1.23/2.34 |
Meaning, run the last command (!! is also called ‘bang bang’, and it’s substituted by the last command you executed), and replace every instance of 1.23 by 2.34
via Unix Bash Scripting.
Have fun
Make Shell Scripts Executable By Default
If you use vim to write shell scripts, you might want to save the “chmod +x” command after saving the script.
By adding the following line to your vimrc file (typically, it’s located at ~/.vimrc), scripts will automatically become executable.
au BufWritePost * if getline(1) =~ “^#!” | if getline(1) =~ “/bin/” | silent !chmod a+x <afile> | endif | endif
(meaning, if the file includes #! with “/bin/” in the path, apply “chmod a+x” on this file).
via shell-fu.
Have fun
Strip Leading Characters Off A String
Say you have a bash variable (say x) storing the string “MyLongString”
This bash command:
1 | echo ${x##My} |
Will strip the leading string “My” off the original string. And thus the output will be:
LongString
via @bashcookbook
Strip the File Name Suffix off a Bash Variable
Suppose you have a bash variable storing a file name, say x =file.jpg
You can use this substitution command in order to strip out the file suffix off the string:
1 | echo ${x%.*} |
The output will be the base name of the file without the suffix.
via @bashcookbook
Enjoy
Create Multiple Nested Directories in Linux
Did you know that you can create a complete directory tree with one command?
Let’s start with a simple example:
mkdir -p a/b/c
will create this tree:
`-- a
`-- b
`-- c
And any of these:
mkdir -p a/{b1,b2}/c
or
mkdir -p a/b{1,2}/c
Will create this tree:
› Continue reading
Edit The Command Line With Vim
This is a quick (and great) tip I found at Daily Vim:
Open a linux terminal, and type some (long) command.
Now type [ctrl]+[x] and then [ctrl]+[e]
The command should be moved now to a vim window.
Edit the command (fix typos, change parameters, etc..) and save.
The command will now be executed.
Enjoy
Renaming Files In Linux
In a previous post, I wrote about converting strings from camelCase to undescore delimited format.
In this post, we’ll do the same but for file names. We’ll use the command line program “rename” for this purpose.
Here we go:
1. Convert all file names in current directory which are of the form “myFileName” or “MyFileName” into the form of “my_file_name”:
1 | rename 's/(.)([A-Z])/$1_\l$2/g' * && rename 's/([A-Z])/\l$1/g' * |
2. Convert all file names in current directory which are of the form “my_file_name” into the form “myFileName”:
1 | rename 's/([a-z])_([a-z])/$1\u$2/g' * |
3. Convert all file names in current directory which are of the form “my_file_name” into the form “MyFileName”:
1 | rename 's/([a-z])_([a-z])/$1\u$2/g' * && rename 's/^([a-z])/\u$1/g' * |
Enjoy
Convert camelCase to Underscores Using sed
This short post deals with converting strings of the form camelCase or CamelCase into camel_case, and vice versa. These are three different popular naming conventions for variable/function/class names.
Convert CamelCase or camelCase to camel_case:
1 | sed -e 's/\([A-Z]\)/_\l\1/g' -e 's/^_\([a-z]\)/\1/g' file.txt |
Convert camel_case to camelCase
1 | sed -e 's/_\([a-z]\)/\u\1/g' file.txt |
Convert camel_case to CamelCase:
1 | sed -e 's/_\([a-z]\)/\u\1/g' -e 's/^\([a-z]\)/\u\1/g' file.txt |
Sed and AWK – A quick reference
Sed (Stream Editor) and AWK (First letters of the surnames of its authors) are very powerful *nix tools for manipulating strings and text files. They combine the power of regular expressions with the power of a programming language for this aim.
Here are two quick references for doing many operations with a one-line code of sed and AWK. (Both are by the same author – Eric Perment):
› Continue reading
Linux Fork Bomb Explained
One famous way to crash your Linux system is to run a “fork bomb” in the terminal. A variant of it looks like this:
WARNING: Malicious code. Don’t run it!
1 | :(){:|:&};: |
This code looks strange, but it’s actually a conventional code in Bash. To make it a little more obvious, let’s replace the : (colon) with the word bomb. So, our code now looks like this:
1 | bomb(){bomb|bomb&}; bomb |
About Me
Tags
My Twitter
Categories
- Algorithms
- Bash
- BlackBerry
- Collaboration
- Command Line
- Cool Tricks
- Easter Eggs
- Ebooks
- Firefox
- Hardware
- Humor
- Linux
- Linux Development
- Linux Kernel
- Networks
- Open Knowledge
- Other
- Productivity
- Programming
- Science
- Security
- Shell Scripts
- Short Posts
- Thoughts
- Tools
- Vim
- Web Development
- Websites
Popular Posts
Archives
- March 2010 (1)
- January 2010 (1)
- December 2009 (2)
- September 2009 (13)
- July 2009 (1)
- June 2009 (6)
- May 2009 (4)
- March 2009 (18)
- February 2009 (10)
- January 2009 (10)
- December 2008 (7)
- November 2008 (8)
- October 2008 (1)
- August 2008 (1)
- July 2008 (1)
- June 2008 (2)

Me @ Social Media