It was around 2017 when I bought a new laptop and switched to Linux. I started off with Ubuntu (but jumped to Elementary later), as most people suggested it was beginner friendly. People suggested getting used to the keyboard and terminal over the mouse to enjoy the ultimate power of Linux. Since then I started learning terminal commands and keyboard shortcuts. The fact that I know fast typing (learned from Typewriting institute) was a cherry on the cake.
2 of the most predominantly used commands in Linux are
The former command (cd) is used to navigate to a path or a different folder in the terminal. The latter command (ls) is used to list all the files and folders in a directory.
I’ll walk you through a few of my experiences handling file and folder operations in Linux.
I love downloading and readings files and books from the internet. But, earlier I had a bad habit of downloading all the files into the
One day I was in a hurry to search for a document. But opening and searching through the long list of files in
Downloads the folder was much frustrating for me as it has 1000+ files.
I wished to find a shortcut way to identify the file. The only clue I had was it was a PDF document. So, I planned to copy all the PDF files to another temporary folder.
#1 How to copy only the PDF files from one folder to another?
cp is the command to copy the files. Thankfully Linux terminal supports
regex almost everywhere. So, my work becomes so simple. Here’s the code to copy the pdf files into another folder.
mkdircommand is used to create a new directory. Regex stands for Regular Expression, which is used to filter items by matching patterns. In our case, it’s the files ending with
There were less than 50 PDF files. I was able to find the file in a couple of minutes.
P. S. It’s always advised to categorize the downloaded files.
#2 How to rename a file?
This is the continuation of the above step. Once I found the file I wanted it to be renamed so that I can find the file quickly in the future.
I searched for the same on the internet but then I found that the file has to be renamed with the move (
mv ) command.
#3 How to delete a folder completely? (including its files and sub-folders)
I copied the renamed file to a safe remember able to place. I don’t want the temporarily created folder and all the files inside it.
This can be achieved with
rm -rf command.
But, it’s a dangerous command. It won’t ask for any confirmation before deleting the folder. Recovering a folder deleted with this command is almost impossible.
It was a bright morning day, and my online class was already started and I was delayed by 2 minutes. I want to start taking notes, but I found that I kept my notebook and pen somewhere last night and couldn’t remember exactly. The very next thing that strikes on my mind was to open Google Docs. But, that site will take some time to load.
#1 How to create a file?
Why don’t we create a text file and start taking notes?
was the question that hit my mind next. I remember one of my friends told me once that the
touch command can be used to create a file instantly. But, I hadn’t given that a try yet.
But… But… But… ,
This command creates a file and does not open this file in edit mode to start taking notes. This is where you can use your favorite text editor to open your file.
nano are the 2 well-known text editors available in Linux.
vim is a extensive text editor used to perform complex file operations and it’s used by most Linux System Administrators
nano is a simple text editor which can be used to perform simple file operations
nano notes.txt which opens up the text file in edit mode. I took all the notes.
Finally, I reached the end of the session, but I don’t know to save the file. I don’t dare to try any commands before taking a backup of the notes. So, I picked up my mobile and took a snap of my notes. On a quick Google search, I found that it was “CTRL + X” to save the file (which will prompt Yes / No to save the file, hitting “Y” and pressing “Enter” will save the file).
After saving the file, I was curious to check if I have saved the file correctly.
#2 How can I read the contents of a file?
On a Google search, I found that there are multiple ways to read a file.
tail are few commands used to read a file.
catcommand displays the entire content of the file
headcommand displays the few lines at the top of the file
tailcommand displays the bottom few lines of the file
Have a look at the output of
#3 How can I read the contents of a file with line numbers?
One day, one of my colleagues asked me,
Hey! Do you know how to display the contents of a file with their line numbers in the terminal?
“I haven’t come across any such commands explicitly available for this. We may write a script that reads the file line by line, and prints them on the console by pre-pending line numbers for each line”, I replied.
“Well. Let’s explore that”, he said.
We both started to solve that. Interestingly in the middle, we found a straightforward command for the same.
Yes. The command is
That’s Amazing!!!! Isn’t it?
There are a lot of file utility commands available in Linux which most people are unaware of. During our development time, we come across multiple scenarios about handling files. Quickly summarizing a few of my discussions with my colleagues here.
Interesting question from Naras,
#1 Do you know a command to find the properties of a file?
stat command displays the properties of the file such as name, size, permissions, created by the user, created and updated date and time, etc.
#2 Is there a command to find the number of words in a file?
This was the question from Udhaya.
The answer is Yes. Not just words, we can also count lines and bytes too using the
This file has 23 lines and 121 words. Its size is 809 bytes.
Few weeks back, I encountered an extraordinary question from Kumar,
Most documents are created with their extension. But recently I found that few PDF documents are not created with
#3 Can we find the type of the document with a command?
file command displays the type of document.
To answer his question, here’s the proof showing the type as PDF document before and after removing the extension from the file name.
Divad raised a wonderful question,
#4 How can I find the occurrences of a word in a file?
It’s possible with
grepstands for Global search for Regular Expression and Print out
grep -i "Linux" notes.txt
-i flag indicates to perform case insensitive search. To perform case sensitive search, remove
-i flag from the command.
Dangerous Raman, who used to always raise Captious argument, raised a question,
#5 What if I want to find all the lines that does not contain a particular word?
It’s possible using the same
grep command but by applying
grep -v "Linux" notes.txt
Hope you enjoyed reading this article. Give a 👍 if you like this article. Comment if you have any questions or suggestions.