Basic Commands practice

Basic Commands in Linux

Basic Commands :

1) pwd : -> used to know the path of present user.

2) ls : ->It gives the list of files & Directories present in the path

3) ls -l or ll: -> Listing of all files along with attributes

4) ls -a : -> Lists all hidden files & directories

5) ls -r : -> lists all files & directories in reverse mode

6) ls -il : -> lists all files & directories along with inode numbers

7) ls -ld <DIR>: -> to view attributes of a particular directory

8) ls -R <DIR>: -> to view tree structure of a directory

9) ls -a* : -> to view all files & directories starts with a

10) man <CMD> : -> to view entire options of a command

To create a file we have 3 methods :

1) touch
2) cat
3) vi(vim)

1) touch :

+ With the help of touch command we can create an empty file.
     syntax: touch
     ex: touch f1
+ With this command we can create n number of files at a time.
     syntax: touch
     ex: touch sun moon stars

2) cat:

+ By using cat we can create datafiles
syntax: cat >
cat > mahesh
(edit data or enter the data)
(ctrl+d)(to save)
+ To view content of a file
cat
+ To add data in existing file
cat >>
(typedata)
(ctrl+d)
+ To view output of file1 file2 we use cat file1 file2. In the same way we can transfer output of that command as input for another file 
    cat file1 file2 > file3
here we are giving file1 file2’s data as input for file3

Note:we cannot edit the written text through cat command. we can only add data to modify existing data we have to use editors. we have number of editors in linux
Example: gedit ,nano, kedit, kate, emacs

3) vi(vim):

But we always use the best editor vim editor we can transfer an output as input for another file
You can grab more detiails through this article vi editor linux  

How To Practice In  Linux-System(command mode) :

1.ls -l  for listing the files as well as directories those are kept in
the particular working directory

root@lb:~# ls -l
total 7366072
-rw-r–r– 1 root root 7542846464 Feb 13 19:28 lb.tar.gz
drwxr-xr-x 2 root root 4096 Feb 13 23:08 Desktop

2.ls -la same as ‘ls -l’but by this command we can also see the hiden
files.

root@lb:~# ls -la
total 7366140
drwx—— 12 root root 4096 Mar 2 21:18 .
drwxr-xr-x 23 root root 4096 Feb 17 21:47 ..
-rw-r–r– 1 root root 7542846464 Feb 13 19:28 lb.tar.gz
-rw——- 1 root root 6372 Mar 4 06:56 .bash_history
-rw-r–r– 1 root root 3106 Feb 20 2014 .bashrc
drwx—— 7 root root 4096 Mar 2 21:43 .cache
drwxr-xr-x 10 root root 4096 Feb 19 19:39 .config
drwx—— 3 root root 4096 Feb 13 23:08 .dbus
drwxr-xr-x 2 root root 4096 Feb 13 23:08 Desktop

3.ls -li same as ‘ls -la’ but it will also shows us the inode number of
each and every file

root@lb:~# ls -li
total 7366072
4588439 -rw-r–r– 1 root root 7542846464 Feb 13 19:28 lb.tar.gz
4587530 drwxr-xr-x 2 root root 4096 Feb 13 23:08 Desktop

4.ls by this command we can see only file name nothing else

root@lb:~# ls
lb.tar.gz Desktop

5.clear it will clear the screen(short cut ctl+l)

root@lb:~# ls -li
total 7366072
4588439 -rw-r–r– 1 root root 7542846464 Feb 13 19:28 lb.tar.gz
4587530 drwxr-xr-x 2 root root 4096 Feb 13 23:08 Desktop
root@lb:~#
root@lb:~#
root@lb:~# ls
lb.tar.gz Desktop
root@lb:~# clear

root@lb:~#

6.exit to end a current session as well current terminal logging

root@lb:~# exit
logout
lb@lb:~$

7.touch to create a new empty file

root@lb:~# touch test
root@lb:~# ls
lb.tar.gz Desktop test

8.cd to change the working/present directory

root@lb:~#cd /home/lb

where ‘/home/lb’ is the desired directory to be change from ‘/root’

9.cat to view the contents of a file and it is also used for creating a new file with some contents

root@lb:~# cat > lb.txt
linuxbeginners.org
—————–
-> One site to learn Linux
root@lb:~# ls
lb.tar.gz Desktop lb.txt test
root@lb:~# cat lb.txt
linuxbeginners.org
—————–
-> One site to learn Linux
root@lb:~# cat >> lb.txt
-> One site to share my article tooo..
root@lb:~# cat lb.txt
linuxbeginners.org
—————–
-> One site to learn Linux
-> One site to share my article tooo..
root@lb:~#

10.mkdir to make a new directory
mkdir newdirname
You can also create a directory at your desired path without changing your present working directory
You can also create directory in a path

root@lb:~# mkdir lb
root@lb:~# mkdir /root/lb1
root@lb:~# mkdir -p /root/lb1/lb2
root@lb:~# mkdir -p /root/lb4 lb5 lb6
root@lb:~# ls
lb.tar.gz Desktop lb lb1 lb4 lb5 lb6 lb.txt test

11.rm to remove a empty file

root@lb:~# rm test
root@lb:~# ls
lb.tar.gz Desktop lb lb1 lb4 lb5 lb6 lb.txt

12.rmdir to remove a empty directory

root@lb:~# rmdir lb6
root@lb:~# ls
lb.tar.gz Desktop lb lb1 b4 lb5 lb.txt

13.rm [-i/-r/-f] to remove a directory with its subdirectories as well as its files that is to remove a directory which already contains some files in it

syntax
  -i stands for interactively
  -r stands for recursively
  -f stands for forcefully

root@lb:~# rm lb1/
rm: cannot remove ‘lb1/’: Is a directory
root@lb:~# rm -ifr lb1/

14.cp to copy something in a destination file or directory

root@lb:~# cp lb.txt linuxbeginners.org
root@lb:~# ls
lb.tar.gz Desktop lb lb4 lb5 lb.txt linuxbeginners.org

15.mv to move one file or directory from one place to another place, it is also used for renaming a directory or file.

syntax
mv source destination
mv oldfilename newfilename [to change the file name]

root@lb:~# mv linux.beginners.org lb
root@lb:~# ls
lb.tar.gz Desktop lb lb4 lb5 lb.txt lb

16.man to view the mannual page of commands for syntax

root@lb:~# man cp
CP(1) User Commands CP(1)

NAME
cp – copy files and directories

SYNOPSIS
cp [OPTION]… [-T] SOURCE DEST
cp [OPTION]… SOURCE… DIRECTORY
cp [OPTION]… -t DIRECTORY SOURCE…

DESCRIPTION
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.

-a, –archive
same as -dR –preserve=all

–attributes-only
don’t copy the file data, just the attributes

–backup[=CONTROL]
make a backup of each existing destination file

-b like –backup but does not accept an argument

–copy-contents
copy contents of special files when recursive

-d same as –no-dereference –preserve=links

17.info to view the information about any command

root@lb:~# #mkdir info

18.–help to view the help doccuments of a command

root@lb:~# ls –help
Usage: ls [OPTION]… [FILE]…
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor –sort is specified.

Mandatory arguments to long options are mandatory for short options too.
-a, –all do not ignore entries starting with .
-A, –almost-all do not list implied . and ..
–author with -l, print the author of each file
-b, –escape print C-style escapes for nongraphic characters
–block-size=SIZE scale sizes by SIZE before printing them. E.g.,
‘–block-size=M’ prints sizes in units of
1,048,576 bytes. See SIZE format below.
-B, –ignore-backups do not list implied entries ending with ~
-c with -lt: sort by, and show, ctime (time of last
modification of file status information)
with -l: show ctime and sort by name
otherwise: sort by ctime, newest first
-C list entries by columns
–color[=WHEN] colorize the output. WHEN defaults to ‘always’
or can be ‘never’ or ‘auto’. More info below
-d, –directory list directory entries instead of contents,
and do not dereference symbolic links
-D, –dired generate output designed for Emacs’ dired mode
-f do not sort, enable -aU, disable -ls –color
-F, –classify append indicator (one of */=>@|) to entries
–file-type likewise, except do not append ‘*’
–format=WORD across -x, commas -m, horizontal -x, long -l,
single-column -1, verbose -l, vertical -C
–full-time like -l –time-style=full-iso
-g like -l, but do not list owner
–group-directories-first
group directories before files.
augment with a –sort option, but any
use of –sort=none (-U) disables grouping
-G, –no-group in a long listing, don’t print group names
-h, –human-readable with -l, print sizes in human readable format
(e.g., 1K 234M 2G)
–si likewise, but use powers of 1000 not 1024
-H, –dereference-command-line
follow symbolic links listed on the command line
–dereference-command-line-symlink-to-dir
follow each command line symbolic link
that points to a directory
–hide=PATTERN do not list implied entries matching shell PATTERN
(overridden by -a or -A)
–indicator-style=WORD append indicator with style WORD to entry names:
none (default), slash (-p),
file-type (–file-type), classify (-F)
-i, –inode print the index number of each file
-I, –ignore=PATTERN do not list implied entries matching shell PATTERN
-k, –kibibytes use 1024-byte blocks
-l use a long listing format
-L, –dereference when showing file information for a symbolic
link, show information for the file the link
references rather than for the link itself
-m fill width with a comma separated list of entries
-n, –numeric-uid-gid like -l, but list numeric user and group IDs
-N, –literal print raw entry names (don’t treat e.g. control
characters specially)
-o like -l, but do not list group information
-p, –indicator-style=slash
append / indicator to directories
-q, –hide-control-chars print ? instead of non graphic characters
–show-control-chars show non graphic characters as-is (default
unless program is ‘ls’ and output is a terminal)
-Q, –quote-name enclose entry names in double quotes
–quoting-style=WORD use quoting style WORD for entry names:
literal, locale, shell, shell-always, c, escape
-r, –reverse reverse order while sorting
-R, –recursive list subdirectories recursively
-s, –size print the allocated size of each file, in blocks
-S sort by file size
–sort=WORD sort by WORD instead of name: none -U,
extension -X, size -S, time -t, version -v
–time=WORD with -l, show time as WORD instead of modification
time: atime -u, access -u, use -u, ctime -c,
or status -c; use specified time as sort key
if –sort=time
–time-style=STYLE with -l, show times using style STYLE:
full-iso, long-iso, iso, locale, +FORMAT.
FORMAT is interpreted like ‘date’; if FORMAT is
FORMAT1FORMAT2, FORMAT1 applies to
non-recent files and FORMAT2 to recent files;
if STYLE is prefixed with ‘posix-‘, STYLE
takes effect only outside the POSIX locale
-t sort by modification time, newest first
-T, –tabsize=COLS assume tab stops at each COLS instead of 8
-u with -lt: sort by, and show, access time
with -l: show access time and sort by name
otherwise: sort by access time
-U do not sort; list entries in directory order
-v natural sort of (version) numbers within text
-w, –width=COLS assume screen width instead of current value
-x list entries by lines instead of by columns
-X sort alphabetically by entry extension
-Z, –context print any SELinux security context of each file
-1 list one file per line
–help display this help and exit
–version output version information and exit

SIZE is an integer and optional unit (example: 10M is 10*1024*1024). Units
are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, … (powers of 1000).

Using color to distinguish file types is disabled both by default and
with –color=never. With –color=auto, ls emits color codes only when
standard output is connected to a terminal. The LS_COLORS environment
variable can change the settings. Use the dircolors command to set it.

Exit status:
0 if OK,
1 if minor problems (e.g., cannot access subdirectory),
2 if serious trouble (e.g., cannot access command-line argument).

Report ls bugs to bug-coreutils@gnu.org
GNU coreutils home page:
General help using GNU software:
For complete documentation, run: info coreutils ‘ls invocation’

19.dir to view the subdirectories and filesn under the directory

root@lb:~# ls
lb.tar.gz Desktop info lb {lb4 lb5
root@lb:~# dir /root
lb.tar.gz Desktop info lb {lb4 lb5

20.su – to become a super user

lb@lb:~$ sudo su –
[sudo] password for lb:

21.who by this command you can see the user name and their ip addresses
who have loged in on your server

root@lb:~# who
lb :0 2015-03-05 21:14 (:0)
lb pts/0 2015-03-05 22:24 (:0)

22.whoami this command shows your current logged in terminal user name

root@lb:~# whoami
root

23.who am i this command shows you the logged in terminal number and user name and more detailed information

root@lb:~# who am i
lb pts/0 2015-03-05 22:24 (:0)

24.pwd to view the present working directory

root@lb:~# pwd
/root

25.rpm -ivh to intall a rpm package

syntax
root@lb:~# #rpm -ivh packagename.rpm
rpm stands for ‘redhat package manager’

     -i stands for install
     -v stands for verbose mode
     -h stands for with hash sign(#)
26.rpm -q to querry about any rpm package

root@lb:~# #rpm -q packagename

27.rpm -e to uninstall a rpm package

root@lb:~# #rpm -e package

28.find / -name to find any file or directory in linux file system

root@lb:~# #find / -name filename

29.su username to switch from one user to another users home directory

root@lb:~# #su mango
output will be
[mango@nettech root]#cd
[mango@nettech mango]#

30.su – username to switch from one user to another user users home directory directly

syntax
root@lb:~# #su – mango

31.useradd to create a new user

synatx
root@lb:~# #useradd username

32.passwd to give a password of a user

syntax
root@lb:~# #passwd tarun
output will be
give a password for user tarun:(here you have to type a password for tarun user)
confirm password:(again type the same password)

33.userdel to remove a user from linux

syntax
root@lb:~# #userdel tarun
34.groupadd to add a new group

syntax
root@lb:~# #groupadd groupname

35.gruopdel to delete a group

syntax
root@lb:~# #groupdel groupname

36.chown to change the ownership of a file or directory

syntax
root@lb:~# #chown ownername filename
example:
[root@nettech /]#ls -l
output
drwxrw-rw- 2 root root 4096 Mar 11 12:03 abcd
(permission) (own) (group own)(size) (name)
root@lb:~# #chown tarun /abcd
in this example /abcd directory owner will be change to tarun user
effect
[root@nettech /]#ls -l
drwxrw-rw- 2 tarun root 4096 Mar 11 12:03 abcd

37.chgrp to change the group ownership of a file or directory

syntax
[root@nettec root]#chgrp newgroupownername filename
example
[root@nettech /]#ls -l
drwxrw-rw- 2 tarun root 4096 Mar 11 12:03 abcd
root@lb:~# #chgrp tarun /abcd
effect
[root@nettech /]#ls -l
drwxrw-rw- 2 tarun tarun 4096 Mar 11 12:03 abcd

38.chmod to change the permission of a file or directory
drwxrw-rw- 2 root root 4096 Mar 11 12:03 abcd
(permission) (ownr) (grpownr) (size) (name) IN OCTAL VALUE

d stands for directiry
r stands for read only permission READ=4
w stands for write only permission WRITE=2
x stands for execute only permission EXECUTE=1

drwxrw-rw- FIRST OCTET FOR DENOTING THE DIRECTORY OR FILE OR LINK FILE ETC.

SECOND THREE OCTET FOR USER OR OWNER PERMISSION (rwx OR 7 IN OCTAL VALUE)
THIRD THREE OCTET FOR GROUP PERMISSION (rw- OR 6 IN OCTAL VALUE)
FORTH THREE OCTET FOR OTHERS PERMISSION (rw- OR 6 IN OCTAL VALUE)

SYNTAX
[root@nettec root]#chmod value fileordirectoryname
example
[root@nettech /]#ls -l
drwxrw-rw- 2 tarun root 4096 Mar 11 12:03 abcd
[root@nettech /]#chmod 402 /abcd
[root@nettech /]#ls -l
drw—–w- 2 tarun tarun 4096 Mar 11 12:03 abcd

40.usermod to modify the user profile

synatx
root@lb:~# #usermod -parameter groupname username

File Prompting Commands :

head    :   used to view first ten lines of a file
syntax :   head
 ex: head /etc/passwd

To view 5 lines of a file
head -n
 ex: head -n 5 /etc/passwd

Tail : is used to view last ten lines of a file
syntax: tail
ex: tail /etc/passwd

To view last 20 lines of a file
tail -n 20 /etc/passwd

more : used to see the content pagewise but we cannot scroll up
syntax: more
ex: more /etc/passwd

less: used to see the content pagewise we can scroll up&down
syntax: less
ex: less /etc/passwd

date: used to view current date&time
to change date
date
ex: date 120111452013
it means 12th month
01 date
11:45 11 hours 45 minutes
2013 year
or
date -s
date -s “wed june 20 20:46:51 IST 2013”

cal : cal is used to view present months calender
cal to view a particular month,particular years calender

cal

cal 3 1983 ————-> to view march 1983 calender
cal 2020 —————-> to view entire 2020 year calender 
    | ——–> this symbol is called as pipe it is used to link conmmands
ex: ls | grep pot

grep : is used to skip something specially from the output
here in example from output of ls we are grepping pot

ll | grep “^d” ———> here from the out put of ll we are greping only directories
mkdir —–> to create a directory
ex: mkdir sun

To create multiple directories :
mkdir
ex: mkdir sun moon stars

To create nested directory
mkdir -p
mkdir -p sun/moon/stars

To change the directory
cd
to change directory one level back
cd ..
to change directory two levels back
cd ../..
Removing a file or directory
to remove a file
rm
to remove an empty directory 
rmdir
to remove directory recursively & forcefully
rm -rf -> to delete directory with out asking yes or no

copying a file
cp —–> to copy a file
ex: cp file1 /etc/
cp -rf —> to copy entire directory along with subdirectories & files
ex: cp -rf sun /ds/dawn
cp -a —–> to copy a file or directory along with permissions
ex: cp -a Server /var/ftp/pub

To move a file or directory
mv
mv deepu /sun/moon

To rename a file or directory
mv

To create chain commands
in linux we can execute “n” number of commands at a single command line
syntax: command1;command2;command3;

ex: date;cal2013;date 120111452013;mkdir mahesh;cd mahesh;cd