I learned Linux and its commands a few days back. So, I thought to pick only 5 commands that are very useful in my daily workflow and share that in this blog to you.
If you don't have a Linux machine, don't worry. You can try out Linux through Bash Shell in Windows. Otherwise, you can use WSL.
WSL stands for Windows Subsystem for Linux. WSL will give access you to use the Linux file system and Linux command-line tools on Windows OS.
Alright, let's get started with some useful Linux commands.
find command
As the name suggests, the find command is used to find files and directories. It follows a recursive algorithm.
Simply, if you use
find .
It will list all the files and folders in the current directory.
Some use cases with find
What if you need to find only folders?
find . -type d // Only find in the current directory with the type of directory
find . -type f // Only find in the current directory with the type of file
This command states that find in the current directory (.) with the type (-type) of directory or folder (d)
What if you need to find only files with the starting letter of "h"?
find . -type f -name "h*"
This command will find in the current directory with the type of file and the name of all files starting with "h"
"*" specifies that anything can come over there. In this context, anything after h will be valid
What if you need to find only files that have the extension "pdf"?
In order to find only pdf files use,
find . -type f -name "*.pdf"
What if you need to find and delete a file?
find hello.txt -delete
This command will find the hello.txt file and deletes it
cat command
cat stands for concatenate. This command is more powerful. Let's see its use cases.
Starting with,
cat hello.txt
This will print the file content in the hello.txt to the terminal.
What if you need to create and write in a file?
cat > about.txt
This greater than symbol ">" is known as the output redirection operator in Linux. In this context, it outputs whatever you have written in the terminal after using this command to the about.txt file.
After writing to exit out from the file use ctrl+c
What if you need to print the content of multiple files?
cat hello.txt about.txt // cat file1 file2
This will print the content in the hello.txt file as well as in the about.txt file
Also, you can merge the content of two files to a new file
cat hello.txt about.txt > new.txt
This will merge the content of hello.txt and about.txt to new.txt
echo command
echo is something the same as print function in python. It will print whatever you pass as arguments to it.
Also, you can write in a file using echo and output redirection operator ">"
echo "Hello Dev Community" > hello.txt
As you can see it overwritten the content in the hello.txt file with the arguments passed to it.
vim editor command
Vim is a file editor. It can be used to read and edit files In the terminal itself.
We already saw to read and write a file using the cat command. But why Vim?
Vim is a nice way to read and write in a file.
use vim or vi in terminal to start the vim editor
You can write in a file using this command,
vim hello.txt // vim filename
There are two modes in vim after you enter,
- Command mode
- Insert mode
By default, vim opens with command mode. You can't write in a file using command mode. For that, you need to enter insert mode.
This can be done by pressing the insert key on your keyboard.
Now you can edit the file as you want.
Alright, I'll add some text to it
Once, you are done with editing, you should enter into command mode to save the file and exit vim.
Enter into command mode by pressing the ESC key on your keyboard.
To save and exit vim you can use :wq command
The most underrated - man command
man command is used to understand all other commands.
If you don't know which command, what it does, you can use
man <command> // man cat
For eg. I can use "man cat" to get the details about the cat command.
As you can see, the cat command is used to concatenate files and print the output. Also, it will show the options to use with the command.
For eg. To print the output of a file with the line numbers, one can use
cat -n hello.txt
That's it for today. Thanks for reading! Have a great day a head :)