DEV Community

Anish Kumar
Anish Kumar

Posted on • Edited on

One day One Command(CLI)

Day 1: pwd
1.pwd: This command stands for Print Working Directory. As the name suggests it helps us know the current/working directory.
Syntax: pwd
1 a.pwd -P: Stands for pwd physical. It returns the current physical directory ignoring all the symbolic links in the path.
Syntax: pwd -P
1 b.pwd -L: Stands for pwd logical. It returns the current logical directory completely preserving the symbolic links in the path.
Syntax: pwd -L

Day 2: poweroff

$ sudo poweroff The poweroff command shuts down the system safely and immediately terminating all processes where unsaved work could be lost. It also unmounts all file systems then powers off the hardware. It is a quick way to turn off the system safely. Users in most cases cannot operate this command without sudo.
$ sudo poweroff --force This command shuts the down the system immediately without terminating any processes. It is generally unsafe to do so. It could be used if the system is unresponsive.
$ sudo poweroff --halt This command works just the way poweroff works but it does not finally cut the power to the hardware systems. It could be used if you want to terminate all the current running processes.

Day 3: ls

  1. ls: The ls command stands for list and it is used to list the files and directories. If used simply without any context it lists the files and directories in the current working directory. It can also be used to list files in other directories by following it up with the path to the directory. To list the files and directories of a file that requires root access we can use sudo on which otherwise we get permission denied. Syntax:ls ls /path/of/directory sudo ls /path/of/restricted directory 3 a.ls -l: This command does the same as ls files with more details. It gives more details such as permissions, ownership, size, date last modified, etc. Syntax:ls -l 3 b.ls -a:This command like the ls file also shows files and directories including hidden ones. Those files usually start with a '.'. Syntax:ls -a 3 c.ls -lh:It is just like ls -l but is in a more human readable format. The sizes are mentioned in kb,mb, etc.

Day 4: cat

  1. cat: This command stand for concatenate. When used on a file it shows the content of the file. If used on executable files or on files containing images they usually return garbled text and symbols as it is interpreting binary data. This command can also copy the content of a file and append it to another file. cat file1 >> file2 appends the contents of file1 to file2. It can also combine two files and copy it to another file. cat file1 file2 > newfile combines the contents of file1 and file2 in newfile. Syntax: cat filename cat file1 >> file2 cat file1 file2 > newfile 4 a.cat -n: This command does the same as cat and numbers all the lines in the output. Syntax: cat -n filename 4 b.cat -b: This command does the same as cat -n but it doesn't number the blank lines. Syntax: cat -b filename 4 c.cat -s: This command does the same as the cat command but it suppresses repeated blank lines into only one blank line. Syntax: cat -s filename 4 d.cat -T: This command does the same as cat but show tabs as ^I helping us differentiate between tabs and spaces. Syntax: cat -T filename 4 e.cat -E: This command is similar to cat -n and instead of numbering all the lines it leaves the $ symbol at the end of a line to help us distinguish between the end of a line or if they just continue. Syntax: cat -E filename 4 f.cat -A: This command is a combination of cat -E and cat -T where it shows the end of each line by leaving a $ symbol and helps distinguishing spaces from tabs by replacing tabs with ^I. Syntax: cat -A filename

Some of these options can be used together.
For eg: cat -ns numbers the lines from start and shortens the repeated blank lines to just one and the numbering is done on the remaining blank line. cat -ET does the same as cat -A. cat -bE numbers only the non blank lines leaves a $ symbol at the end of a line, etc.

Day 5: echo

  1. echo: The echo command sends a message or a variable to all active users or can be used to create or overwrite files. Syntax: echo "Hi everyone" echo $HOME echo "the text" > filename.extension

5 a.echo -e: This command works the same way as echo does but it takes in escape sequences as well. For eg: \n,\t,etc.
Syntax: echo -e "First Line\nNext Line"

Day 6: head and tail

=====================================

I'm sorry to have missed yesterday. So today I will be doing 2 commands.

=====================================

  1. head: It displays the first few lines of a file it is used with. The default number of lines it shows is 10. If used on a file containing binary data it outputs garbled texts and symbols. When used on multiple files it will display each file with a file header.

Syntax: head file.extension
head file1.txt file2.txt

=====================================

6 a. head -n: It behaves the same as head but instead of the default number of lines, it displays the number of lines mentioned. If the mentioned number exceeds the data in the particular file then it displays all of the file without any errors. If this command is used on multiple files it outputs the number of lines in each of the files.

Syntax: head -n file.extenstion
head -n 5 file.txt

=====================================

6 b. head -c: It behaves similar to head -n but instead of the lines it takes number of bytes which the user has mentioned. Similarly, it displays the entire file if the number of bytes mentioned is more than what is available without any errors.

Syntax: head -c file.extension
head -c 25 abc.txt

=====================================

The commands after this may not work for non GNU systems.

=====================================

6 c. head -q: When this is used on multiple files it suppresses the file headers. It can also be used in combination with head -c or head -n.

Syntax: head -q file1.txt file2.txt
head -q -n 2 file1.txt file2.txt

=====================================

6 d. head -v: When this is used even on a single file then the file headers are displayed. If used together with head -q, head -v has the higher priority in whatever order it is done. So head -q -v or head -v -q will always display the headers.

Syntax: head -v file.txt

=====================================

  1. tail: This command is similar to head command with the only difference being it displays the last few lines.

Syntax: tail file.txt

=====================================

7 a. tail -n: This like the tail command instead of the last default number of lines we can specify the number of lines we want.

Syntax: tail -n file.txt

=====================================

7 b. tail -c: This command outputs the last number of specified bytes of the file.

Syntax: tail -c file.txt

=====================================

The commands after this may not work on non GNU systems.

=====================================

7 c. tail -f: This command works similar to the tail command, at the beginning it only displays the last 10 lines but it will continue to give more outputs as the file grows. This can be useful for growing log files. If data is overwritten instead of appended to the said file, tail -f may not work properly.

Syntax: tail -f file.extension

=====================================

7 d. tail -F: This is just a better version of tail -f. If it encounters any errors such as the file is temporarily deleted or being overwritten it retries and when the file is available again, it starts the process again.

Syntax: tail -F file.extension

=====================================

7 d i. tail -F/f --max-unchanged-stats: As tail -F keeps on checking the file indefinitely, it can be stopped using this command. We can issue the number of times tail -F can retry and if there is constantly no updates in the file it will stop retrying.

Syntax: tail -F --max-unchanged-state=5 file.extenstion

=====================================

7 d ii. tail -F/f --pid: This command is the same as tail -F but it stops retrying after a specific process ends.

Syntax: tail -F --pid= file.extension
tail -F --pid==123 logfile.txt

=====================================

7 e. tail -q: This suppresses the file headers if multiple files are used.

Syntax: tail -q file1.extension1 file2.extension2

=====================================

7 f. tail -v: This forces the file headers in the output even if only one file is used.

Syntax: tail -v file.extension

*************************************

Day 7: grep

=====================================

  1. grep: This command is used to look for a given pattern in a files or output lines.

Syntax: grep "pattern" filename

=====================================

8 a. grep -i: This command works the same way as grep but it is case insensitive.

Syntax: grep -i "PaTtern" filename

=====================================

8 b. grep -r: This command searches for the given pattern recursively completely in a given directory. It searches every file or director that is contained in the given directory and gives its results.

Syntax: grep -r "pattern" filename

=====================================

8 c. grep -n: This command does the same as the grep command but it also numbers the output lines.

Syntax: grep -n "pattern" filename

Top comments (0)