The bash shell uses backslash characters to escape out spaces in a long string that would otherwise be interpreted as separate parameters. This enables you to use the cp or rm commands against files with spaces in their filenames.
Take this example: cp my\ awesome\ extra\ cool\ file.txt myfile.txt
this command will copy a file with spaces in the filename. That is a very useful thing to know sometimes. A harder thing to do is deleting a file that is named -rf myfile.txt. Of course you can just type: rm -- -rf\ myfile.txt
and get a good result, this is not the end of the world.
Use the \t escape to render a tab character, \a is the bell character, this harkens back to the days of the old UNIX teletypes that had a real bell inside them that would ring on the receipt of the ASCII code 007. That is a very interesting piece of trivia.
Another use of the backslash character is to perform various tricks with grep. Try this out: grep ‘ \<ram\’> memory.txt this command will search for the text “ram”, but not the word ” program” or “anagram”. That is one very useful trick. There is an example below.
homer@neo ~ $ grep '\<SUSE\>' .bashrc # From a SUSE .bashrc file. |
Here is another example.
homer@neo ~/Documents $ ls -hula total 4.9M drwxr-xr-x. 7 homer homer 4.0K 25-09-13 10:08 am . drwx------. 31 homer homer 4.0K 24-09-13 07:48 pm .. -rw-r--r--. 1 homer homer 4.8M 03-09-13 12:05 pm AdvancedLinuxPocketbook.pdf -rw-r--r--. 1 homer homer 248 10-09-13 10:52 pm blog.txt drw-r--r--. 2 homer homer 4.0K 24-09-13 12:20 pm Bonus Chapters drw-r--r--. 7 homer homer 4.0K 24-09-13 12:20 pm BUDGET -rw-r--r--. 1 homer homer 14K 30-08-13 10:15 pm circle -rw-r--r--. 1 homer homer 809 10-09-13 10:52 pm circle.cpp drw-r--r--. 32 homer homer 4.0K 24-09-13 12:20 pm CPP_Programs -rw-rw-r--. 1 homer homer 1.6K 24-09-13 01:20 pm doctors-network.dia -rw-r--r--. 1 homer homer 237 10-09-13 10:52 pm image-loop.cpp drw-r--r--. 2 homer homer 4.0K 24-09-13 12:25 pm ipcalc-0.41 -rw-r--r--. 1 homer homer 244 10-09-13 10:52 pm login.sh -rw-r--r--. 1 homer homer 119 10-09-13 10:52 pm mkdir.sh drw-r--r--. 2 homer homer 4.0K 24-09-13 12:20 pm mydir -rw-r--r--. 1 homer homer 9.0K 29-08-13 09:06 pm -rf myfile.txt |
I created a file named -rf myfile.txt. Now the challenge is to delete it.
I did this using this command.
homer@neo ~/Documents $ rm -- -rf\ myfile.txt |
The — parameter after the rm command tells it to stop looking for more command line parameters. I found this solution here: http://www.unix.com/unix-advanced-expert-users/10069-how-remove-file-leading-dash-s-name.html.
The sed command also uses backslashes, see this for an example. This is useful for translating part of a string to something else.
homer@neo ~/Documents $ ls -hula | sed s'\circle\square\'gi; total 4.9M drwxr-xr-x. 7 homer homer 4.0K 25-09-13 10:11 am . drwx------. 31 homer homer 4.0K 24-09-13 07:48 pm .. -rw-r--r--. 1 homer homer 4.8M 03-09-13 12:05 pm AdvancedLinuxPocketbook.pdf -rw-r--r--. 1 homer homer 248 10-09-13 10:52 pm blog.txt drw-r--r--. 2 homer homer 4.0K 24-09-13 12:20 pm Bonus Chapters drw-r--r--. 7 homer homer 4.0K 24-09-13 12:20 pm BUDGET -rw-r--r--. 1 homer homer 14K 30-08-13 10:15 pm square -rw-r--r--. 1 homer homer 809 10-09-13 10:52 pm square.cpp drw-r--r--. 32 homer homer 4.0K 24-09-13 12:20 pm CPP_Programs -rw-rw-r--. 1 homer homer 1.6K 24-09-13 01:20 pm doctors-network.dia -rw-r--r--. 1 homer homer 237 10-09-13 10:52 pm image-loop.cpp drw-r--r--. 2 homer homer 4.0K 24-09-13 12:25 pm ipcalc-0.41 -rw-r--r--. 1 homer homer 244 10-09-13 10:52 pm login.sh -rw-r--r--. 1 homer homer 119 10-09-13 10:52 pm mkdir.sh drw-r--r--. 2 homer homer 4.0K 24-09-13 12:20 pm mydir |
nice tips. one can also use
# rm " -rf myfile.txt"
(quotes) instead of backslash escaping. as for the sed, it can use any delimiter that is following the ‘s’ command. for example,you can even use white-space as a delimiter.