Linux : Basic Commands
Linux :
Linux, an open-source operating system, is a powerful and versatile platform that has become the backbone of modern computing. Its flexibility, security, and a wide array of commands make it an essential tool for developers, sysadmins, and enthusiasts alike. Linux provides a command-line interface that enables users to interact with the system efficiently and perform various tasks, from simple file management to complex network configurations.
Basic Commands:
- pwd: It gives the present working directory.
$ pwd
/home/user/projects/my_project
- echo:
echo
command is used to display text or messages in the terminal or command-line interface
$ echo "Hello, World!"
Hello, World!
//Using Escape Sequences
$ echo "First Line\nSecond Line\nThird Line"
First Line
Second Line
Third Line
- ls: command in Linux is used to list files and directories in the current working directory. It provides valuable information about the contents of a directory, such as file names, permissions, ownership, size, and modification timestamps.
// Listing files and directories
$ ls
file1.txt file2.txt directory1 directory2
//Displays information like file permissions,hard links,owner, group,
file size, timestamp, and the name of each file/directory.
$ ls -l
-rw-r--r-- 1 user user 1024 Aug 1 10:00 file1.txt
-rw-r--r-- 1 user user 2048 Aug 1 11:00 file2.txt
drwxr-xr-x 2 user user 4096 Aug 1 12:00 directory1
//Hidden Files
$ls -a
//Files size human readable like "K" for kilobytes
$ ls -lh
-rw-r--r-- 1 user user 1.0K Aug 1 10:00 file1.txt
-rw-r--r-- 1 user user 2.0K Aug 1 11:00 file2.txt
whoami: Command in Linux is used to display the current user's username.
cd: Change Directory, It allows you to navigate to different directories within the file system.
// To a particular directory
$ cd Documents
//Home Directory
$ cd ~
//Parent Directory (One Level Up)
$ cd ..
- mkdir: Command in Linux is used to create directories or folders. It allows you to create single directories, multiple directories, and nested directories.
//Single directory
$ mkdir new_folder
//Multiple directory
$ mkdir folder1 folder2 folder3
//Nested directory
$ mkdir -p A/B/C/D/E
- touch & cat: Commands in Linux are used for creating and viewing files, respectively. The
touch
command is used to create empty files or update the timestamp of existing files.
//Empty file
$ touch file.txt
//View Contents of a file
$cat file.txt
//Creating a new file with content
$cat > newfile.txt
This is my first file
(Press CTRL+D to save and exit)
//Combine multiple files into one
$ cat file1.txt file2.txt > combined_file.txt
//Appending content to existing file
$ cat new_content.txt >> existing_file.txt
//Display content with line numbers
$ cat -n file.txt
1 Line 1
2 Line 2
3 Line 3
Basic editors:
Vi/Vim: A classic and powerful modal text editor.
Nano: It is a user-friendly and straightforward editor for beginners.
Sublime Text: Fast, simple, and extensible editor with a free evaluation version.
Creating a fruits.txt and fruits1.txt with Vi editor:
$vi fruits.txt (Editor will open , Press 'i' for insert mode and enter the data. To save and exit press 'ESC+ :wq') $vi fruit1.txt (Repeat the same process as above)
head, tail, and diff commands: Commands in Linux are used to display the top and bottom lines of a file, respectively.
diff
command in Linux, the notation "1c1" represents a change that has occurred between two files being compared.General Format of the diff
line_number_1cchange_type line_number_2
line_number_1
The line number in the first file where the change occurredchange_type
The type of change that occurred. The most common change types are:c
indicates that a change (modification) has occurred in the line.a
indicates that a new line has been added in the second file.d
indicates that a line has been deleted from the first file.
line_number_2
The line number in the second file where the corresponding change occurred
In conclusion, Linux commands offer a powerful and efficient way to interact with the operating system through the command-line interface.
Thank you for taking out valuable time and effort to read till the very end; feedback is always welcomed and appreciated.