Linux Commands Every User Should Know

Before we go on to the list of commands, you need to open the command line first, so in this post, I will use "Git Bash" which is an application for Microsoft Windows OS environments that provides Unix-based shell utilities & experience for Git command-line commands.


Here is a list of basic Linux commands:


cd command Use the cd command to navigate through Linux files and directories. Depending on the current working directory, it requires either the full path or the name of the directory.

  • cd .. (with two dots) to move one directory up

  • cd to go straight to the home folder

  • cd- (with a hyphen) to move to your previous directory

  • cd / - Return to Root Directory

whoami command

The whoami command displays the effective user ID's user name. In other words, it shows the name of the user who is currently logged in.

[vagrant@localhost ~]$ whoami
vagrant


pwd command The location of your current working directory (folder). The command returns an absolute (full) path, which is a list of all directories that begin with a forward slash (/).

[vagrant@localhost ~]$ pwd
/home/vagrant


ls command List all directories and files using, You can use the ls command in a variety of ways:

  • ls -R will list all the files in the sub-directories as well

  • ls -a will show the hidden files

  • ls -al will list the files and directories with detailed information like the permissions, size, owner, etc.

David@DESKTOP-VIK2BBH MINGW64 ~
$ ls
'3D Objects'/
 AppData/
'Application Data'@
 Contacts/
 Cookies@
David@DESKTOP-VIK2BBH MINGW64 ~
$ ls -al
total 7489
drwxr-xr-x 1 David 197121       0 May 25 19:54  ./
drwxr-xr-x 1 David 197121       0 Mar 14  2021  ../
drwxr-xr-x 1 David 197121       0 May 26 12:19  .VirtualBox/
-rw-r--r-- 1 David 197121    2431 May 26 14:24  .bash_history
drwxr-xr-x 1 David 197121       0 Nov 11  2021  .docker/

mkdir command

Use mkdir command to make a new directory use mkdir command to make a new directory

  • To generate a new directory inside another directory, use this Linux basic command mkdir FolderA/FolderC

  • Use the p (parents) option to create a directory in between two existing directories. For example, mkdir -p FolderA / FolderB / FolderC.


David@DESKTOP-VIK2BBH MINGW64 /c/folderA/folderB/folderC
$ pwd
/c/folderA/folderB/folderC

cat command

It is used to list the contents of a file on the standard output. To run this command, type cat followed by the file’s name and its extension.


[root@localhost ~]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"

touch command

You can use the touch command to create a new file from the Linux command line.


We can also use this command to create multiple files using {}, for instance:

[root@localhost /]# mkdir devops
[root@localhost /]# cd devops
[root@localhost devops]# touch testfile{1..15}.txt
[root@localhost devops]# ls
testfile10.txt  testfile12.txt  testfile14.txt  testfile1.txt  testfile3.txt  testfile5.txt  testfile7.txt  testfile9.txt
testfile11.txt  testfile13.txt  testfile15.txt  testfile2.txt  testfile4.txt  testfile6.txt  testfile8.txt
[root@localhost devops]#


mv command

The mv command is primarily used to move files, but it can also be used to rename them.

  • You need to type mv, the file’s name, and the destination’s directory. For example: mv file.txt /root/FolderA

  • To rename files, the command is mv oldfilename.ext newdilename.ext

Example 1: Rename FIle

David@DESKTOP-VIK2BBH MINGW64 /c/filesexample
$ ls
creatingsinglefile.txt  file2.txt  file3.txt  file4.txt

David@DESKTOP-VIK2BBH MINGW64 /c/filesexample
$ mv file2.txt newfilename.txt

David@DESKTOP-VIK2BBH MINGW64 /c/filesexample
$ ls
creatingsinglefile.txt  file3.txt  file4.txt  newfilename.txt

David@DESKTOP-VIK2BBH MINGW64 /c/filesexample
$

Example 2: Move file

root@localhost ~]# cd /
[root@localhost /]# ls
bin   dev  home  lib64  mnt  proc  run   srv  tmp  vagrant
boot  etc  lib   media  opt  root  sbin  sys  usr  var
[root@localhost /]# pwd
/
[root@localhost /]# mkdir folderA folderB
[root@localhost /]# ls
bin   dev  folderA  home  lib64  mnt  proc  run   srv  tmp  vagrant
boot  etc  folderB  lib   media  opt  root  sbin  sys  usr  var
[root@localhost /]# touch folderA/file.txt
[root@localhost /]# cd folderA
[root@localhost folderA]# ls
file.txt
[root@localhost folderA]# mv file.txt /folderB
[root@localhost folderA]# ls
[root@localhost folderA]# cd ..
[root@localhost /]# cd folderB
[root@localhost folderB]# ls
file.txt

cp command

Use the cp command to copy files from the current directory to a different directory.

[root@localhost folderB]# ls
file.txt  newfile.txt
[root@localhost folderB]# cp file.txt /folderA
[root@localhost folderB]# ls
file.txt  newfile.txt
[root@localhost folderB]# cd ..
[root@localhost /]# ls
bin   dev  folderA  home  lib64  mnt  proc  run   srv  tmp  vagrant
boot  etc  folderB  lib   media  opt  root  sbin  sys  usr  var
[root@localhost /]# cd folderA
[root@localhost folderA]# ls
file.txt  newfile.txt
[root@localhost folderA]#

rmdir command

The rmdir command can be used to delete a directory. rmdir, on the other hand, can only be used to delete empty directories.


Locate command

This command, like the search command in Windows, can be used to locate a file. Furthermore, if you use the -I argument with this command, it will search for files regardless of the case, allowing you to find a file even if you don't remember its exact name.


To search for a file that contains two or more words, use an asterisk (*)

For example, locate -i AAA*BBB command will search for any file that contains the word “AAA” and “BBB”, whether it is uppercase or lowercase.


if `you are using Centos, you will first need to install "locate":

Step 1: run: sudo yum install mlocate

Steo 2: run sudo updated


Examples:

[root@localhost folderA]# locate -i file.txt
/folderA/file.txt
/folderA/newfile.txt
/folderB/file.txt
/folderB/newfile.txt
[root@localhost folderA]#

we can use '.' prior to our string, it will return all files with the specified file names

[root@localhost folderA]# locate .sys
/etc/dbus-1/system.d/org.freedesktop.systemd1.conf
/usr/lib/firmware/slicoss/gbdownload.sys
/usr/lib/firmware/slicoss/gbrcvucode.sys
/usr/lib/firmware/slicoss/oasisdbgdownload.sys
/usr/lib/firmware/slicoss/oasisdownload.sys

find command

Find, like the locate command, searches for files and directories. The find command, on the other hand, is used to locate files within a given directory.


Example 1: Find all files in a folder

[root@localhost folderA]# find
.
./newfile.txt
./file.txt
[root@localhost folderA]#

Example 2: Find a specific file

[root@localhost folderA]# find -name newfile.txt
./newfile.txt


grep command

Grep is another basic Linux command that is unquestionably useful for everyday use. It allows you to search all of the text in a given file. For example, grep aa notepad.txt will look for the word aa in the notepad file. Lines containing the searched word will be displayed in their entirety.


Options:

Options Description-c : This prints only a count of the lines that match a pattern
-h : Display the matched lines, but do not display the filenames.
-i : Ignores, case for matching
-l : Displays list of a filenames only.
-n : Display the matched lines and their line numbers.
-v : This prints out all the lines that do not matches the pattern
-e exp : Specifies expression with this option. Can use multiple times.
-f file : Takes patterns from file, one per line.
-E : Treats pattern as an extended regular expression (ERE)
-w : Match whole word
-o : Print only the matched parts of a matching line,

Credit: https://www.geeksforgeeks.org/grep-command-in-unixlinux/

Example :

David@DESKTOP-VIK2BBH MINGW64 /c/filesearch

$ ls

Grep.txt

David@DESKTOP-VIK2BBH MINGW64 /c/filesearch
$ cat grep.txt
A “Timebox” is an agreed and limited period used by a
 person/team to perform a dedicated activity.
Once the Timebox is approved and set, the team
/person works to complete a goal within this t
ime. If the time ended without completing all
the activities required, then how to continue
 will depend on the time-box approach (Soft Vs. Hard).

grep is a command-line uti
lity for searching plain-text data sets
 for lines that match a regular expression.
 Its name comes from the ed command g/re/p, whic
h has the same effect. grep was originally developed for the Unix o
perating system, but later available fo
r all Unix-like systems and some others such as OS-9.

David@DESKTOP-VIK2BBH MINGW64 /c/filesearch
$ grep effect grep.txt
h has the same effect. grep was originally developed for the Unix o

David@DESKTOP-VIK2BBH MINGW64 /c/filesearch

head command

The head command displays the first lines of any text file. It will display the first ten lines by default, but you can change this number to your personal preferences.

tail command

The tail command performs a similar function to the head command, but instead of displaying the first ten lines of a text file, it displays the last ten lines.

diff command

The diff command, which stands for difference, compares the contents of two files line by line. It will output the lines that do not match after analyzing the files. (The most basic version of this command is diff file1.ext file2.ext).


sudo command

This command allows you to carry out tasks that require administrative or root privileges. However, it is not recommended to use this command on a regular basis because it is easy for an error to occur if something goes wrong.


➔ sudo -i changes from normal user to root user

David@DESKTOP-VIK2BBH MINGW64 /c/vagrant-training/centosexample
$ vagrant ssh
Last login: Thu May 26 15:22:52 2022 from 10.0.2.2
[vagrant@localhost ~]$ whoami
vagrant
[vagrant@localhost ~]$ sudo -i
[root@localhost ~]# whoami
root
[root@localhost ~]#

df command

To get a report on the system's disk space usage in percentages and KBs, use the df command. Type df -m to display the report in megabytes.

jobs command

The jobs command displays all current jobs and their statuses. A job is essentially a process that the shell initiates.


ping command

To check your server connectivity, use the ping command. For example, simply typing ping google.com will check whether you can connect to Google and also measure the response time.

[root@localhost /]# ping www.google.com
PING www.google.com (142.250.185.68) 56(84) bytes of data.
64 bytes from fra16s48-in-f4.1e100.net (142.250.185.68): icmp_seq=1 ttl=112 time=79.1 ms


473 views0 comments