Team OS : Your Only Destination To Custom OS !!

Welcome to TeamOS Community, Register or Login to the Community to Download Torrents, Get Access to Shoutbox, Post Replies, Use Search Engine and many more features. Register Today!

Tips & Tricks 21 simple yet powerful Linux commands!!

mobi0001

The Power Is Yours!!!
Uploader
Power User
✅ Verified Member
Member
Downloaded
62.3 GB
Uploaded
11.3 TB
Ratio
186.25
Seedbonus
1,014
Upload Count
89 (104)
Member for 4 years
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.

Some of these tips depend on how your shell is configured.
  • Using tab to autocomplete
When you are typing 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.

For example, if you are trying to copy a file named my_test_file_1.txt, you can just type ‘cp m’ and hit tab to see the possible options.

You can use tab in completing commands as well. In case there is only 1 file, the tab key pressed twice also enables to complete the command.
  • Switch back to the last working directory
Suppose you end up in a long directory path and then you move to another directory in a totally different path. And then you realize that you have to go back to the previous directory you were in. In this case, all you need to do is to type:
Code:
cd -

This will revert to the last working directory. You don’t need to type the long directory path or copy paste it anymore.
  • Go back to home directory
You can use the command below to move to your home directory from anywhere in Linux command-line:

Code:
cd ~

However, you can also use just cd to go back to home directory:

Code:
cd

Most modern Linux distributions have their shell pre-configured for this command. It saves you at least two keystrokes.
  • List the contents of a directory
Presumably only ls -l, right?

However, if your shell is configured to allow, then use:

Code:
ll
- Double L
  • Running multiple commands in one single command
This will help you to run multiple Linux 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 the execution.

Code:
command_1; command_2; command_3
  • 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?

Imagine a situation where you want to build a code and then if the build was successful, run the make?

You can use the && separator for this case. && makes sure that the next command will only run when the previous command was successful.

Code:
command_1 && command_2

A perfect example of this command is when you use sudo apt update && sudo apt upgrade to upgrade your system.
  • Easily search and use the commands that you had used in the past
Imagine a situation where you used a long command couple of minutes/hours ago and you have to use it again. Problem is that you cannot remember the exact command anymore.

Reverse search is your savior here. You can search for the command in the history using a search term.

Just use the keys ctrl+r to initiate reverse search and type some part of the command. It will look up into the history and will show you the commands that matches the search term.

Code:
ctrl+r search_term

By default, it will show just one result. To see more results matching your search term, you will have to use ctrl+r again and again. To quit reverse search, just use Ctrl+C.

Note that in some Bash shells, you can also use Page Up and Page Down key with your search term and it will autocomplete the command.
  • Unfreeze your Linux terminal from accidental Ctrl+S
You probably are habitual of using Ctrl+S for saving. But if you use that in Linux terminal, you’ll have a frozen terminal.

Just use Ctrl+Q and you can use the terminal again.

Code:
ctrl+Q
  • Move to beginning or end of line
Suppose you are typing a long command and midway you realize that you had to change something at the beginning. You would use several left arrow keystrokes to move to the start of the line. And similarly for going to the end of the line.

You can use Home and End keys here of course but alternatively, you can use Ctrl+A to go to the beginning of the line and Ctrl+E to go to the end.
  • Delete entire line from cursor position
So many people either do not know about it or hardly use it.

In the Linux terminal, if you press Ctrl+U, it deletes everything from your current cursor position to the beginning of the line.

Similarly, if you press Ctrl+K, it deletes everything from your cursor position to the end of the line.

Possible made a mistake in typing the password? Instead of using backspace key all the way, simply use Ctrl+U and retype the password.
  • Reading a log file in real time
In situations where you need to analyze the logs while the application is running, you can use the tail command with -f option.

Code:
tail -f path_to_Log

You can also use the regular grep options to display only those lines that are meaningful to you:

Code:
tail -f path_to_log | grep search_term

You can also use the option F here. This will keep the tail running even if the log file is deleted. So if the log file is created again, tail will continue logging.
  • Reading compressed logs without extracting
Server logs are usually gzip compressed to save disk space. It creates an issue for the developer or sysadmin analyzing the logs. You might have to scp it to your local and then extract it to access the files because, at times, you don’t have write permission to extract the logs.

Thankfully, z commands save you in such situations. z commands provide alternatives of the regular commands that you use to deal with log files such as less, cat, grep etc.

So you get zless, zcat, zgrep etc and you don’t even have to explicitly extract the compressed files.
  • Use less to read files
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.

Code:
less path_to_file

You can search for terms inside less, move by page, display with line numbers etc.
  • 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 create a directory and then go into the newly created directory. There you can use the !$ options.

A better way to do the same is to use alt+. . You can use . a number times to shuffle between the options of the last commands.
  • 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.

Another way if your shell configuaration provides is by pressing the up arrow key, then home key and typing sudo. Though this is a longer method, it works if one cannot recall !! command.
  • Using alias to fix typos
You might often mistype grep as gerp. If you put an alias in your bashrc in this fashion:

Code:
alias gerp=grep

This way you won’t have to retype the command again.
  • Copy Paste in Linux terminal
This one is slightly ambiguous because it depends on Linux distributions and terminal applications. But in general, you should be able to copy paste in terminal with these shortcuts:

Select the text for copying and right click for paste (works in Putty and other Windows SSH clients)
Select the text for copying and middle click (scroll button on the mouse) for paste
Ctrl+Shift+C for copy and Ctrl+Shift+V for paste

  • Kill a running command/process
This one is perhaps way too obvious. If there is a command running in the foreground and you want to exit it, you can press Ctrl+C to stop that running command.
  • Using yes command for commands or scripts that need interactive response
If there are some commands or scripts that need user interaction and you know that you have to enter Y each time it requires an input, you can use Yes command.

Just use it in the below fashion:

Code:
yes | command_or_script

Another example:
Code:
sudo apt-get install -y php7.4*
  • 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:

Code:
> filename

Find if there are files containing a particular text

There are multiple ways to search and find in Linux command line. But in the case when you just want to see if there are files that contain a particular text, you can use this command:

Code:
grep -Pri Search_Term path_to_directory
  • Using help with any command
Almost all command and command line tool come with a help page that shows how to use the command. Often using help will tell you the basic usage of the tool/command.

Just use it in this fashion:

Code:
command_tool --help
 

Chuck

🤴 Super Admin
Downloaded
300.6 GB
Uploaded
2.9 TB
Ratio
9.91
Seedbonus
591,311
Upload Count
24 (26)
Member for 5 years
I have never used Linux so I'll take your word for it that these are useful. ;)
 

mobi0001

The Power Is Yours!!!
Uploader
Power User
✅ Verified Member
Member
Downloaded
62.3 GB
Uploaded
11.3 TB
Ratio
186.25
Seedbonus
1,014
Upload Count
89 (104)
Member for 4 years
I have never used Linux so I'll take your word for it that these are useful. ;)
I use 3 different distros + windows. You can trust me mate. ;)
 

PsyTom

Power User
✅ Verified Member
Member
Downloaded
1.4 TB
Uploaded
502.1 TB
Ratio
367.69
Seedbonus
1,662,990
Upload Count
0 (0)
Member for 3 years
nice, keep up such good work.
 
Top