Hey guys in this article, I will write the most important commands for a newbie Linux user. Read the article completely to learn about Linux basic commands.
Read More:
- Check the Complete JUnit 5 Tutorial
- Check the Complete JavaServer Faces (JSF) Tutorial
- Check the Spring Boot JdbcTemplate Tutorials
- Check the Complete Spring Boot and Data JPA Tutorials
- Check the Complete Spring MVC Tutorials
- Check the Complete JSP Tutorials
- Check the Complete Spring Boot Tutorials [100+ Examples]
- Check the Complete Spring Boot and Thymeleaf Tutorial
- Check the Complete AWS Tutorial
- Check the Complete JavaServer Faces (JSF) Tutorial
- Check the Complete Spring Data JPA Tutorial
- Check the Complete Spring Security Tutorial
- Check the Javascript Projects for Beginners
Table of Contents
Terminal Emulators
The above is the screenshot of the Gnome terminal application. As you can see the command prompt contains the following information:
[username@hostname directoryname]
In our case the username is bushansirgur, hostname is Bushans-MacBook-Air, and directory is mentioned as ~. This ~ is a special character in our case. It means the home directory of the user. In our case, the home directory path is /home/bushansirgur/
.
The Gnome terminal is one of many implementations of terminal emulators. Different Linux environments may come pre-installed with different terminals.
date command
date
command prints the current date time.
$ date
Sun Jan 01 09:00:10 IST 2023
In case you want to know the current date/time in UTC, use the following command.
$ date -u
Sun Jan 1 13:30:53 UTC 2023
cal command
cal
command is used to display a calendar in your shell, by default it will display the current month
$ cal
January 2023
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
$ cal 02 2023
February 2023
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28
whoami command
whoami
command will tell you which user account you are using in this system.
$ whoami
bushansirgur
id command
id
prints real user id, and various other details related to the account.
$ id
uid=501(bushansirgur) gid=20(staff) groups=20(staff),12(everyone),61(localaccounts),79(_appserverusr),80(admin),81(_appserveradm),98(_lpadmin),702(com.apple.sharepoint.group.2),33(_appstore),100(_lpoperator),204(_developer),250(_analyticsusers),395(com.apple.access_ftp),398(com.apple.access_screensharing),399(com.apple.access_ssh),400(com.apple.access_remote_ae),701(com.apple.sharepoint.group.1)
pwd command
pwd
command, short for the print working directory, will help you to find out the absolute path of the current directory.
$ pwd
/Users/bushansirgur
cd command
The next command we will learn is cd
, short for change directory. This command will help you to change your current directory. We will move to Documents/
directory in our example
bushansirgur@Bushans-MacBook-Air ~ % cd Documents
bushansirgur@Bushans-MacBook-Air Documents % pwd
/Users/bushansirgur/Documents
bushansirgur@Bushans-MacBook-Air Documents % cd ~
bushansirgur@Bushans-MacBook-Air ~ % pwd
/Users/bushansirgur
Here you can see that first, we moved to Documents/
directory, and then we moved back to the home directory by using ~
character
. directory and .. directory
.
and ..
has special meaning in Linux. . (single dot) means the current directory and .. (double dot) means the parent directory. We can use these in various situations for daily activities.
$ cd ..
The above command changes the current directory to the parent directory.
ls command
We use ls
command to list the files and directories inside any given directory. If you use ls
command without any argument, then it will work on the current directory. We will see a few examples of the command below
$ ls
Applications Library Pictures mongodb-data
Desktop Movies Postman
Documents Music Public
Downloads OneDrive mongodb
$ ls Documents/
Adobe
java-workspace
$ ls /
Applications Volumes etc sbin
Library bin home tmp
System cores opt usr
Users dev private var
In the last two commands, we provided a path as the argument to the ls command. / is a special directory, which represents the root directory in the Linux filesystem.
mkdir command
We can create new directories using mkdir
command. For our example, we will create a code directory inside our home directory.
$ mkdir code
$ ls
code Desktop Documents Downloads
We can also create nested directories in a single command using the -p
option.
$ mkdir -p dir1/dir2/dir3
$ ls dir1 dir1/dir2/
dir1/:
dir2
dir1/dir2/:
dir3
rm command
rm
command is used to remove a file or directory. The -r
option is being used to remove in a recursive way. With -f
you force the removal, ignoring errors and never prompt. You can chain the flags, so instead of rm -r -f
you can as well type rm -rf
. But, always double-check before you use the rm -rf
command, if you by mistake give this command in your home directory or any other important directory, it will not ask to confirm, but it will delete everything there. So, please be careful and read twice before pressing the enter key.
$ rm -rf dir1/dir2/dir3
$ls dir1/ dir1/dir2/
dir1/:
dir2
dir1/dir2/:
cp command
We use the cp
command to copy a file in the Linux shell. To copy a folder with its contents recursively use the cp
command with the -r
flag. We use the cp file_to_copy new_location format. In the example below, we are copying the file1.txt
to file2.txt
.
$ cp file1.txt file2.txt
$ ls -l
-rw-rw-r--. 1. fedora fedora 75 Jan 01 05:56 file2.txt
-rw-rw-r--. 1. fedora fedora 75 Jan 01 05:59 file1.txt
In another example, I will copy the file flower.png
from the Pictures directory in my home directory to the current directory.
$ cp ~/Documents/flower.png .
In the following example, I will be copying the images directory (and everything inside it) from the Downloads directory under the home to the /tmp/
directory.
$ cp -r ~/Downloads/images /tmp/
Renaming or moving a file
The mv
command is used to rename or move a file or directory. In the following example, the file hello.txt
is renamed to nothello.txt
$ mv file1.txt file2.txt
$ ls -l
-rw-rw-r--. 1. fedora fedora. 75 Jan 01 03:23 file2.txt
tree command
tree
command prints the directory structure in a nice visual tree design way.
$ tree
.
|- code
|- Desktop
|- Downloads
|- Music
|- Pictures
wc command
wc
, short for word count, is a useful command which can help us to count newlines, words, and bytes of a file.
$ cat file1.txt
some text
$ wc -l file1.txt
1 file1.txt
$ wc -w file1.txt
2 file1.txt
The -l
flag finds the number of lines in a file, -w
counts the number of words in the file.
echo command
echo
command echoes any given string to the display.
$ echo "Hello"
Hello
Redirecting the command output
In Linux shells, we can redirect the command output to a file, or as input to another command. The pipe operator |
is the most common way to do so. Using this we can now count the number of directories in the root (/ ) directory very easily.
$ ls /
bin boot dev etc lib
$ ls / | wc -w
5
The |
is known as a pipe. To know more about this, watch this video.
Using > to redirect output to a file
We can use >
to redirect the output of one command to a file, if the file exists this will remove the old content and only keep the input. We can use >>
to append to a file, which means it will keep all the old content, and it will add the new input to the end of the file.
$ ls / > file1.txt
$ cat file1.txt
bin
boot
dev
etc
lib
$ ls /usr/ > file1.txt
$ cat file1.txt
bin
games
include
lib
$ ls -l /tmp/ >> file1.txt
$ cat file1.txt
bin
games
include
lib
total 378
-rwxrwxr-x. 1 fedora fedora 45 Jan 01 05:34 test.java
-rw-------. 1 fedora fedora 45 Jan 01 06:55 somefile
That’s it for this article, I hope this article helps you to understand the most basic Linux commands as a newbie. If this article, helps you then please share this with your friends and colleagues.