GNU/Linux Bash Commands Tutorial for Beginners -1 Working with Files and Directories

Hi,

I will explain Linux Commands for Beginners in this tutorial series.

 

In a basic sense, Linux commands are classified into 6 subjects.

  1. File and Directory Command
  2. System commands
  3. System statistic commands
  4. Process commands
  5. Search commands
  6. Network commands
  7. Package Installation commands
  8. Hardware commands

 

 

We will run all of these commands on the Terminal in Linux.

 

1.File and Directory Command

Basic File & Directory commands used in Linux / Unix are generally commands that allow access, deletion, modification, creation, compression, and existing files to various files and directories.

 

These commands are as follows.

  • ls
  • pwd
  • cd
  • mkdir
  • rm
  • cp
  • mv
  • wc
  • ln
  • touch
  • cat
  • more
  • head
  • tail

 

ls  ( List )

This command list the existing folders or files in the current directory. Equivalent to the Dir command in Unix and Windows. The ls command returns different values according to the parameter it gets.

I will not explain all the parameters here. Just type “man ls” to see all parameters and usage.

execute ls in the current directory, output will be as follows.

 

 

If you use the ls command as ls -a, it will list all open hidden files in the directory. Or if you use ls command as ls -l, it gives extensive information about the files in the directory. It contains information about the modification date and priviliges for the file, etc.

The use and output of the command are as follows.

 

 

 

pwd   (  Print Working Directory )

This command displays current working directory as follows.

 

 

[[email protected] scripts]# pwd
/home/oracle/scripts
[[email protected] scripts]# 
[[email protected] scripts]# cd ..
[[email protected] oracle]# 
[[email protected] oracle]# pwd
/home/oracle
[[email protected] oracle]#

 

 

cd    ( Change Directory )

This command is used to change the specified directory.

Usage of command is as follows.

 

cd “Directory_Name”

 

cd .. will take you one level up the directory.

cd ~ will take you to the top directory.

 

[[email protected] scripts]# pwd
/home/oracle/scripts
[[email protected] scripts]# 
[[email protected] scripts]# cd ..
[[email protected] oracle]# 
[[email protected] oracle]# pwd
/home/oracle
[[email protected] oracle]# 
[[email protected] oracle]# cd ~
[[email protected] ~]# 
[[email protected] ~]# 
[[email protected] ~]# pwd
/root
[[email protected] ~]#

 

 

 

mkdir  ( Make Directory )

The task of this command is to create a new directory with the name specified in the directory where it is located.

[[email protected] oracle]#
[[email protected] oracle]# mkdir mehmet
[[email protected] oracle]#
[[email protected] oracle]# cd mehmet/
[[email protected] mehmet]#
[[email protected] mehmet]# pwd
/home/oracle/mehmet

 

 

rm  ( Remove )

The file specified by this command is deleted.

You can use as rm -r “Directory Name”. The directory specified by this usage is deleted.

If you don’t want to reply rm question for remove, you can use -rf option to delete without question.

rm -rf command removes a directory along with its content forcefully and recursively.

 

[[email protected] oracle]# rm nohup.out 
rm: remove regular file `nohup.out'? y
[[email protected] oracle]# 
[[email protected] oracle]#

[[email protected] oracle]# rm -r mehmet/
rm: remove directory `mehmet'? y
[[email protected] oracle]# 
[[email protected] oracle]# 

[[email protected] oracle]# rm -rf mehmet/
[[email protected]h oracle]#

 

 

cp   ( Copy )

With this command, the contents of a specified file or directory are copied to another specified file or directory. Its use is as follows.

You can copy a file with cp command and copy a directory and its content with cp -r command.

[[email protected] oracle]# cp controlfile.bkp Control.bck
[[email protected] oracle]# 
[[email protected] oracle]# 
[[email protected] oracle]# ls -l Control.bck 
-rw-r----- 1 root root 127270912 Sep 9 13:55 Control.bck
[[email protected] oracle]# 
[[email protected] oracle]# cd mehmet/
[[email protected] mehmet]# 
[[email protected] mehmet]# mkdir salih
[[email protected] mehmet]# 
[[email protected] mehmet]# ls -ltr
total 4
drwxr-xr-x 2 root root 4096 Sep 9 13:53 salih
[[email protected] mehmet]# 
[[email protected] mehmet]# cd ..
[[email protected] oracle]# 


If you try to copy directory with cp command, you will get following error.

[[email protected] oracle]# cp mehmet/ MehmetSalih
cp: omitting directory `mehmet/'
[[email protected] oracle]# 
[[email protected] oracle]# 

You should use cp -r command for directory.

[[email protected] oracle]# cp -r mehmet/ MehmetSalih
[[email protected] oracle]# 
[[email protected] oracle]# cd MehmetSalih/
[[email protected] MehmetSalih]# 
[[email protected] MehmetSalih]# ls -l
total 4
drwxr-xr-x 2 root root 4096 Sep 9 13:54 salih
[[email protected] MehmetSalih]# 
[[email protected] MehmetSalih]# pwd
/home/oracle/MehmetSalih
[[email protected] MehmetSalih]# cd ..
[[email protected] oracle]# 
[[email protected] oracle]#

 

 

mv   ( Move )

With this command, a file can be moved to another directory or the name can be changed with this command.

Its use is as follows.

 

Any directory can be moved as follows.

[[email protected] mehmet]# mv salih/ ../
[[email protected] mehmet]# 
[[email protected] mehmet]# cd ..
[[email protected] oracle]# 
[[email protected] oracle]# cd salih/
[[email protected] salih]# 
[[email protected] salih]# pwd
/home/oracle/salih
[[email protected] salih]#

 

Any directory can be renamed as follows.

[[email protected] oracle]# mv salih/ Deveci
[[email protected] oracle]# 
[[email protected] oracle]# cd Deveci/
[[email protected] Deveci]# 
[[email protected] Deveci]# pwd
/home/oracle/Deveci
[[email protected] Deveci]# 
[[email protected] Deveci]#

 

 

wc   ( Word Count )

This command displays the number of lines, words and characters of the specified file.

Its use is as follows.

[[email protected] oracle]# wc error.log 
108840 417220 3056666 error.log      --  error.log contains 108840 lines, 417220 words and 3056666 characters
[[email protected] oracle]# 

 

 

 

ln    ( Link )

With this command you can call a file with a symbolic link. In other words, this link will keep the address of our file.

Its use is as follows.

 

You can link error.log with error like following, then you can use error.log as error.

[[email protected] oracle]# 
[[email protected] oracle]# ln error.log error
[[email protected] oracle]# 
[[email protected] oracle]# 
[[email protected] oracle]# wc error
108840 417220 3056666 error
[[email protected] oracle]# 
[[email protected] oracle]#

 

 

You can link any directory with ln -s command, then you can use that link instead of original directory.

[[email protected] oracle]# 
[[email protected] oracle]# ln -s MehmetSalih/ MSD
[[email protected] oracle]# 
[[email protected]ih oracle]# 
[[email protected] oracle]# cd MSD/
[[email protected] MSD]# 
[[email protected] MSD]# 
[[email protected] MSD]# pwd
/home/oracle/MSD
[[email protected] MSD]# 
[[email protected] MSD]# 
[[email protected] MSD]# mkdir deveci
[[email protected] MSD]# 
[[email protected] MSD]# 
[[email protected] MSD]# ls -ltr
total 8
drwxr-xr-x 2 root root 4096 Sep 9 13:54 salih
drwxr-xr-x 2 root root 4096 Sep 9 14:18 deveci
[[email protected] MSD]# 


Content of MSD is actually content of MehmetSalih, MSD is just link of MehmetSalih directory.

[[email protected] MSD]# cd ../MehmetSalih/
[[email protected] MehmetSalih]# 
[[email protected] MehmetSalih]# 
[[email protected] MehmetSalih]# ls -ltr
total 8
drwxr-xr-x 2 root root 4096 Sep 9 13:54 salih
drwxr-xr-x 2 root root 4096 Sep 9 14:18 deveci
[[email protected] MehmetSalih]#

 

 

touch 

This command creates a file with 0 bytes size with the current date if it does not exist. Use of the command is as follows.

[[email protected] oracle]# 
[[email protected] oracle]# touch MehmetDeveci
[[email protected] oracle]# 
[[email protected] oracle]# 
[[email protected] oracle]# ls -l MehmetDeveci 
-rw-r--r-- 1 root root 0 Sep 9 14:23 MehmetDeveci
[[email protected] oracle]#

 

cat  ( Concatenate )

The contents of the file specified by this command are displayed. Usage of command is as follows.

[[email protected] oracle]# cat MehmetDeveci 
State : Optimal
Strip Size : 1.0 MB
Number Of Drives : 4
Span Depth : 1


Is VD Cached: No

Exit Code: 0x00
[[email protected] oracle]#

 

 

 

You can use cat> File1 usage, thus you can Insert data from standard input to file1. When you execute this command, the standard input waits for data, and after you complete the data you typed, you can finish the standard input with ctrl-c. The data you entered is saved with File1. With this command, the contents of File1 are reset first and then the data you enter is added.

If you want to add data from the standard input to the existing data, use the following usage.

cat >> File1 adds and saves the data from the standard input from the EndofFile of File1.

 

[[email protected] oracle]# cat >>MehmetDeveci 
Hi, I am Mehmet Salih Deveci

^C
[[email protected] oracle]# 
[[email protected] oracle]# cat MehmetDeveci

State : Optimal
Strip Size : 1.0 MB
Number Of Drives : 4
Span Depth : 1

Is VD Cached: No

Exit Code: 0x00

Hi, I am Mehmet Salih Deveci

[[email protected] oracle]#


[[email protected] oracle]# cat >MehmetDeveci 
Hi, I am Mehmet Salih Deveci

^C
[[email protected] oracle]# 
[[email protected] oracle]# 
[[email protected] oracle]# cat MehmetDeveci 
Hi, I am Mehmet Salih Deveci

[[email protected] oracle]#

 

 

 

 

more

With the help of this command, the contents of the specified file are displayed by page.

Cat command displays all content, but more command displays content of file page by page.

 

[[email protected] oracle]# more error.log 
Adapter #0

Enclosure Device ID: 252
Slot Number: 0
Drive's postion: DiskGroup: 0, Span: 0, Arm: 0
Enclosure position: 0

 

 

 

head 

This command displays the contents of the file as in the more command, but only the first 10 lines. Its use is as follows.

[[email protected] oracle]# 
[[email protected] oracle]# head error.log 
Adapter #0

Enclosure Device ID: 252
Slot Number: 0
Drive's postion: DiskGroup: 0, Span: 0, Arm: 0
Enclosure position: 0
Device Id: 8
WWN: 51340CCA06F1980A3
Sequence Number: 2
Media Error Count: 0
[[email protected] oracle]#

 

 

tail 

This command displays the contents of the file as in the more command, but only the last 10 lines. Its use is as follows.

[[email protected] oracle]# tail error.log 
Disk Cache Policy : Disabled
Encryption Type : None
Bad Blocks Exist: No
PI type: No PI

Is VD Cached: No

Exit Code: 0x00
[[email protected] oracle]#

 

 

If you want to see more lines than 10 and keep printing new lines, you can use tail -Nf command.

I have used 15 lines in this example, If you want to change it, you can see more than 15 lines.

[[email protected] oracle]# tail -15f error.log 
Creation Time : 03:00:06 PM
Default Cache Policy: WriteBack, ReadAheadNone, Direct, No Write Cache if Bad BBU
Current Cache Policy: WriteBack, ReadAheadNone, Direct, No Write Cache if Bad BBU
Default Access Policy: Read/Write
Current Access Policy: Read/Write
Disk Cache Policy : Disabled
Encryption Type : None
Bad Blocks Exist: No
PI type: No PI

Is VD Cached: No

Exit Code: 0x00

 

 

I will continue to explain Linux commands tutorial series in the next article.

 

  

Do you want to learn Linux System Administration for Beginners, then read the following articles.

https://ittutorial.org/linux-administration-tutorial-for-beginners/

 901 views last month,  2 views today

About Mehmet Salih Deveci

I am Founder of SysDBASoft IT and IT Tutorial and Certified Expert about Oracle & SQL Server database, Goldengate, Exadata Machine, Oracle Database Appliance administrator with 10+years experience.I have OCA, OCP, OCE RAC Expert Certificates I have worked 100+ Banking, Insurance, Finance, Telco and etc. clients as a Consultant, Insource or Outsource.I have done 200+ Operations in this clients such as Exadata Installation & PoC & Migration & Upgrade, Oracle & SQL Server Database Upgrade, Oracle RAC Installation, SQL Server AlwaysOn Installation, Database Migration, Disaster Recovery, Backup Restore, Performance Tuning, Periodic Healthchecks.I have done 2000+ Table replication with Goldengate or SQL Server Replication tool for DWH Databases in many clients.If you need Oracle DBA, SQL Server DBA, APPS DBA,  Exadata, Goldengate, EBS Consultancy and Training you can send my email adress [email protected].-                                                                                                                                                                                                                                                 -Oracle DBA, SQL Server DBA, APPS DBA,  Exadata, Goldengate, EBS ve linux Danışmanlık ve Eğitim için  [email protected] a mail atabilirsiniz.

Leave a Reply

IT Tutorial