Linux commands : File system

Β·

4 min read

πŸ˜€ πŸŽ‰ πŸ‘ πŸ‘ πŸ™‚

For the last 5 years when I was in my second year of BTech I have been using Linux only(Single boot machine) . All thanks to my professor Abhijeet A.M who made us use linux. I practiced so many commands over the year and from many twitter threads as well . I will list down all of them ina series of blogs and will try to mention in what scenario we can use a particular command . This is 1st blog in series

Linux has many distributions: Debian , Red Hat , Ubuntu , Fedora but I use Ubuntu because it's easy to use. If you have windows OS you can install Ubuntu from the app store for CLI.

What is Operating System : OS is a code / script which is installed on a hardware to manage hardware resources and interfaces so that multiple applications can utilize them(resources) . Which resources : Storage , Processor , NIC (network interface card) , RAM (Memory) .

Linux has a hierarchical files and folder system. Folders can contain files and folders both. Folders are called as directory in Linux. We have a root directory at the top of the hierarchy. Let's start

How to manage files and folders in Linux ?

Create, list files and folders πŸ™‚

Go to the terminal and create a directory with command

mkdir documents ;

This will create a directory. Now go to that directory with command cd (change directory )

cd directory_name ; // replace "directory_name" with name that u want to give to directory
cd documents;

Now you are in the documents folder , list down the files and folders in a current directory with ls command .

ls ;   //this will give empty results as the directory is empty .

Now create different files in a folder with command

touch filename;
touch abc.txt ;

This will create a file with name abc and text format. Create directory β€œtest” in current directory With mkdir command

mkdir test;

Now run ls command to get a list of files and folders .

touch_ls (2).png

What is Absolute & Relative path ?

An absolute path refers to the complete details needed to locate a file or folder, starting from the root element and ending with the other subdirectories. Absolute paths are used in websites and operating systems for locating files and folders

A relative path refers to a location that is relative to a current directory.

Tip : To get absolute path of any directory run pwd command in that directory

pwd.png

Move / Copy / Delete / Rename Files πŸ‘

Task : Move a file abc.txt from documents folder to test folder & then copy it back from test folder to documents folder. mv command is used to move files.

mv source_path destination_path ;

mv  abc.txt /test ;

This command has moved abc.txt file to test folder so you will not get this file in current directory .

move_ls.png Now go into test directory and check

test_ls.png

Copy a file from test to documents directory . To go into previous directory we use cd .. Similarly '../' is one step back folder .

cp abc.txt ../

copy.png

Rename File πŸ˜€

mv  abc.txt info.txt ;// this will create info.txt file and move abc file into it

rename.png Remove File & delete directory πŸ˜€

rm filename ; 
rm abc.txt ; // to remove file 
rmdir test ;// to remove EMPTY directory  this will give error
rm -r test ; // to delete any non empty directory you need to del all files folders inside it recursively with command 

mkdir test;
rmdir test ;// this will work as test is empty directory

rmdir.png

Edit files and search patterns πŸ˜€

vim abc.txt ; // this will open a vim editor

press the 'i' button to go in insert mode, and update data in the file. To exit and save the changes press 'Esc' button and type ':wq' and hit enter to save the changes .

vim_save.png

Now if you want to search any string or pattern you can do it with the grep command . This will highlight pattern in red if it is present . grep pattern file_name ;

grep Helllo abc.txt;

grep_search.png

TIP πŸ˜€ If you run any command and now only partially remember it then along with history command grep can be used in pipeline to search that command.

PIPELINE ? πŸ˜€ You can take output of one command and provide it as input to another command

history | grep cp ;

grep_history.png

Β