Linux basic commands for beginners

Spread the love

Linux basic commands for beginners

Linux operation system is widely used to host web applications and web developers need to know how to use Linux  command line.  This article explores basic Linux commands for web developers.

Linux basic commands for beginners
Linux basic commands for beginners

Linux is a powerful and flexible open-source operating system that is used by many organizations and individuals worldwide. In order to effectively use and manage Linux, it is important to be familiar with some of its basic commands. In this tutorial, we will cover some of the most commonly used Linux commands and provide examples of how to use them.

ls: list files and directories in the current directory.

cd: change to a different directory.

pwd: print the current working directory.

mkdir: create a new directory. rm: remove a file or directory.

cp: copy a file or directory to another location.

mv: move or rename a file or directory.

sudo: run a command with administrative privileges.

cat: display the contents of a file.

grep: search for a specific string or pattern within a file.

These commands are essential for navigating the file system, creating and modifying files and directories, managing permissions, and searching for information. By mastering these commands, beginners can become more comfortable and proficient in using Linux.

ls Command

The ls command is used to list the files and directories in a directory. By default, ls will display a list of all the files and directories in the current directory. For example:

ls

You can use the -l option to display the files and directories in a long format, which provides more information such as the file permissions, owner, size, and date modified. For example:

ls -l

cd Command

The cd command is used to change the current directory. For example, if you want to change the current directory to your home directory, you would use the following command:

cd ~

You can also use the cd command to move to a different directory by specifying the path to the directory. For example, if you want to move to the Downloads directory, you would use the following command:

cd ~/Downloads

pwd Command

The pwd command is used to display the current working directory. For example:

pwd

mkdir Command

The mkdir command is used to create a new directory. For example, if you want to create a new directory called new_directory, you would use the following command:

mkdir new_directory

mkdir new_directory

rmdir Command

The rmdir command is used to remove an empty directory. For example, if you want to remove the new_directory directory, you would use the following command:

rmdir new_directory

cp Command

The cp command is used to copy files and directories. For example, if you want to copy the file file1.txt to a new file called file2.txt, you would use the following command:

cp file1.txt file2.txt

You can also use the cp command to copy a directory and all its contents to a new directory. For example, if you want to copy the directory dir1 to a new directory called dir2, you would use the following command:

cp -r dir1 dir2

mv Command

The mv command is used to move or rename files and directories. For example, if you want to rename the file file1.txt to file2.txt  you would use the following command:

mv file1.txt file2.txt

You can also use the mv command to move a file or directory to a different location. For example, if you want to move the file file.txt to the directory, you would use the following command:

mv /path/to/your/file.txt /path/to/your/destination/directory/

rm Command

The rm command is used to delete files and directories. For example, if you want to delete the file file2.txt, you would use the following command:

rm file2.txt

It’s important to be cautious when using the rm command as it permanently deletes files and there is no way to recover them. To avoid accidentally deleting important files, it’s a good practice to use the -i option, which prompts you to confirm each file before it is deleted. For example:

rm -i file2.txt

You can also use the -r option to recursively delete a directory and all its contents. For example, if you want to delete the directory new_directory and all its contents, you would use the following command:

rm -r new_directory

touch Command

The touch command is used to create a new empty file. For example, if you want to create a new empty file called new_file.txt, you would use the following command:

touch new_file.txt

cat Command

The cat command is used to display the contents of a file. For example, if you want to display the contents of the file new_file.txt, you would use the following command:

cat new_file.txt

You can also use the cat command to concatenate multiple files and display the output. For example, if you have two files called file1.txt and file2.txt, you can use the following command to display their contents together:

bash
cat file1.txt file2.txt

echo Command

The echo command is used to display a message or the contents of a variable. For example, if you want to display the message Hello, World!, you would use the following command:

echo "Hello, World!"

You can also use the echo command to write the output to a file. For example, if you want to write the message Hello, World! to a file called hello.txt, you would use the following command:

echo "Hello, World!" > hello.txt

head Command

The head command is used to display the first few lines of a file. By default, head displays the first 10 lines of a file. For example, if you want to display the first 10 lines of the file hello.txt, you would use the following command:

head hello.txt

You can also specify the number of lines to display using the -n option. For example, if you want to display the first 5 lines of the file hello.txt, you would use the following command:

head -n 5 hello.txt

tail Command

The tail command is used to display the last few lines of a file. By default, tail displays the last 10 lines of a file. For example, if you want to display the last 10 lines of the file hello.txt, you would use the following command:

tail hello.txt

grep Command

The grep command is used to search for a specific pattern in a file. For example, if you want to search for the word Hello in the file hello.txt, you would use the following command:

grep "Hello" hello.txt

By default, grep displays the lines that contain the search pattern.

sort numbers.txt | uniq

You can also use the -c option to display only the count of the number of lines that contain the pattern. For example

grep -c "Hello" hello.txt

sort Command

The sort command is used to sort the contents of a file. For example, if you have a file called names.txt that contains a list of names, you can use the following command to sort the names in alphabetical order:

sort names.txt

You can also sort the contents of the file in reverse order using the -r option. For example:

sort -r names.txt

uniq Command

The uniq command is used to remove duplicate lines from a file. For example, if you have a file called numbers.txt that contains a list of numbers, you can use the following command to remove any duplicate numbers:

sort numbers.txt | uniq

It’s important to note that the uniq command only works on sorted files. This is why we use the sort command before using uniq.

wc Command

The wc command is used to display the number of lines, words, and characters in a file. For example, if you want to display the number of lines, words, and characters in the file hello.txt, you would use the following command:

bash

wc hello.txt

You can also use the -l option to display only the number of lines, the -w option to display only the number of words, and the -c option to display only the number of characters.

cut Command

The cut command is used to extract specific columns from a file. For example, if you have a file called data.txt that contains data separated by a tab character, you can use the following command to extract the second column:

cut -f2 data.txt

You can also specify the delimiter character using the -d option. For example, if your data is separated by a comma instead of a tab, you would use the following command:

cut -d, -f2 data.txt

paste Command

The paste command is used to combine the contents of multiple files. For example, if you have two files called file1.txt and file2.txt, you can use the following command to combine their contents vertically:

paste file1.txt file2.txt

tr Command The tr command is used to translate or delete characters from a file. For example, if you have a file called file.txt that contains some uppercase letters, you can use the following command to convert all uppercase letters to lowercase:

tr [:upper:] [:lower:] < file.txt

find Command

The find command is used to search for files in a directory. For example, if you want to search for all .txt files in the /home directory, you would use the following command

find /home -name *.txt

You can also use the find command to perform actions on the files that it finds. For example, if you want to delete all .txt files in the /home directory, you would use the following command:

find /home -name *.txt -delete

chmod Command

The chmod command is used to change the permissions of a file or directory. For example, if you want to give read, write, and execute permissions to the owner of the file file.txt, you would use the following command:

chmod 744 file.txt

The numbers 744 represent the permissions. The first number represents the owner’s permissions, the second number represents the group’s permissions, and the third number represents the permissions for others.

chown Command

The chown command is used to change the owner of a file or directory. For example, if you want to change the owner of the file file.txt to the user john, you would use the following command:

chown john file.txt

rsync Command

The rsync command is used to synchronize files between two directories. For example, if you want to synchronize the contents of the /home/user1 directory with the /home/user2 directory, you would use the following command:

rsync -avz /home/user1 /home/user2

The -a option tells rsync to preserve the permissions, ownership, and timestamps of the files. The -v option tells rsync to display verbose output. The -z option tells rsync to compress the data before transferring it.

tar Command

The tar command is used to create and extract archive files. For example, if you want to create an archive of the directory /home/user1, you would use the following command:

tar -cvf archive.tar /home/user1

The -c option tells tar to create an archive. The -v option tells tar to display verbose output. The -f option tells tar the name of the archive file to create.

To extract an archive, you would use the following command:

tar -xvf archive.tar

The -x option tells tar to extract the contents of the archive.

Summary:

In conclusion, we learnt about Linux basic commands for beginners.  If you want to explore and learn about Linux, check the best seller course below.

Learning Linux

Related Tutorials:

Previous Article:

 

 

Check Also

Kubernetes for beginners

Kubernetes for Beginners tutorial

Spread the loveLast updated:17th February, 2023Kubernetes for Beginners Kubernetes has revolutionized the way containerized applications …