Linux – File Commands (Faq)

1. Learn the below commands

ls -lrt -> usee this regularly
ls -al -> to view the hidden files

2. To navigate to your linux home directory

cd
cd ~
cd $HOME

cd – –> go backs to the directory where you come from.

cd.. –> Navigate to previous directory.

3. What are the options used for cat command.

[sdbt ~]$cat > file1.txt #To create a new file
linux is open source
linux is written by Linux Torvalds
linux is simple and elegant
written in the c programming language
cntrl+z

[sdbt ~]$ cat file1.txt #To view the file contents
linux is open source
linux is written by Linus Torvalds
linux is simple and elegant
written in the c programming language

[sdbt ~]$ cat >> file1.txt #To append the exist file
Linux is user for servers
linux is fast and secure
Its acts has multi user platforms

[sdbt ~]$ cat file1.txt #To view the file contents
linux is open source
linux is written by Linus Torvalds
linux simple and elegant
written in the c programming language
Linux is user for servers
linux is fast and secure
Its acts has multi user platforms

[sdbt ~]$ cat file1.txt > file2.txt #To divert output in another file

4. How to create a empty file

touch filename

> filename

5. Vi Editors

vi is a text editor. It can be used to edit all kinds of plain text. It is especially useful for editing programs.

There are three mode
– Command mode
– Insert mode
– Escape mode

Command mode
1G – Move Cursor into First Line of the File
nG – Move Cursor into nth Number of the Line in File
G – Move Cursor into Last Line of the Line in File.
yy – Copy a Single line.
nyy – Copy Multiple line.
dd – Delete a Single Line.
ndd – Delete Multiple Line.
dw – Delete Single word.
x – Delete a Single character.
r – Replace a Single Character.
R – Replace a Multiple Character.
p – Paste the deleted or copied information.
u – Undo the changes
k – Move the cursor upward.
j – Move the cursor downward.
h – Move the cursor left side.
l – Move the cursor right side.

Insert mode
i – allow insert cursor location.
I – allow insert start line of cursor location.
a – allow insert next to cursor location.
A – allow insert end line of cursor locaion.
o – open a new line below and allow insert from the cursor location.
O – open a new line above and allow insert from the cursor location.

Escaple mode
Esc + :
w -Save
q -Quit
! -Force
n -next
/string -Search string in file
rew -review the multiple file once more

7. Directory creation and removal

[sdbt ~]$ mkdir dir1 # create directory

[sdbt ~]$ mkdir dir2 dir3 dir4 # create Multiple Directory

[sdbt ~]$ mkdir -p dir5/sdira/sdirb/sdirc # create a Parental with Subdirectory

[sdbt ~]$ rmdir dir1 # removes empty directory

[sdbt ~]$ rm file.txt # removes/deletes a file

[sdbt ~]$ rm -rf dir5 # removes the directory which have contents

8. head and tail commands

head

Give Output the first 10 lines of the file

[sdbt ~]$ head alphabets.txt
aA
bB
cB
dD
eE
fF
gG
hH
iI
jJ

[sdbt ~]$ head -3 alphabets.txt
aA
bB
cB

tail
Give Output the bottom 10 lines of the file

[sdbt ~]$ tail alphabets.txt
qQ
rR
sS
tT
uU
vV
wW
xX
yY
zZ

[sdbt ~]$ tail -3 alphabets.txt
xX
yY
zZ

10. Other mandatory commands

sort
sort lines of text files.

[sdbt ~]$ sort fileinfo.txt
animals
animals
fruits
fruits
vechicles
vegetables
wechicles

uniq
Filter adjacent matching lines.

[sdbt ~]$ sort fileinfo.txt | uniq

animals
fruits
vechicles
vegetables
wechicles

comm
Compare sorted files file1 and file2 line by line

[sdbt ~]$ sort fileinfo.txt >infosort.txt[sdbt ~]$ sort filedummy.txt >infodsort.txt[sdbt ~]$ comm infosort.txt infodsort.txt

animals
animals
birds
fruits
fruits
vechicles
vegetables
wechicles

diff
Compare files line by line

[sdbt ~]$ diff fileinfo.txt filedummy.txt
2d1
< animals 4d2 < animals 6a5 > birds

wc
Print line,word and byte counts for each fie

[sdbt ~]$ wc alphabets.txt
26 26 78 alphabets.txt

[sdbt ~]$ wc -l alphabets.txt
26 alphabets.txt

-l lines
-w words
-c bytes

cp
Copy files and directories

[sdbt ~]$ cp file1.txt filenew.txt

[sdbt ~]$ cp -R dir5 dirdup

[sdbt ~]$ cp file1.txt /home/jlaxmi/dir5/sdira/fnew.txt

[sdbt ~]$ cp /home/jlaxmi/dir5/sdira/fnew.txt /home/jlaxmi/dirdup/sdira/fdup.txt

mv
Rename sorce to destination, or Move sources to directory.

[sdbt ~]$ mv file1.txt filepast.txt

[sdbt ~]$ mv filepast.txt /home/jlaxmi/dir5/sdira/file1.txt

[sdbt ~]$ mv /home/jlaxmi/dir5/sdira/file1.txt /home/jlaxmi/dirdup/sdira/fileone.txt

11. What is the use of strings commands.

strings is used for filter characters from the binary file.
We use this command to create pfile from spfile , when spfile is corrupted.