Kerem Demirtürk
5 min readNov 12, 2022

--

Useful Commands for Troubleshooting

Hey,

In this article, you will find useful commands for Troubleshooting.

Sometimes in our local machine, sometimes at job etc. We have to investigate some errors or logs for find clue or understand the what’s going on so in this article I will introduce some commands that I used in my life. Let’s start!!!

  • vi

Vi is a text editor, probably you saw many memes or read some horrible stories about Vi but It’s not that hard, believe me. There are some parameters you need to learn and once you learn them you will get used to using Vi.

vi;
i -> Insert mode
ESC -> Terminate insert mode
dd -> delete line
:q! -> Exit without save
:wq -> Exit with save
/<search> -> Search
k -> Move cursor up
j -> Move cursor down
h -> Move cursor left
l -> Move cursor right

Let’s create scenario; You have to create a file which called example.txt with Vi and you have to display on terminal and need to learn what is the size of this txt file. Its very easy example but if we want to used to it we should start with basic examples.

Let’s create another scenario; You have to examine your production Nginx configuration and you did something wrong when you are insert mode on Vi. What should you do as a first step? or What should you do before the change something in your configuration? I prepared an example video for this example but please before the watching my video, figure out the problem yourself.

I prepared one more video about searching something on Vi and Less, you will realise searching something on Vi and Less is very similar but of course there is some extra features on Vi but we don’t need to think right now. Please watch the video and try out on your terminal.

  • nano

Nano is a text editor like Vi but its very different and easy to use. Nano is also a WYSIWYG command-line text editor. WYSIWYG means What You See Is What You Get. Nano has most of the shortcuts listed at the bottom of the window, making it extremely simple to use. I won’t show or write the parameters of nano because you can find the instructions when you log in to the nano from the your terminal.

  • less

Less is a command line utility that displays the contents of a file or a command output, one page at a time. When starting less doesn’t read the entire file which results in much faster load times compared to text editors like vim or nano. I already show how to use less one of the videos that you can find the above.

e or j -> Move cursor down
y or k -> Move cursor up
g -> Go to the last line in the file
G -> Go to beginning of the file
q -> Exit
/<search> -> Search
?<search> -> Search
  • echo

The echo command is one of the most basic and frequently used commands in Linux. The arguments passed to echo are printed to the standard output. I already show how to use less one of the videos that you can find the above.

  • ls
ls -l -> list content permissions, number of links to the content, 
owner of the content,group owner of the content,
size of the content in bytes, last modified date / time of the content,
file or directory name

ls -lh -> list the files or directories in the same table format above,
but with another column representing the size of each file/directory

ls -a -> list files or directories including hidden files or directories

ls -la -> list files or directories in a table format with extra information
including hidden files or directories:

ls -lah -> to print disk usage

ls -d */ -> list only directories

ls * -> list the contents of the directory with its subdirectories

ls -R -> list all files and directories with their corresponding subdirectories
down to the last file

ls -s -> list files or directories with their sizes

The ls command is one of the many Linux commands that allow a user to list files or directories from the CLI. This command is very easy to use and have very useful parameters too so maybe for beginning you don’t need to use with complex parameters but keep in mind.

  • cat
cat <file name> -> Output the file content
cat -n -> To view contents of a file preceding with line numbers.
cat > newfile -> To create a new file.
cat file1 >> file2 -> To append the contents of one file to the end of another file.

Cat(concatenate) command is very frequently used in Linux. It reads data from the file and gives their content as output. It helps us to create, view, concatenate files.

  • tail

The tail command reads a file, and outputs the last part of it (the “tail”).

tail -f -> This option causes tail to loop forever, 
checking for new data at the end of the file(s).
When new data appears, it will be printed.

I prepared a video about tail command. In this video, there is a web server and we need to track web server access and error logs.

  • grep

Grep is used to search text and strings in a given file. In other words, grep command searches the given file for lines containing a match to the given strings or words.

grep <search> filename -> Search any line that contains the <search >in filename

grep -i '<search>' filename -> Perform a case-insensitive search for the word <search>

grep -R 'httpd' -> Look for all files in the current directory and in all of its subdirectories
  • du

The du command is a standard Linux/Unix command that allows a user to gain disk usage information quickly.

du -h -> prints size outputs, such as the ones above, in a human-readable format.

du -sh -> To get a summary of the directory’s usage in a human-readable format.

du -ah -> write count of all files, not just directories with human readable format.

Like I said before, this article just for beginning to somethings explore and I did not wrote many feature about that commands so don’t piss to me.

Thank you for reading, see you in other articles.

Kerem DEMIRTURK

--

--