Is Tech Making Linux Commands Better or Worse?

 Linux Commands:Part2

Summary 

file
wc
cat
vi
cp
echo
head
tail
sed
more
less
diff
sort
grep

 

file [option] [filename]
file command is used to determine the type of a file.
file type may be of human-readable(e.g. ‘ASCII text’) or MIME type(e.g. ‘text/plain; charset=us-ascii’)

file email.py
file name.jpeg
file Invoice.pdf
file exam.ods
file videosong.mp4

wc [OPTION]... [FILE]...
It is used to find out number of lines, word count, byte and characters count.
By default it displays four-columnar output

Examples: creating two files
cat states.txt
cat capital.txt

now we can check using wc
wc states.txt  [passing one file in aragument]
wc states.txt capital.txt [passing two files in aragument]

wc -l  number of lines
wc -w This option prints the number of words present in a file
wc -c count of bytes/characters

vi [filename]
vi filename: Creates a new file if it already not exist, otherwise opens existing file.
vi -R filename : Opens an existing file in read only mode.
view filename : Opens an existing file in read only mode.

cp
cp [OPTION] Source Destination
cp [OPTION] Source Directory
cp [OPTION] Source-1 Source-2 Source-3 Source-n Directory

cat
It reads data from the file and gives their content as output
cat [filename] display file content without line numbers.
cat -n [filename] display line numbers.
cat >newfile

echo
echo command in linux is used to display line of text/string
echo [option] [string]
echo [string]
\b : it removes all the spaces in between the text
\n : this option creates new line from where it is used.
\t : this option is used to create horizontal tab spaces.
\v : this option is used to create vertical tab space

head
By default, it prints the first 10 lines of the specified files
head [OPTION]... [FILE]...
create file called states.txt and execute this cmd.

head states.txt
head -n 5 states.txt : it will display first 5 lines
head -n 10 states.txt : it will display first 10 lines

tail
By default it prints the last 10 lines of the specified files
tail [OPTION]... [FILE]...

tail states.txt
tail -n 5 states.txt
tail -n 7 state.txt | sort -r
tail -f [filename]   --its going to show live update

sed
stream editor and it can perform searching, find and replace, insertion or deletion

print perticular line nuber
sed -n '12p' states.txt : it will print 12th line nunber
print b/w lines
sed -n '5,10p' states.txt
replace
sed 's/chandra/sekhar/' chandra.txt : it will replace chandra with sekhar in each line in chandra.txt file.

's/chandra/sekhar/g' chandra.txt : g means globally replace wherever is avaiable chandra.

more [filename]
more command is used to view the text files in the command prompt,
displaying one screen at a time in case the file is large (For example log files).
display line by line press enter key.

less [filename]
if u want to go screenby screen ctrl+f -forward
if u want to go backward screen ctrl+v -backward
finally :q u need to type to exit

diff b/w more and less
more traverse file in forward only & less forward + backward also
sort states.txt
cat states.txt | sort
cat states.txt | sort -r
cat states.txt | sort | tr [a-z] [A-Z] traslate small to caps.

grep
used to search
grep telanagana states.txt
grep -i Telanagana states.txt : ignore case  

Chandra Sekhar

A Blogger and Author! This post was written and edited by me, a technologist. I started this site to share my inspirations, work, and free materials, which I hope others may find useful as well. Man with a creative streak who enjoys experimenting with various web design strategies.

Previous Post Next Post

Microservice Communication in a Distributed System