DEV Community

Prasanth
Prasanth

Posted on

1

Linux-Basic command

Table-content:

1.Terminal Cursor movement
2.Basic commands
3.Standard Streams in Linux

1.Terminal Cursor movement

i. Ctrl + A - move to the beginning of the lin e
ii. Ctrl + E - Move to the end of the line
iii. Ctrl + B -- Move backward one character
iv. Ctrl + F - move forward one character
v. Alt + B - Move backward one word
vi. Alt + F - Move forward one word
vii. Ctrl + R - Reverse search command history, Ctrl + R after press up Arrow.
viii. Ctrl + G cancel search history.
ix. Ctrl + L - clear the terminal
X. TAB - auto completion filename or command
xi. Ctrl + Z - stop the current command and resume wit fg in foreground or bg in background
xii. !! = repeat last command.

2.Basic commands

1.check current working directory

=> #pwd

output:-

/root

2.check current logged user

i. $ whoami- Display the username of currently logged in user.
ii.$ who - shows all logged user along with terminal sessions
iii. $ w - shows all logged user along with terminal sessions with activities.

3.chck the system hostname

i. $ hostname

ii. Show the detailed information about Hostname
$ hostnamectl
iii. check system ip & mac with help of hostname
$ hostname -I
(we will see, up coming blogs how to set the hostname temporarily and permanent)

4. check ip address

$ ip a
$ ip addr
$ ifconfig
(we will see, up coming blogs how to set the ip address ,gateway, broadcast,...etc, permanent and detailed result information)
5.clear the screen - $ clear

6.man = man is manual page of all command provide detailed documentation, syntax, options, example.

example:-

$ man ls

7.sudo
sudo is you allows you run command as super user ( admin privileges)

Example:-

$ sudo dnf update
- dnf is RHCL based package manger

8.$ history
-used to find out all those commands we ran previously.

9. create directory(folder)

`$ mkdir dir

$ mkdir dir1 dir2

$ mkdir -p grandpa/father/son

10 . Change directory

$ cd /home/user/Documents
$ cd .. => parent directory (move up one level in the directory structure)
$ cd . -> stay in the current directory
$ cd ../../ => move up multiple level.
$ cd - => go back to the previous directory
$ cd / = change root directory
$ cd ~ = change the home directory

11. Create file:-

i. create empty file using touch

$touch file.txt
$touch file.txt file1.txt

ii. Using echo

$echo "Welcome to MyLinuxBlog" > welcome.txt

iii. using cat

$cat > file.txt

Hi, this the text file(text somthing)
then Ctr + D(save)

iv. printf

$printf " Line-1 \n Line-2 \n Line-3 " > file.txt

12. List the file and directory

i. $ ls -lah
-l = list
-a = hidden
-h = human readable format
ii. $ ls -li
-i check inode for files and directory.
iii. $ ls -R
-R = recursively search (show the content of sub-ls directory)
iv. $ ls -ld */ (list the directory only)
v. $ ls -ld .. (parent)
vi. $ ls -ld . (current)

-t = sort by modification time

13. Copy the file and directory.

$ cp f1.txt destination-path/dirname

Example:-

$ cp file.txt dir1

Source to Destination copy:

$ cp file.txt /home/user/backup/

$ cp -r dir1 /home/user/backup/

Destination to Source copy:

$ cp /home/user/backup/file2.txt /home/user

option:-

-v verbose
-i = confirmation
-n = copy with without overwriting

14. Move and rename the file and directory

Rename the file:-

mv Oldname.txt Newname.txt

Move the file:-

mv file1.txt /home/usr/backup/

mv file2.txt file3.txt /home/user/backup/

option:-

-i - before moving ask confrimation.
-u -update
-v -verbose
-f force

*same method to follow directory move and rename.

when use mv the directory why do not use -r only using cp -r for dir ?

15. Delete the File and Directory

$rmdir dir1
(empty directory)
$rm -rfi dir (delete everything inside all directory )

option:-

-i - confirmation
-r - recusively
-v - verbose
-f - force
$rm -if file.txt

*-rf * = Be careful to use this option when deletion ,make to confirm once or four before use this option, can not retrieve the data after deletion.
rm -rf * ==> (* means all)Be careful

16. cat – display the content of the file

$cat > file.txt (it create the new file ,
it waits for you enter some text(owerites)
Ctrl + D(save)
$cat >> file.txt
appends to the file without overwriting existing content.
$cat -n file.txt -> Show with line numbers
$cat file1 file2 > merged.txt -> Merge files
$cat file.txt | grep "word" -> Search for a word
$tac file.txt -> Display in reverse.

17.more -Display the content page by page only navigation to forward direction not backward direction.

Example:

$more file.txt

Enter – scroll down one line
space – scroll down one page
-q - quite

$more +n file.txt
$more -n file.txt
n = number,

Example:-

$more +10 file.txt

18. less -Display the content page by page only navigation to forward direction and backward direction.

$less file.txt

output:-

hello welcome to Linux world
/search_word

example:
/Linux
space – scroll down one page
q - quite
b – scroll up up one page
g - go the begning of the file
G -go to end of the file.

19.head -display the first few lines, not real-time monitoring.

Example:-

$head f1.txt

$head -n 420 f1.txt

$head -c 20 f1.txt

-c display first 20 byte of the file content

20.tail-display the last few lines, real-time monitoring.

Example:-

$tail f1.txt
$tail-n 420 f1.txt
$tail -c 20 f1.txt


21. Standard Streams in Linux:-

1.stdin(0) - Takes input from the keyboard and file
2.stdout(1) -shows output like what you see on the screen
3.stderr(2) -shows error message ,also on the screen

1. Standard Streams

i. Standard Input (stdin - File Descriptor 0):

  • Take the input from the user like keyboard, when run the cat, read command it required input , it wait for you type something other wise Ctr + D.

ii. Standard Output (stdout - File Descriptor 1):

*share the output on your screen like when you run echo, ls command .

Example:-

$echo "Hellow Linux world"

*echo command send to text stdout ,it displays on your screen.

iii. Standard Error (stderr - File Descriptor 2):

  • send the error message on your screen
  • when made mistake on terminal send the message stderr not for stdout.

Example:-

$cd dir1

*dir1 is not created really at the time error message sent to stderr.

2. Redirection in Linux: stdin , stdout ,stderr .

Example:-

i.Redirect Input (<):

  • Input from the keyboard:

< -> Take the input from a file instead of the keyboard .
$ sort < names.txt
$ wc -l < nmaes.txt (wc -l -word count line)

  • stdout (1) -Output to the screen: $echo "Welcome To My Blog" > file.txt

ii. Redirect Output (>):

$ echo "Hello" > file.txt
$ echo "Hello world" >> file.txt

iii. Redirect Errors (2>):

Example:-

$ls /usr/home/ 2> err.log

*/usr/home/ this actually not real path ,at the time error message sent to stderr.

output:

No such file or directory.

22.Pipe operator:-

  • Pipe operator (|) connect multiple command.

syntax:-

command1 | command2

Example:-

$ ls | wc -l
$ cat file.txt | grep "Linux" (grep- pattern searcher)

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

If this article connected with you, consider tapping ❤️ or leaving a brief comment to share your thoughts!

Okay