Beginner Linux Commands

Summary

  • Dealing with Directories
  • Dealing with Files
  • Getting started with VIM
  • Manage Processes
  • Miscellaneous Commands

Dealing with Directories


mkdir  test -> Creates directory

cd test -> Change directory to test

cd .. -> Navigate to previous directory in the tree

cd - -> Navigate to previous directory

cp -rf test /usr/local -> Copies the test directory and all the content to /usr/local/

mv -rf test /usr/local -> Moves the test directory and all the content to /usr/local/

rm -rf test ->  Deletes test directory

mv test test2 -> Renames test to test2

Dealing with File Content


============================ CAT ======================================

cat test.txt (view contents in a file)
cat -n test.txt (view content with line numbers)

============================ WC =======================================

wc test.txt (view lines, words and characters)
wc -l test.txt (only lines)

============================ HEAD =====================================

head test.txt (get top 10 lines)
head -n 5 test.txt (returns top 5 lines)

============================ TAIL =====================================

tail test.txt will (return last 10 lines)
tail -n 5 test.txt (returns last 5 lines)
tail -f /var/log/messages (live monitoring on what is happening on the system)

============================ LESS ======================================

less test.txt (a simple editor)
/kernel (search kernel key word)

============================ GREP ======================================

grep bash /etc/passwd (searches word kernel in test.txt)
grep bash /etc/passwd /etc/group (search a word multiple files)
grep -e root -e bash /etc/passwd /etc/group
grep bas /etc/passwd -c (number of keywords found)
grep -i bas /etc/passwd (CASE INSENSITIVE)
grep root_file test.txt -w (Search exact word)

============================ CUT =========================================

cut -d : -f 1 /etc/passwd (cuts the row into columns based on delimiter : in this case)
cut -d : -f 1,5 passwd (1 and 5)
cut -d : -f 1-5 passwd (1 to 5)
free -m (view memory usage)
free -m foo.txt (writes the output to file)
cut -d ' ' -f 4 foo.txt4
[limitation of cut: files need to be even and well formatted]

============================ AWK ===========================================

awk '{print $4}' foo.txt (returns only 4th column default delimiter is spaces)
awk -F : '{print $1}' passwd (F-field seperator)
awk -F : '{print $1,$3}' passwd (1 and 3)

============================ SEARCH =========================================
find /root -iname "*.log" - Search for files with .log extension

Getting started with VIM

Quick Options in Vim Editor:


vim /etc/ssh/sshd_config

:set nu - Show line numbers

:set nonu - No line numbers

:set ic - Set Case insensitivity

:set noic - Set No Case insensitivity

yy - copy line

yw - copy word

10yy - copy 10 lines

dd - cut line

10dd - cut 10 lines

dw - cut word

gg - Puts Cursor at Beginning of a File

G - Puts Cursor at End of a File

Insert Mode 


i - Starts at Exact Cursor position

a - Start From Right Next to Cursor position

I - Puts Cursor position at Beginning of a Line

A - Puts Cursor position at End of a Line

o - Puts Cursor position to New line Below

O - Puts Cursor position to New line Above

cc - Puts the cursor by Deleting the Existing Line

Search


:s/root/rootX - Search (Case sensitive) & Replace root with rootX

:2s/root/rooX/gi - Search with case insensitivity and Replace first two search results

:%s/root/rootX/gi - Search with case insensitivity and Replace root with rootX

 

Manage Processes


htop -> list running process

kill 1232 or kill -TERM 1232 or kill -15 1232 -> Terminate process

kill -KILL 1232 or kill -15 1232 -> Force Terminate process

ps -> list all processes

ps aux -> list all processes with details

ps axjf -> list all process with details and tree

kill -HUP 1232 -> Hangup signal reloads config of the process

 

Miscellaneous Commands


arch - Print Machine architecture

cal -Display Calendar

date - Display Date and Time

which java - Display version of Java installed

Advertisements
Beginner Linux Commands

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s