1) Introduction to Linux Operating System
Imagine you have a computer, and you want to install a stable, secure, and flexible operating system. Linux is like a superhero that rescues you!
Linux is free and open-source, so you don't have to pay a penny. It's like getting a pizza for free, but instead of just one topping, you get to add all the toppings you want. This includes pepperoni to mushrooms to even pineapple (if you're into that sort of thing).
Plus, Linux is customizable. You can tweak and modify the system to your liking. So, if you want a reliable and flexible operating system, look no further than Linux.
2) Basic Linux Commands
Firstly, lets us learn how to make files by the following methods:
1. CAT command
Cat is short for concatenate. This command displays the contents of one or more files without having to open the file for editing.
Now, open the terminal and type the following command:
cat >examplefile1
Here, the (>) means redirecting to the file to create it if it has not existed earlier.
Now, the cursor moves to the next line enabling us to write whatever text we want like this:
This is my 1st file on Linux
Hello Everyone!
To add multiple lines of text just press Enter at the end of each line. Once you’re done, hit CTRL+D to exit the file.
Without any options, the command will read the contents of a file and display them in the console:
cat examplefile1
If you are bored with doing Ctrl+C and Ctrl+V for copying and pasting files from one place to another. Linux commands do it in a fun and easy way.
cat examplefile1 > destination
This command enables examplefile1 data to overwrite all the data of destination file. Here is an important thing for all of us to know cat is not an editor means it cannot able to edit or remove the existing text rather it appends the new text below the existing one. We can do this by the following command:
cat >>destination
Hello from the destination file.
Now, we will check the content inside the destination file. It would be something like this:
This is my 1st file on Linux
Hello Everyone!
Hello from the destination file.
Cat command helps in concatenating too. The command for concatenating any number of files in one file:
cat examplefile1 destination > samplefile1
After pressing enter, all the content has been saved in the samplefile1 file. Let us check by putting the command cat samplefile1
.
This is my 1st file on Linux
Hello Everyone!
Hello from the destination file.
Finally a worth-knowing command for coders:
tac samplefile1
It will simply reverse the content written earlier. The output should be something like this:
Hello from the destination file.
Hello Everyone!
This is my 1st file on Linux
2. Touch command
The touch command's primary function is to modify a timestamp. Commonly, the utility is used for multiple or single file creation, although this is not its primary function.
- For creating files using touch command:
touch file1 file2 file3
When we will type the command ls -lu
, it will show us the access time of a certain file. For ex:
Here is the access time: Apr 17 09:03.
When we will type the command ls -l
, it will show us the modification time of the particular file.
Here is the modification time: Apr 17 9:03.
- For changing Access Time to the Current
touch -a file1
Again, type the command ls -lu
to check access time
- For changing Modification Time to Current
touch -m file1
Type the command ls -l
to check the modification time.
Note: There is a change time also which means when is the last time the file metadata is changed. We can check access time, modification time and change time by the command stat file1
.
3. Vi command
It is a programming editor, that can be used to edit all kinds of plain text especially useful for editing programs.
Important shortcuts for this editor:
:w - for saving the data
:wq or :x - to save and quit from the editor
:q - for quitting only
:q! - for quitting forcefully without saving the data
A) Creating the file using vi command
Type vi filea
on terminal, a blank screen will open like this:
Do not get scared, just simply type i
for inserting the text whatever we want to type like this:
Now, to save this text and exit this editor. Firstly, press the Esc button on your keyboard then :wq
and finally press enter button.
Congratulations! You have created a new file using vi editor.
4. Nano command
Firstly you need to install the nano editor if not installed already using the below command:
sudo apt install nano
Once it is installed, you can proceed further and learn how to use it. Let's start your learning:
- How to Create a File
nano file-name
This opens up a blank nano editor with the file name at the top and a set of keyboard shortcuts for use with the command-line editor at the bottom of the console.
- Searching and Replacing Text
To search for a string in a text file, just hit Ctrl + w and thereafter, type the search keyword and press ENTER. In the example below, I’m searching for the string Allow in the /etc/ssh/sshd_config configuration file.
To proceed to the next matching string, just press Alt + w
. To search and replace a string in a text file, hit Ctrl + \
.
5. How to create a directory
To create a directory in Linux, pass the directory’s name as the argument to the mkdir
command. For example, to create a new directory newdir
, you would run the following command:
mkdir newdir
You can verify that the directory was created by listing the contents using the ls
command:
ls -l
Output
drwxrwxr-x 2 username userr 4090 Jan 23 02:09 newdir
- How to Create Parent Directories
A parent directory is a directory that is above another directory in the directory tree. To create parent directories, use the -p
option.
Let’s say you want to create a directory/home/linux/Game/GTA/exp
:
mkdir /home/linux/Game/GTA/exp
Instead of creating the missing parent directories one by one, invoke the mkdir
command with the -p
option:
mkdir -p /home/linux/Game/GTA/exp
When the -p
option is used, the command creates the directory only if it doesn’t exist.
2)How to Create Multiple Directories
To create multiple directories, specify the directories’ names as the command arguments, separated by space:
mkdir dir1 dir2 dir3
6. How to copy a file
Linux offers the cp command that you can use to copy the files from one directory to another by specifying its path.
There are various options that you can use with the cp command. The following is a quick cheat sheet:
-v: Added to make the cp command verbose.
-i: Added to make the cp command interactive, especially when copying the files into a directory that contains files with the same filename.
-p: Added to copy a file and its attributes, such as access permissions and modification dates.
-b: Added to create a backup of the file being copied to the destination folder, but with a different extension.
-r: It specifies recursiveness and is used when you want to copy all files in a directory.
The following example demonstrates how to make a backup copy of a.txt
named a.txt.bak
.
cp a.txt a.txt.bak
To confirm the copy operation, use ls
to list the files in the directory. Both the source file and the copy are listed.
ls -l
Output
-rw-rw-r-- 1 userr userr 4905 Jul 23 06:00 a.txt
-rw-rw-r-- 1 userr userr 4905 Jul 22 08:11 a.txt.bak
Use the -p
option to retain the file attributes of the original file in the copy. For example, Ubuntu assigns the duplicate the same date stamp as the original.
cp a.txt a.txt.bak -p
-rw-rw-r-- 1 userr userr 2208 Jul 23 06:00 a.txt
-rw-rw-r-- 1 userr userr 2208 Jul 22 06:00 a.txt.bak
- How to Copy a File to Another Directory in Linux
Here is the pattern for copying a file to a directory on Linux.
cp sourcefile target_dir
To give the copy a new name, append the name to the path of the target directory.
cp sourcefile target_dir/targetfile
7. How to rename the file or directory
If you want to rename a file, you can do it like this:
mv file1.txt file2.txt
As simple as that. However, if you are not in the directory, you will need to type a bit more. For example:
cd /home/user/imp/filess
mv file1.txt file2.txt
- Rename Multiple Files With the mv Command
for f in *txt; do
mv -- "$f" "${f%.txt}.pdf"
done
This will create a loop (for) looking through the list of files with the extension .txt. It will then replace each .txt extension with .pdf. Finally, it will end the loop (done).
8. How to remove a file or directory
Here are some commands that will help you successfully remove file or a directory
rm -rf
: removes a non-empty file or directory forcefullyrm -rp
: removes non-empty directories including parent and subdirectory.rm -r
: removes empty directoriesrmdir
: It is used to remove the specified directoryrmdir -p
: remove both parent and child directory
3) Intermediate Linux Commands
hostname
: shows what version of linux you are using
ifconfig
: shows the ip address
If you want to see the version of your OS you are using. Then type the command given below:
cat /etc/os-release
1) How to install Apache Server
yum install httpd
Here, yum(yellow dog update modified) is a package manager and 'd' in httpd signifies daemon.
2) How to start a service or software
service httpd start
If you want to see the status of whether the Apache service has been started or not. You can type the command given below:
service httpd status
To make the httpd service automatically on when the linux machine restarts:
chkconfig httpd on
3) How to create a user in Linux via the command
useradd emp1
The following command will show the created user:
cat /etc/passwd
(i) To create a group:
groupadd techSaumil
The following command will show the created groups:
cat /etc/group
It will also display the emp1 because when we make a new user, it will automatically create a new group.
For adding the existing user in an existing group, use the command given below:
gpasswd -a temp techSaumil
For adding the existing users in an existing group, use the command given below:
gpasswd -M a,b,c techSaumil
4) How to create a soft link to a file
ln -s file1 softfile1
5) How to create a hard link to a file
ln file2 backupfile2
There is an advantage in creating a hard link to a file because in the above case, if file2 got mistakenly deleted then you can use backupfile2.
And also, if we update any data in file2, then it will automatically update the data in backupfile2.
6) How to create an archive file
To create an archive called ‘project.tar’ from the contents of the ‘project’ directory, type:
tar -cvf project.tar project
Here, cvf
represents create, verbose and forcefully respectively.
If you want to reduce the size of file, you can use the command given below:
gzip project.tar
Output after ls
command
project.tar.gz
For unzipping the file, use the following command:
gunzip project.tar.gz
It will give the output -> project.tar
Now, for extracting the file:
tar -xvf project.tar
Output after ls
command
project
Conclusion
In conclusion, learning Linux commands can open doors to endless opportunities. Whether you want to manage files, control your system, or automate tasks, Linux gives you the freedom to do it all. By exploring different commands, you can easily move around folders, change permissions, and make your computer work for you. Embrace the power of Linux and embark on an exciting journey of discovery. Let the command line be your trusted companion in the world of technology. Get ready to unleash the true potential of Linux, step by step!