0% found this document useful (0 votes)
5 views3 pages

Trick and Shorcuts For Cli

This document provides essential tips for using the Command Line Interface, including techniques like tab completion, command history, and understanding commands. It covers file operations, navigation shortcuts, piping, redirection, and system information gathering. Additionally, it emphasizes caution with destructive commands and the importance of practicing in safe environments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Trick and Shorcuts For Cli

This document provides essential tips for using the Command Line Interface, including techniques like tab completion, command history, and understanding commands. It covers file operations, navigation shortcuts, piping, redirection, and system information gathering. Additionally, it emphasizes caution with destructive commands and the importance of practicing in safe environments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Here are couple simple tricks to remember while using Command Line Interface.

1. Tab Completion:
• Use the Tab key to autocomplete commands, file names, and directories. It
saves time and reduces errors.
• For example, if you type ‘ec’ and hit ‘tab’ it will complete the command as ‘echo’

2. Command History:
• Utilize the up and down arrow keys to cycle through previously used
commands. History displays a list of recent commands and executes a specific
command by its number.

3. Understanding Commands:
• Use [command] --help to access command manuals and understand their
usage, options, and syntax.
• ls –help will give you all the flags that you can use for the list command.

4. File and Directory Operations:


• ‘.’ represents the current directory, and ‘..’ represents the parent directory. Use
them in paths accordingly.
• mkdir creates directories, while touch creates empty files.

5. Working with Files:


• Use wildcards (*, ?) for matching multiple files/directories based on patterns.
• Redirect output using > (overwrite) or >> (append) to write command output to
a file.
• Example: echo ‘hello’ > mytext.txt
echo ‘world’ >> mytext.txt
• The content in mytext.txt will be:
hello
world

6. Navigation Shortcuts:
• cd ~ navigates to the home directory, and cd - switches to the previous
directory.

7. Piping and Redirection:


• Use | to pipe command output to another command and > or >> for output
redirection.
• For example: there are few files: example.pdf, test_example.pdf, test.pdf
• ls | grep “example”
• this will list only example.pdf, test_example.pdf and not test.pdf.

8. File Permissions:
• Understand the chmod command to change file permissions (+x, -r, etc.) and
chown to change ownership.

9. Be Cautious with rm Command:


• Be extremely cautious when using rm. Always double-check the command and
options before deleting files/directories. rm -rf can't be undone.

10. Keyboard Shortcuts:


• Ctrl + C terminates a running command, and Ctrl + Z pauses it.
• Ctrl + R initiates a reverse search in command history.

11. System Information:


• Use commands like uname -a, df, free, and top to gather system information.

12. Checking the distribution of the terminal.


• Some commands might vary depending on distribution used for the terminal for
example, if you would like to install Nodejs.
• sudo apt install nodejs npm …. This works for Ubuntu.
• sudo apk add nodejs npm … this works for Alpine Linux

To check the distribution, look into the os-release. Or simply put ‘cat etc/os-release’
this will show the type of distribution the terminal is using and accordingly use the
commands.

12. Practice in a Safe Environment:


• Avoid experimenting with potentially destructive commands on critical systems.
Use test environments or online sandboxes for practice. Use Docker to practice
your commands, you can practice installing too on this terminal.

13. Useful Flags:


• Many commands have flags or options (e.g., -l, -a, -h). Get familiar with
common flags to customize command behavior.

14. xargs.
• xargs is a powerful command used in Linux to build and execute command lines
from standard input. It's particularly useful when you want to convert input
from other commands into arguments for a command that doesn't accept input
directly.
• Example: if you want to copy one file into several locations, you can use xargs.
xargs -n 1 cp -v file.txt <<<”directory1/my_folder/ directory1/recovery/”

this copies file.txt in both the location directory1/my_folder/ and


directory1/recovery/

15. find

• The find command in Linux is a versatile tool used to search for files and
directories within a filesystem hierarchy. It's powerful and offers various
options to filter results based on criteria like filenames, sizes, modification
times, and more.

• Example: find directory/ -name “file.txt” -delete.


This is finds the “file.txt” in directory/ and then deletes it.
16. paste
• The paste command in Linux is used to merge lines from multiple files. It
concatenates lines from each file side by side, separated by a delimiter (by
default, a tab).
• For example: paste -d’ ‘ file.txt test.txt > sample.txt
This will paste the content of file.txt and test.txt to sample.txt line by line with a
delimiter ‘ ‘.

Remember if the terminal says that your command is not found, here are the
following reasons:
1. The tool for the running the command is not installed, for example node
code2.js is resulting in command not found then Nodejs is not installed.

2. The distribution might be wrong for example, sudo apt install (this is for ubuntu
Linux) might not always work, check the distribution, if it is Alpine Linux then
you can use sudo apk add.

3. The command may be used on the wrong OS, Microsoft command may vary
from Linux commands.

4. The command does not exist.

You might also like