If you are using bash, here are some time saving tips that can come handy in day to day tasks of every Linux user. This list is not exhaustive in any manner and is compiled from various Q&A sites and forums.
Use CTRL+R to search through command history. Hit it again to view next search result.
Use hashtags to organize your bash history and to save yourself from typing long commands repeatedly. Just append a hashtag after your command. Example :
tail -f /var/log/apache2/error.log #ApacheLog
Now you can search it later in reverse search. Just hit CTRL+R and type ApacheLog
Tired of typing clear every time you want to clear your terminal screen, just hit CTRL+L. It also not get stored in your command history.
Hit CTRL+W to remove the last word and CTRL+U to remove the entire line.
Need to execute the last command with sudo, use sudo !!
ls -l /root
sudo !! # This is equivalent to sudo ls -l /root
(!! actually repeats the last executed command).
You can get the arguments of your last executed command by !$.
Suppose you created a directory :
mkdir /path/to/directory
You might want to cd into the directory you have just created, you can just :
cd !$
Alternatively, you can also use +.(period). Pressing it repeatedly gives arguments of previous commands chronologically.
If you are working with long directory paths and switch frequently, instead of using cd and typing the long path names again and again, use pushd and popd. These are the built-ins which allows you to maintain a stack of directories you have worked with. Example :
pushd /home/a/b/c/d/e/f
This will push your current directory onto your directory stack and move to the directory specified in the argument i.e. f. Now you can come back by simply using popd.
popd
The complete directory stack can be viewed by dirs. The first entry will always be the current directory and the stack is displayed from second entry onwards.
Quickly locate a file on disk
locate filename
Suppose you start entering a command in your terminal, but midway you realize that you want to execute another command before this one a) Enter the command you want to postpone executing. b) Hit CTRL+U c) Execute one or more commands. d) Hit CTRL+Y
If you need to compare two files and does not have any file comparison program installed, you can use diff command. Example :
diff -y myfile1 myfile2
and it will display the files side by side in two columns with differences marked.
0 Comment(s)