Here are some tiny but useful Linux commands, terminal tricks and shortcuts that will save you a lot of time while working with Linux command line. You might already know a few of these Linux command tips or perhaps all of it. In either case, you are welcome to share your favorite tricks in the comment section. Some of these tips also depend on how the shell is configured. Let’s begin!
01. Switch back to the last working directory
This will put you back in the last working directory. You don’t need to type the long directory path or copy paste it anymore.
02. Switch back to home directory
You can use the command below to move to your home directory from anywhere in Linux command-line. Most modern Linux distributions have the shell pre-configured for this command. Saves you at least two keystrokes here.
03. Use Tab + Tab for auto-completion
When you are starting to type something in Linux terminal, you can hit the tab key and it will suggest all the possible options that start with string you have typed so far.
04. Running multiple commands in one single command
Suppose, you have to run several commands one after another. You can use the ‘;’ separator for this purpose. This way, you can run a number of commands in one line. No need to wait for the previous commands to finish their business.
05. Running multiple commands in one single command only if the previous command was successful
In the previous command, you saw how to run several commands in one single command to save time. But what if you have to make sure that commands don’t fail?
You can use ‘&&’ separator for this case. ‘&&’ makes sure that the next command will only run when the previous command was successful.
You can use ‘&&’ separator for this case. ‘&&’ makes sure that the next command will only run when the previous command was successful.
06. Use less instead of cat to read files on terminal
To see the contents of a file, cat is not the best option especially if it is a big file. cat command will display the entire file on your screen.
You can use Vi, Vim or other terminal based text editors but if you just want to read a file, less command is a far better choice.
You can use Vi, Vim or other terminal based text editors but if you just want to read a file, less command is a far better choice.
07. Reuse the last item from the previous command with !$
Using the argument of the previous command comes handy in many situations.
Say you have to check directory content then you decided to move to that directory. see in below image example.
Say you have to check directory content then you decided to move to that directory. see in below image example.
08. Reuse the previous command in present command with !!
You can call the entire previous command with !!. This comes particularly useful when you have to run a command and realize that it needs root privileges. A quick sudo !! saves plenty of keystrokes here.
09. Execute the latest command entered earlier starting with <text>
Use !<text> to execute the latest command from history that starting with <text>. here <text> is variable argument that you have to pass.
for example, firefox command will open firefox browser. then !fi will do the same if firefox command is in history.
for example, firefox command will open firefox browser. then !fi will do the same if firefox command is in history.
10. Empty a file without deleting it
If you just want to empty the contents of a text file without deleting the file itself, you can use a command similar to this:
11. Create multiple subdirectories in one Linux command
If you want to create a directory containing several subdirectories, or a directory tree, using the command line in Linux, generally you have to use the mkdir command several times. However, there is a faster way to do this.
12. Remove multiple subdirectories in one Linux command
If you want to remove several subdirectories within another directory using the command line in Linux, generally you have to use the rm command several times. However, there is a faster way to do this.
13. Clear bash history
Sometimes you might want to hide the history of commands you typed in the shell on Linux. This is useful when you are building a server image to roll out multiple virtual machines.To clear the Bash History, use this combination of command
14. Finding installation date and uptime in Linux
To find the uptime in Linux, simply you have to run uptime command. To find the installation date in Linux, you need to find a file, folder, or log that was created during the installation time. One such file can be accessed by running the following command. This makes it easy to spot the installation date of your Linux-based operating system.
15. Check Laptop Battery Details and Status In Terminal
First command given below will show all details about battery installed in your device. However, you can only display the status of the battery by with combination of upower and grep commands as shown in second command below.
16. Convert a given string or URL into an ASCII QR code
Here is how we can convert a given string or URL into ASCII QR code in Linux using command line.
17. Get notified when a command is completed
It is good for monitoring commands that takes long time to complete. Use notify-send to get notified when a command execution is completed. add notify-send with your custom message in below format at the end of your command(s).
18. Show file permission in octal format
File permissions in Linux can be displayed in octal format using Linux stat command as shown below.
19. Download whole website using wget
If you ever need to download an entire Web site, perhaps for off-line viewing, wget can do the job. you can modify all the options as per your requirement.
20. Change file permission only for directory or file in specified directory
You can change file permission using chmod easily for particular file or directory, but if you try to use '-R' flag then it will assign the specified file permission to all sub_directories and files under the directory.
if you wish to change file permission only for files or directory under specified directory use below commands.
if you wish to change file permission only for files or directory under specified directory use below commands.
Comments
Post a Comment