Archive for May 2014
Use Robocopy as a Backup Solution
By : Unknown
Thanks to the robocopy command, you don't need to use Window's backup software or install a third party program to manage your backups.
Just execute robocopy c:\users\tim\documents f:\backup\documents /copyall /e /r:0 /dcopy:t /mir, obviously replacing the source and destination folders with whatever you'd like to backup and where. The robocopy command with these options functions identically to an incremental backup software tool, keeping both locations in sync.
You don't have the robocopy command if you're using Windows XP or earlier. However, you do have the xcopy command, which can be used to do something very similar: xcopy c:\users\tim\documents f:\backup\documents /c /d /e /h /i /k /q /r /s /x /y.
No matter which command you choose to use, just create a script file containing the command and schedule it to run in Task Scheduler and you'll have your own custom made backup solution.
I've chosen to use the robocopy command on my personal computers as my only local backup solution because I like the level of control it gives me. Hopefully you take that as a vote of confidence in this incredibly useful Command Prompt trick.
Just execute robocopy c:\users\tim\documents f:\backup\documents /copyall /e /r:0 /dcopy:t /mir, obviously replacing the source and destination folders with whatever you'd like to backup and where. The robocopy command with these options functions identically to an incremental backup software tool, keeping both locations in sync.
You don't have the robocopy command if you're using Windows XP or earlier. However, you do have the xcopy command, which can be used to do something very similar: xcopy c:\users\tim\documents f:\backup\documents /c /d /e /h /i /k /q /r /s /x /y.
No matter which command you choose to use, just create a script file containing the command and schedule it to run in Task Scheduler and you'll have your own custom made backup solution.
I've chosen to use the robocopy command on my personal computers as my only local backup solution because I like the level of control it gives me. Hopefully you take that as a vote of confidence in this incredibly useful Command Prompt trick.
View Your Computer's Important Network Information
By : Unknown
Maybe just for your own information, but certainly when you're troubleshooting a network or Internet problem, you'll probably at some point need to know details about your computer's network connection.
Everything you'd want to know about your network connection is available somewhere in the Control Panel in Windows, but it's much easier to find, and much better organized, in the results from the ipconfig command.
Open Command Prompt and execute ipconfig /all. What displays on screen next is everything important about your network connection: your IP address, hostname, DHCP server, DNS information, and much, much more.
Combine this hack with Command Prompt Trick #7 and you've got a very easy way to get information about your connection to someone helping you with a problem.
Access Previously Used Command with the Arrow Keys
By : Unknown
Another great Command Prompt trick has to be the use of the keyboard arrow keys to cycle through previously executed commands. The up and down arrow keys cycle through the commands you've entered and the right arrow automatically enters, character by character, the last command you executed.
This might not sound that interesting, but there are several situations where the arrow keys become huge time savers.
Consider this example: You've typed out 75 characters of a command and then try to execute it, only to find that you forgot to add an option at the very end. No problem, just hit the up arrow and the entire command is automatically entered in the Command Prompt window, ready for you to edit to make it work.
Automatically Complete Commands with Tab Completion
By : UnknownTab completion is another Command Prompt trick that can save you lots of time, especially if your command has a file or folder name in it that you're not completely sure of.
To use tab completion in the Command Prompt, just enter the command and then the portion of the path that you do know, if at all. Then press the tab key over and over to cycle through all of the available possibilities.
For example, let's say you want to change directories to some folder in the Windows directory but you're not sure what it's named. Type cd c:\windows\ and then press tab until you see the folder you're looking for. The results cycle or you can use Shift+Tab to step through the results in reverse.
Copy & Paste Easier with QuickEdit Mode
By : Unknown
How about an even easier way to copy from the Command Prompt? And a secret way to easily paste?
Just right-click on the Command Prompt title bar and select Properties. On the Options tab, in the Edit Options section, check the QuickEdit Mode box and then click OK.
Enabling QuickEdit Mode is like having Mark enabled all the time so selecting text to copy is really easy.
But it also enables an easy way to paste into the Command Prompt: just right click once and whatever is in the clipboard is pasted in the Command Prompt window. Normally, pasting involves right-clicking and selecting Paste.
Watch Star Wars Episode IV
By : Unknown
Yes, you read that correctly, you can watch an ASCII version of the full Star Wars Episode IV movie right in the Command Prompt window!
Just open Command Prompt and execute telnet towel.blinkenlights.nl. The movie will start immediately. This isn't a terribly productive use of the Command Prompt, nor is it really a trick of the Command Prompt or any command, but it sure is fun!
Note: The telnet command is not enabled by default in Windows 7 or Windows Vista but can be turned on by enabling Telnet Client from Windows Features in the Programs and Features applet in Control Panel. If you'd rather not enable Telnet but would like to see the movie, you can also watch it in your browser here.
Copy From the Command Prompt
By : Unknown
As you may or may not know, copying from the Command Prompt is not as easy as copying from other programs, which is part of the reason why saving a command's output to a file, Command Prompt Trick #7, is so handy.
But what if you do just want to copy a short section of text to the clipboard? It's not too hard but it's not very intuitive either.
Right-click anywhere in the Command Prompt window and choose Mark. Now, highlight with your left mouse button whatever you'd like to copy. Once your selection is made, press Enter.
Now you can paste that information into whatever program you'd like.
Customize the Command Prompt Title Bar Text
By : Unknown
Tired of that Command Prompt title bar text? No problem, just use the title command to change it to whatever you like.
For example, let's say your name is Raghu Doradla, and you want to express your ownership of the Command Prompt: execute title Property of Raghu doradla and the Command Prompt's title bar will change immediately.
The change won't stick, so the next time you open Command Prompt the title bar will be back to normal.
The title command is usually used to help give a custom appearance in script files and batch files... not that I'm saying titling it with your name isn't a good idea!
View Your Hard Drive's Entire Directory Structure
By : UnknownI think one of the neatest little commands is the tree command. With tree, you can create a kind of map of your computer's directories.
Execute tree from any directory to see the folder structure under that directory.
Tip: With so much information, it's probably a good idea to export the results of the tree command to a file. For example, tree /a > c:\treeresults.txt, just as explained in Command Prompt Click Here!!!
Save a Command's Output to a File
By : Unknown
An incredibly useful Command Prompt trick is the use of redirection operators, specifically the > and >> operators.
These little characters let you redirect the output of a command to a file, giving you a saved version of whatever data the command produced in the Command Prompt window.
For example, let's say you're about to post a computer problem to an online forum and you want to provide really accurate information about your computer. An easy way to do that would be to use the systeminfo command with a redirection operator.
For example, you might execute systeminfo > c:\mycomputerinfo.txtto save the information provided by the systeminfo command to a file. You could then attach the file to your forum post.
See How To Redirect Command Output to a File for more examples and a better explanation of how to use redirection operators.
Get Help for Any Commandhttp://0.tqn.com/d/pcsupport/1/0/A/D/-/-/help-switch.jpg
By : Unknown
Believe it or not, the help command does not provide help for every Command Prompt command. However, any command can be suffixed with the /? option, usually called the help switch, to display detailed information about the command's syntax and often times even some examples.
I doubt that the help switch is the coolest Command Prompt trick you've ever heard of, but it's hard to disagree that it's one of the more useful.
Unfortunately, neither the help command nor the help switch offer much in the way of explaining how to interpret the syntax. See How To Read Command Syntax if you need help with that.
Become a Command Prompt Power User with Function Keys
By : Unknown
The fact that the function keys actually do something in the Command Prompt is maybe one of the best kept secrets about the tool:
F1: Pastes the last executed command (character by character)
F2: Pastes the last executed command (up to the entered character)
F3: Pastes the last executed command
F4: Deletes current prompt text up to the entered character
F5: Pastes recently executed commands (does not cycle)
F6: Pastes ^Z to the prompt
F7: Displays a selectable list of previously executed commands
F8: Pastes recently executed commands (cycles)
F9: Asks for the number of the command from the F7 list to paste
Command Prompt Trick #17 is full of arrow key shortcuts, a few of which are similar to these function key tricks.
Run Command Prompt as an Administrator Automatically
By : Unknown
Many commands require that you execute them from an elevated Command Prompt in Windows - in other words, execute them from a Command Prompt that's run as an administrator.
You can always right-click on any Command Prompt shortcut and chooseRun as administrator but creating a shortcut to do the same thing can be a huge time saver if you're a frequent Command Prompt power user.
To complete this Command Prompt trick, just create a Command Prompt shortcut on the desktop, enter the shortcut's properties and then select the Run as administrator box located in the Advanced button on theShortcut tab.
View a Command's Results One Page (or Line) at a Time
By : Unknown
Ever run a command, like the dir command, that produces so much information on the screen that it's almost useless? You're not alone.
One way around this is to execute the command in a special way so whatever information is generated is shown to you one page, or one line, at a time.
To do this, just type the command, the dir command for example, and then follow it with the pipe redirection operator and then the more command.
For example, executing dir /s | more will generate the thousands of lines of results that you expect from the dir command, but the more command will pause each page of results with -- More -- at the bottom of the page, indicating that the command is not done running.
Just press the space bar to advance by page or press the Enter key to advance one line at a time.
See Command Prompt Trick #7 below for a different solution to this problem.
Use Ctrl-C to Abort a Command
By : Unknown
Just about any command can be stopped in its tracks with the abort command: Ctrl-C.
If you haven't actually executed a command, you can just backspace and erase what you've typed, but if
you've already executed it then you can do a Ctrl-C to stop it.
Warning: Ctrl-C isn't a magic wand and it can't undo things that aren't undoable, like a partially complete format command. However, for things like the dir command that seem to go on forever or questions you're asked at the prompt that you don't know the answer to, the abort command is an excellent Command Prompt trick to know.
If you haven't actually executed a command, you can just backspace and erase what you've typed, but if
you've already executed it then you can do a Ctrl-C to stop it.
Warning: Ctrl-C isn't a magic wand and it can't undo things that aren't undoable, like a partially complete format command. However, for things like the dir command that seem to go on forever or questions you're asked at the prompt that you don't know the answer to, the abort command is an excellent Command Prompt trick to know.
Command prompt Hacks
By : Unknown
Did you know that the prompt itself in the Command Prompt is completely customizable thanks to the prompt command? It is, and when I say customizable, I mean really customizable.
Instead of C:\>, you can set the prompt to any text you want, have it include the time, the current drive, the Windows version number, you name it.
You can always execute prompt alone, without options, to return it to its sometimes boring default.
Instead of C:\>, you can set the prompt to any text you want, have it include the time, the current drive, the Windows version number, you name it.
One useful example is prompt $m$p$g which will show the full path of a mapped drive in the prompt, alongside the drive letter.
You can always execute prompt alone, without options, to return it to its sometimes boring default.
Earn through Blogging
By : Unknown
Since I’ve started blogging, I have had more than a few people ask me how they too can make money from blogging.
It’s easy to look at people who have risen to fame and prominence by building a high-profile, money-making blog and think “Hey, I can do that, too.”
You might not become the next Perez Hilton or Dooce, but if you play your cards right, you can start to pull in an income from your blog.
Whether the income is enough to allow you to quit a full-time job depends on a number of different factors. No matter how much you end up making, creating a blog that you find exciting is one of the fun ways to make money online.
There are a few different ways you can make money from blogging. I recommend using a combination of income-generating methods.
Some techniques might work for one type of blog, but not another, so it’s a good idea to try several to see what generates the most revenue for you.
AdSense
When it comes to putting advertisements on your blog to generate income, Google’s AdSense is often the top pick for bloggers. It’s simple to use and doesn’t require you to have a great amount of technical know-how.
To put an AdSense ad on your blog, you pretty much just have to copy and paste the code.
It’s also one of the biggest advertising networks available, so you’ll have a wide range of advertisers to work with. That also means that you can make money from it even if your blog falls into a pretty obscure niche.
Amazon Associates
The Amazon Associates program allows you to earn a portion of sales when people click-through from your blog to Amazon.com and make a purchase.
You only earn money when people buy something from the web giant and the amount you earn varies based on what people order and how many people purchase through your links.
The Associates program works best for people whose blog focuses on products and shopping in someway. You’ll earn more money if people are in a shopping mood when they visit your site.
Other Affiliate Programs
While Amazon’s affiliate marketing program might be one of the biggest out there, it’s definitely not the only one available.
Depending the topic of your blog and the amount of traffic you get, you can participate in any number of other marketing programs. Some marketing programs only want blogs that have a high traffic volume, so you might have to wait and build your audience before signing up.
As your blog grows, fun ways to make money online can include reaching out to small companies whose products you trust and use to see if they would like to set up an affiliate marketing program with you.
You would post a link to their product on your blog and if someone clicks through your blog to their site and orders it, you would get a portion of the sale.
Private Sponsorships
As your blog becomes more established, you can start to find ways to make more money through private ad sales and sponsorships.
Unlike AdSense ads, private ads don’t just come to you. You’ll have to go out and find them, much like the advertising staff of a magazine approaches companies about placing ads in their pages.
To sell private ads, you’ll need to convince the company that your blog has a sufficient amount of traffic. You’ll also need to show the company that your blog’s topic is a good fit for whatever service or product they offer.
E-Books
When you blog, you provide people with information for free. One way to expand on the information you give people and make money doing it is to write an e-book and sell it on your blog.
Ideally, your e-book will connect with the content on your blog and expand on it in some way. People won’t want to pay money for content you’ve previously given away.
For example, if you have a cooking blog, your e-book could be a short cookbook featuring previously unpublished recipes.
The recipes could have been tested by a group of people before publishing and not available anywhere else.
Personal Appearances and Speaking Fees
If your blog really becomes popular, you might be able to expand your income opportunities beyond the web and into the real world.
Some bloggers get paid to appear at conferences or to give readings to the public.
You might not get paid to give a speech at first, but as your blog becomes more well-known and respected, organizations might offer you some money to say a few words.
Offer Exclusivity
Once your blog has an established readership, you can expand your income-generating opportunities further by offering memberships to your website.
People can pay a yearly or monthly amount to gain access to exclusive content on your blog, to participate in forums, or to receive a monthly newsletter from you.
Starting a members-only portion of your blog lets your readers feel special while it makes you some extra money.
The most important thing to remember when starting your blog is that you won’t become a millionaire overnight. Many bloggers are excited to bring in just a few dollars a month.
It takes a considerable amount of focus and dedication to your blog to turn into a project that generates a significant amount of income.
Linux Fun
By : UnknownAs you start spending more and more time working on Linux command line, you tend to learn some cool tricks that make your life easy and save you lot of time. I have been working on Linux command line for many years now and I have learned a lot of Linux command line tricks. Here in this article, I will discuss some Linux command line tricks that I find worth using in my day to day command line activities.
NOTE – All the examples in this article are tested on bash shell.
Linux command line tricks
1. How to switch between directories efficiently?
Working on Linux command line means switching between lot of directories. You are in a directory ‘A’, then you move to directory ‘B’. Now you want to come back to directory ‘A’. Typing the complete directory path for ‘A’ can be cumbersome sometimes. For this you can use ‘cd -’ short cut.
Here is an example :
$ pwdSo we see that it’s easy to switch between two directories using cd- .
/home/himanshu
$ cd /usr/local/bin/
$ cd -
/home/himanshu
But, ‘cd -’ resolves only a partial problem. It can only switch you back to last directory only. What if you switch between multiple directories and then want to come back to the first or some other desired directory? I mean, suppose you are in a directory ‘A’, then you switch to directories ‘B’ -> ‘C’ -> ‘D’ -> ‘E’ and then you want to again go back to directory ‘A’.
Well, for this, you can use the combination of ‘pushd’ and ‘popd’.
Here is an example :
$ pwdAs you can see, first you pass the desired directory (to which you want to come back eventually) as argument to ‘pushd’ and then through ‘popd’ you can actually trigger a directory switch to that directory from anywhere on the command prompt.
/home/himanshu
$ pushd /home/himanshu
~ ~
$ cd /usr
$ cd /tmp
$ cd /proc
$ popd
~
$ pwd
/home/himanshu
2. How to make efficient use of Linux command line history using !! and ! ?
Double exclamation ie ‘!!’ represents the last run command on the shell. Here is an example :
$ uname -a
Linux himanshu-Inspiron-1525 3.2.0-36-generic-pae #57-Ubuntu SMP Tue Jan 8 22:01:06 UTC 2013 i686 i686 i386 GNU/Linux
$ !!
uname -a
Linux himanshu-Inspiron-1525 3.2.0-36-generic-pae #57-Ubuntu SMP Tue Jan 8 22:01:06 UTC 2013 i686 i686 i386 GNU/Linux
So what best can we do with !! ?
Well, firstly, you can extend the command easily. Here is an example :
$ !! | grep Linux
uname -a | grep Linux
Linux himanshu-Inspiron-1525 3.2.0-36-generic-pae #57-Ubuntu SMP Tue Jan 8 22:01:06 UTC 2013 i686 i686 i386 GNU/Linux
Also, it so happens many times that you run a command and you get an error that the command requires root privileges. Then you press the ‘up arrow’ key + home key + write ‘sudo’ . Well all this can be avoided using !!.
Here is an example :
$ touch new_binary
touch: cannot touch `new_binary': Permission denied
$ sudo !!
sudo touch new_binary
[sudo] password for himanshu:
$ ls new_binary
new_binary
Sometimes you would like to append a command to existing shell script or would like to create a new shell script, then you can use ‘!!’ to the task easily. Here is an example :
$ ls -lart /home/himanshu/practice/*.py
-rw-rw-r-- 1 himanshu himanshu 50 Mar 1 00:23 /home/himanshu/practice/firstPYProgram.py
$ echo !! > script.sh
echo ls -lart /home/himanshu/practice/*.py > script.sh
$ cat script.sh
ls -lart /home/himanshu/practice/firstPYProgram.py
So we see that this way !! proves to be easy and time saving.
Now, lets come to single exclamation ie ‘!’ . Unlike double exclamation ie ‘!!’, through single exclamation ‘!’, we can access any previously run command that exists in command line history. Here are some examples :
Use serial number from output of history command to run a particular command
$ historyuname -a | grep Linux
...
...
...
2039 uname -a | grep Linux
2040 dmesg
2041 clear
2042 cd bin
2043 clear
2044 pwd
2045 touch new_binary
2046 sudo touch new_binary
2047 ls new_binary
2048 history
$ !2039
Linux himanshu-Inspiron-1525 3.2.0-36-generic-pae #57-Ubuntu SMP Tue Jan 8 22:01:06 UTC 2013 i686 i686 i386 GNU/Linux
So we see that command number 2039 was run through single exclamation ‘!’ without having to type or copy paste the command again.
You can use negative integer values with ‘!’ to run second last command, third last command, fourth last command…and so on.
Here is an example :
$historyuname -a | grep Linux
...
...
...
2049 ! 2039
2050 uname -a | grep Linux
2051 history
$ !-2
Linux himanshu-Inspiron-1525 3.2.0-36-generic-pae #57-Ubuntu SMP Tue Jan 8 22:01:06 UTC 2013 i686 i686 i386 GNU/Linux
Run a new command with argument of previous command
Here is an example :
$ ls /home/himanshu/practice/*.py-rw-rw-r-- 1 himanshu himanshu 50 Mar 1 00:23 /home/himanshu/practice/firstPYProgram.py
/home/himanshu/practice/firstPYProgram.py
$ ls -lart !$
ls -lart /home/himanshu/practice/*.py
So we see that ‘!$’ can be used to fetch argument from previous command and use it with the current command.
In case of two arguments, use carrot ‘!^’ to access first argument
Here is an example :
$ ls /home/himanshu/practice/*.py /home/himanshu/practice/*.txt-rw-rw-r-- 1 himanshu himanshu 50 Mar 1 00:23 /home/himanshu/practice/firstPYProgram.py
/home/himanshu/practice/file.txt /home/himanshu/practice/output.txt /home/himanshu/practice/sort.txt
/home/himanshu/practice/firstPYProgram.py /home/himanshu/practice/sort1.txt /home/himanshu/practice/test.txt
/home/himanshu/practice/input.txt /home/himanshu/practice/sort2.txt
$ ls -lart !^
ls -lart /home/himanshu/practice/*.py
So we see that through ‘!^’ we can access the first argument of the previous run command.
To access the any other argument (of previous run command) in current command, ‘![prev command name]:[argument number]‘ can be used.
Here is an example :
$ ls !ls:2
ls /home/himanshu/practice/*.txt
/home/himanshu/practice/file.txt /home/himanshu/practice/sort1.txt /home/himanshu/practice/test.txt
/home/himanshu/practice/input.txt /home/himanshu/practice/sort2.txt
/home/himanshu/practice/output.txt /home/himanshu/practice/sort.txt
So this way, the second argument (of the previous command) was accessed.
To access all the arguments of a previously run command, use ‘!*’
Here is an example :
$ ls -lart !*Use ‘![keyword]‘ to run the last command starting with [keyword]
ls -lart /home/himanshu/practice/*.py /home/himanshu/practice/*.txt
-r--r--r-- 1 himanshu himanshu 50 Oct 24 2012 /home/himanshu/practice/output.txt
-r--r--r-- 1 himanshu himanshu 7 Nov 10 13:46 /home/himanshu/practice/input.txt
-r--r--r-- 1 himanshu himanshu 8 Dec 7 20:38 /home/himanshu/practice/sort1.txt
-r--r--r-- 1 himanshu himanshu 8 Dec 7 20:39 /home/himanshu/practice/sort2.txt
-r--r--r-- 1 himanshu himanshu 14 Dec 14 20:45 /home/himanshu/practice/file.txt
-r--r--r-- 1 himanshu himanshu 41 Jan 23 20:42 /home/himanshu/practice/sort.txt
-rw-rw-r-- 1 himanshu himanshu 50 Mar 1 00:23 /home/himanshu/practice/firstPYProgram.py
-rw-rw-r-- 1 himanshu himanshu 0 Mar 10 15:31 /home/himanshu/practice/test.txt
Here is an example :
$ !ls-rw-rw-r-- 1 himanshu himanshu 50 Mar 1 00:23 /home/himanshu/practice/firstPYProgram.py
ls -lart /home/himanshu/practice/*.py
So we see that the last ls command was executed. This way you can just write the first keyword of the command (which is command name usually) and you do not need to write the complete command. Single exclamation ‘!’ will do it for you.
3. Use comma ‘,’ operator wherever it makes life easy
As already said, the comma operator can make life easy for you on Linux command line. Here are some examples :
Convert to lower case
Comma operator can be used to convert the whole string or only the first letter to lower case.
Here is an example :
$ words="Example of comma OPERATOR"example of comma operator
$ echo ${words,}
example of comma OPERATOR
${words,,}
So we see that through a single comma only the first letter was converted to lower case while through double comma, complete string was converted to lower case.
Use comma with file names
Comma operator can be used with file names. A couple of examples are shown below.
$ touch new_file{1,2,3}So we see that comma operator helped in creating three files easily.
himanshu@himanshu-Inspiron-1525:~/practice$ ls new_file*
new_file1 new_file2 new_file3
Similarly, for one of the very popular work people do on command line is to rename files by adding .old or .new temporarily. You can save the time by doing something like :
mv my_filename.{old,new}
This will rename my_filename.old to myfilename.new.
4. How to delete files with leading or trailing spaces?
You might find yourself struggling with deleting files with leading or trailing spaces through ‘rm’ command on Linux command line.
For example :
$ rm tempFileSo we see that rm command says that this file does not exist. But you are pretty confident that file with such name exists. Then the only thing could be that this file name would be having leading or trailing spaces.
rm: cannot remove `tempFile': No such file or directory
You can use double quotes to avoid this problem :
$ rm "tempFile "The above command worked in my case.
Note that if you do not want to use double quotes then ‘\ ‘ can be used. Here is an example :
$ rm tempFile\Remember to add a space after back slash.
5. How to delete files with names beginning with hyphen (-) ?
Sometimes you may find yourself stuck with a situation like this :
You have to delete a file named -1mpFile.out
$ ls
-1mpFile.out CPPfile.o libCfile.so mylinuxbook_new prog split
But, when you try using rm command, following error is produced :
$ rm -1mpFile.out
rm: invalid option -- '1'
Try `rm ./-1mpFile.out' to remove the file `-1mpFile.out'.
Try `rm --help' for more information.
Even if you use double quotes, you get the following error :
$ rm "-1mpFile.out"
rm: invalid option -- '1'
Try `rm ./-1mpFile.out' to remove the file `-1mpFile.out'.
Try `rm --help' for more information.
So, rm command considers the hyphen ‘-’ as an indicator that some command line option will follow and so it treats ’1mpFile.out’ as an option. Hence the error.
Now, to tell ‘rm’ that the word beginning with hyphen is file name, you need to pass double hyphen (–) first. Here is an example :
$ rm -- -1mpFile.out
So this should remove the file successfully.
Since this problem is generic ie you will observe this problem even while creating this file using ‘touch’ command etc. Double hyphen can be used with other commands too for the same purpose.
Here is another example of double hyphen but this time with touch and ls commands :
$ touch -1mpFile.out
touch: invalid option -- '1'
Try `touch --help' for more information.
$ touch -- -1mpFile.out
$ ls — -1mpFile.out -1mpFile.out
So we can safely use double hyphen (–) in a generic sense with different Linux commands.
6. How to delete all files in a directory except some (with particular extensions) ?
Suppose you have a directory with lot of files and you want to delete all the files except some of them (with particular file extensions). This can be done in following way :
Here is a directory containing lot of files :
$ ls
a.out Cfile.c file.c macro.c my_printf.c orig_file.orig stacksmash.c
bfrovrflw.c cmd.c firstPYProgram.py main.c new_printf.c orig_file.rej test_strace.c
bufrovrflw.c env.c helloworld.c my_fopen.c new.txt prog.c virtual_func.c
Now, you want to delete all the files except .c and .py files.
Here is what you can do :
$ rm !(*.c|*.py)So you can see that files with all other extensions got deleted.
$ ls
bfrovrflw.c Cfile.c env.c firstPYProgram.py macro.c my_fopen.c new_printf.c stacksmash.c virtual_func.c
bufrovrflw.c cmd.c file.c helloworld.c main.c my_printf.c prog.c test_strace.c
7. How to create customized backup using touch and find commands?
Touch command in association with find command can be used to create customized backups.
Suppose you want to create a backup of files that you created or changed in a directory between 9am and 5pm. For this, the very first step is to create two files temp1 and temp2 with timestamps as 9am and 5pm respectively.
$ touch -d "9am" temp1So we see that timestamps was as expected. Now move to the directory where you want to create the backup of files. Here are the contents of the directory in my case :
$ touch -d "5pm" temp2
These commands will create two files temp1 and temp2 with access and modification timestamps as 9am and 5pm respectively.
Let’s cross check these by using stat command:
$ stat temp1
File: `temp1'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 806h/2054d Inode: 528534 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/himanshu) Gid: ( 1000/himanshu)
Access: 2013-04-28 09:00:00.000000000 +0530
Modify: 2013-04-28 09:00:00.000000000 +0530
Change: 2013-04-28 19:06:05.982909491 +0530
Birth: -
$ stat temp2
File: `temp2'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 806h/2054d Inode: 529476 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/himanshu) Gid: ( 1000/himanshu)
Access: 2013-04-28 17:00:00.000000000 +0530
Modify: 2013-04-28 17:00:00.000000000 +0530
Change: 2013-04-28 19:06:12.090939793 +0530
Birth: -
$ lsbufrovrflw.c cmd.c file.c helloworld.c main.c my_printf.c prog.c test_strace.c
bfrovrflw.c Cfile.c env.c firstPYProgram.py macro.c my_fopen.c new_printf.c stacksmash.c virtual_func.c
Now, I create a directory named ‘bkup’ and run the following command :
$ find . -newer ../temp1 ! -newer ../temp2 -exec cp '{}' ./bkup/ ';'The -newer and ! -newer options in command above will first find all the files with modification time between 9am and 5pm. Then the -exec option makes sure that the cp command is run for every result (‘{}’) of find command and the file is copied to ./bkup/ folder. The terminating ‘;‘ is the indication that cp command terminates here.
Now, if you see the ‘bkup’ directroy, you’ll find all the backed up files there. Here is what I saw in my case :
$ cd bkup/bfrovrflw.c Cfile.c env.c firstPYProgram.py macro.c my_fopen.c new_printf.c stacksmash.c virtual_func.c
$ ls
bufrovrflw.c cmd.c file.c helloworld.c main.c my_printf.c prog.c test_strace.c
As all the files were created between 9am and 5pm so all of them were backed up.
8. Why rm command fails with error ‘Argument list too long’?
This usually happens when you have a directory containing huge number of files. When you do a rm -rf over it, you get something like :
-bash: /bin/rm: Argument list too long
This issue can be resolved using following command (please switch over to the desired directory before running this command):
find * -xdev -exec rm -f '{}' ';'
The find command above will supply input to rm command in batches that it can process. This is one of the fastest method to delete files.
9. How to search for all the files in a directory containing a particular string?
This can be easily achieved using grep command.
Here are a couple of examples :
$ grep -l "printf" *.cSo we see that even the lines containing the string “buff” were displayed in the output.
bfrovrflw.c
bufrovrflw.c
Cfile.c
cmd.c
env.c
file.c
helloworld.c
macro.c
main.c
my_fopen.c
my_printf.c
new_printf.c
prog.c
stacksmash.c
test_strace.c
$ grep -l "buff" *.c
bfrovrflw.c
If it is desired to view the lines where the string is used in the file, then ‘find’ command can be used with ‘xargs’ and ‘grep’ command in the following way :
$ find ./ -name "*.c" | xargs grep "buff"
./bfrovrflw.c: char buff[15];
./bfrovrflw.c: gets(buff);
./bfrovrflw.c: if(strcmp(buff, "MyLinuxBook"))
10. How to Empty a file using ‘>’ operator ?
Suppose you want to empty a file from command line.
Here is how easily you can do it :
$ > [complete file path]
For example :
$ > ./logfile
This will delete all the contents of the file ‘logfile’ and empty it.
11. How to search man pages for a particular string?
You might have used Linux man pages to understand more about a command, function etc. But, what if you want to know which man pages discuss about a particular topic. For example, what if I want to know that which man pages discuss about ‘login’?
Well, there exists an option -k through which you can do this. Here is an example :
$ man -k login...
access.conf (5) - the login access control table file
add-shell (8) - add shells to the list of valid login shells
chsh (1) - change login shell
faillog (5) - login failure logging file
faillog (8) - display faillog records or set login failure limits
getlogin (3) - get username
getlogin_r (3) - get username
gnome-session-properties (1) - Configure applications to start on login
hotot (7) - lightweight & opensource microbloging client
issue (5) - prelogin message and identification file
lastlog (8) - reports the most recent login of all users or of a given user
login (1) - begin session on the system
login (3) - write utmp and wtmp entries
login.defs (5) - shadow password suite configuration
login_tty (3) - tty utility functions
logname (1) - print user's login name
...
...
So we see that all the man pages that discuss about ‘login’ were displayed in the output.
12. How to redirect stderr output messages to a file?
It so happens mostly that standard commands/programs/services stream normal log messages to stdout while error log messages to stderr stream. Now, if you just do something like :
$ [some-command] > logfileThen only the messages that were directed to stdout would be redirected to the file ‘logfile’ but no message that was directed to stderr would be redirected to the file.
Here is an example :
$ touch new > /home/himanshu/practice/logfile
touch: cannot touch `new': Permission denied
$ cat /home/himanshu/practice/logfile
$
So we see that error was not redirected to the log file.
Now, to correct this, do something like :
$ touch new > /home/himanshu/practice/logfile 2>&1
$ cat /home/himanshu/practice/logfile
touch: cannot touch `new': Permission denied
So we see that this time the error was redirected to the file successfully. Please note that 2>&1 combines both stdout and stderr streams to stdout stream only.
13. How to follow multiple log files on the go?
If it is required to follow multiple log files as they are being updated then this can be done through tail command.
Suppose I want to monitor two log files ‘logfile’ and ‘logfile1′ simultaneously then I will use the tail command as follows :
$ tail -f logfile logfile1
==> logfile <==
==> logfile1 <==
==> logfile <==
hi
==> logfile1 <==
hello
So you can see that dynamic updates to these log files can easily be monitored through tail command.
14. How to make a command not to show up in the output of ‘history’ command?
Well, sometimes you would want to run a command but do not want it to appear in the output of Linux history command.
You can achieve this by inserting a space before you type the command on prompt.
Here is an example :
$ df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda6 29640780 6174904 21960188 22% /
udev 1536752 4 1536748 1% /dev
tmpfs 617620 892 616728 1% /run
none 5120 0 5120 0% /run/lock
none 1544040 156 1543884 1% /run/shm
Note that there is a space between ‘$’ and ‘df’.
Now, let’s confirm whether this command appears in the output of ‘history’ :
$ history | grep dfThe df command was not captured in the output of history command.
1633 ls *.pdf
1634 mv LinuxCommandsPart1.pdf LinuxCommandsPart1
2245 history | grep df
15. How to simulate on-screen typing just like you see in movies?
Well, to simulate typing just like you see in movies, use ‘pv’ command.
Try this out :
echo "You can simulate on-screen typing just like in the movies" | pv -qL 10
Check the output for yourself. :-)
16. How to escape the aliased version of a command?
The alias command, as we all know is used to create aliases of the commands that act as short cuts and save time.
For example, I have created alias of ls command such that whenever I execute ls, its ‘ls -lart’ that gets executed.
$ alias ls='ls -lart'
Now, if I ever intend to escape the alias and want to execute only ls, then I can do this by beginning the command with a backslash ie ‘\’.
Here is an example :
$ \ls
1 CPPfile.o libCPPfile.so mylinuxbook_new prog.c stacksmash.c
So we see that the original ls command was executed.
NOTE – If you want to suppress an alias for a whole login session, you can use ‘unalias’ command for that.
17. How to get rid of that unknown process that forbids you to delete a file?
There are situations where you want to delete a file but you get an error like ‘file is already in use’. You try to find which process in using this file but all your effort goes in vain. What would you do in this case?
Well, you can use the ‘fuser’ command. It tell you the process that is using a particular file. You can use ‘fuser -k [filename]‘ command to kill that process.
Read our article on fuser command to know more about it.
18. How to monitor and redirect logs to file simultaneously?
If you want to redirect logs to a file but also want to monitor them in parallel, you can use the Linux ‘tee’ command.
Here is an example :
./program | tee logfile
The command above will execute the ‘program’. All the logs will be redirected to ‘logfile’ while you can simultaneously view them on stdout (command line in most of the cases) also.
19. How to check if a command succeeded or failed?
Well this can be done through ‘$?‘ environment variable. It holds 0 if last run command was success else it holds a non zero value in case of failure.
Here is an example :
$ touch abc
touch: cannot touch `abc': Permission denied
$ echo $?
1
As the last command failed, so the output was ’1′.
20. How to copy paste on command line through keyboard?
You can copy the text by selecting it first and then by pressing :
CTRL + SHIFT + C
and paste it using :
CTRL + SHIFT + V
By : Unknown
Now the life of computing is getting transformed from phase to phase , to an advanced level.The ease of use of many devises increases specially Android and other mobile computing platforms gave birth to advancement in Mobile computing , With advancements it gave birth to exhaustion of resources...This demo illustrated how the way it works Click here !!!
| e-Cloud |
Now the life of computing is getting transformed from phase to phase , to an advanced level.The ease of use of many devises increases specially Android and other mobile computing platforms gave birth to advancement in Mobile computing , With advancements it gave birth to exhaustion of resources...This demo illustrated how the way it works Click here !!!

